← All free-response questionsQuestion 4 · 2D Array · 6 points
Theater Seats
A theater stores its seats in a 2D array of Seat objects. Row 0 is closest to the stage.
public class Seat
{
/** Returns true if this seat has been sold */
public boolean isSold()
{ /* implementation not shown */ }
// There may be instance variables, constructors,
// and methods that are not shown.
}
public class Theater
{
/** The seating chart; rectangular, with at least one row */
private Seat[][] seats;
/** Returns the index of the row closest to the stage that
* has at least groupSize unsold seats next to each other
* in the same row, or -1 if no row does.
* Precondition: groupSize >= 1
*/
public int findRow(int groupSize)
{ /* to be implemented */ }
// There may be instance variables, constructors,
// and methods that are not shown.
}Your class
6 pointsWrite findRow. It returns the smallest row index that contains a run of at least groupSize consecutive unsold seats. Return -1 if no row has such a run.
In this chart, X is sold and . is unsold:
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| row 0 | X | . | . | X | . |
| row 1 | . | X | . | . | . |
| row 2 | . | . | . | . | X |
findRow(2) returns 0. findRow(3) returns 1. findRow(4) returns 2. findRow(5) returns -1.
Tab inserts spaces. Drafts save as you type.
Self-score
0 / 6