Create an array of arrays:
To iterate, you can get the index or write a for each loop:
The copying methods we saw on the previous set of slides do not implement deep copy of an array of arrays.
Create a class called TheaterSeating with the following requirements (for all methods, think about what parameters you need):
true means the seat is occupied, false means it’s available.toString()true if successful, false if the seat was already takenSubmit your TheaterSeating.java solution to gradescope
public class TheaterSeating {
private boolean[][] seating;
private int available;
private int rows;
private int columns;
public TheaterSeating(int rows, int columns) {
seating = new boolean[rows][columns];
this.rows = rows;
this.columns = columns;
available = rows * columns;
}
public int countAvailableSeats() {
return available;
}
public String toString() {
String out = "";
for (int i=0; i < rows; i++) {
out += i + "\t";
for (int j=0; j < columns; j++) {
if (seating[i][j]) out += new String(Character.toChars(0x1F464)) + "\t";
else out += "__\t";
}
out += "\n";
}
return out;
}
public boolean bookSeat(int row, int column) {
if (row < 0 || column < 0) return false;
if (row >= rows || column >= columns) return false;
if (seating[row][column]) return false;
seating[row][column] = true;
available--;
return true;
}
public void cancelBooking(int row, int column) {
if (row >= 0 && row < rows && columns >= 0 && column < columns) {
if (seating[row][column]) {
available++;
seating[row][column] = false;
}
}
}
}.length attribute is part of each sub-array