← All free-response questionsQuestion 4 · 2D Array · 6 points
Classroom Seating
A teacher's seating chart is a 2D array of student names. Row 0 is the front of the room. An empty seat holds null.
public class SeatingChart
{
/** The seating chart; rectangular, at least 1 x 1.
* Empty seats are null.
*/
private String[][] seats;
/** Moves students forward as described below and returns
* the number of students whose seat changed.
*/
public int moveForward()
{ /* to be implemented */ }
// There may be instance variables, constructors,
// and methods that are not shown.
}Your class
6 pointsWrite moveForward. In each column, students move toward row 0 to fill empty seats. Students in a column keep their front-to-back order, and all empty seats end up at the back of the column. Return the number of students who ended up in a different seat.
Before (. means empty):
| col 0 | col 1 | col 2 | |
|---|---|---|---|
| row 0 | . | Bo | . |
| row 1 | Ana | . | . |
| row 2 | . | Cy | Di |
| row 3 | Ed | . | Fay |
After moveForward(), which returns 5:
| col 0 | col 1 | col 2 | |
|---|---|---|---|
| row 0 | Ana | Bo | Di |
| row 1 | Ed | Cy | Fay |
| row 2 | . | . | . |
| row 3 | . | . | . |
Tab inserts spaces. Drafts save as you type.
Self-score
0 / 6