Question: JAVA Here is my code with the instructions as comments. I am having an issue with the getNumberOfFreeSeats method. I have attached the error that

JAVA Here is my code with the instructions as comments. I am having an issue with the getNumberOfFreeSeats method. I have attached the error that I get after running the file using the test code.

CODE

public class Theater {

/*

* This method should return true if the row and column passed in are

* valid for the String array named seats (the row and column are within the array).

* You are guaranteed that the seats array has at least 1 row.

* All rows in the seats array have the same number of columns.

*/

public static boolean isSeatInTheater(int row, int column, String[][] seats) {

if (row

return false;

if (row > seats.length - 1)

return false;

int columnSize = seats[0].length - 1;

if (column > columnSize)

return false;

return true;

}

/*

* This method should return true if the customer directly in front of

* the customer at row, column is someone more than 3 units taller than them.

* A customer is front of another if they are in the same column but one

* row greater.

* Remember that row 0 is at the back of the theater. The front of the

* theater is the last row in the array.

* A height of -1 means there is no customer in the seat.

*/

public static boolean shouldCustomerBeMoved(int row, int column, int [][]heights) {

if(row == heights.length - 1)

return false;

if(heights[row + 1][column] == -1)

return false;

if(heights[row + 1][column] - 3 > heights[row][column]) {

return true;

}

return false;

}

/*

* This method should return the row number with the greatest number of occupied seats.

* If a seat is empty it will contain "" (an empty string), otherwise it will contain the name of

* the customer sitting there.

*/

public static int findMostOccupiedRow(String [][] names) {

int rowReturn = 0;

int highest = 0;

for(int row = 0; row

int count = 0;

for(int seat = 0; seat

if(!names[row][seat].equals(""))

count++;

}

if(count > highest) {

rowReturn = row;

highest = count;

}

}

return rowReturn;

}

/*

* This method should go through the array of seats and return how many seats are free (empty).

* If a seat is empty the array at that row column will contain "" (an empty string).

*

* This method should return the number of free seats.

*/

public static int getNumberOfFreeSeats(String [][] seats) {

int count = 0;

for (int i1 = 0; i1

for (int j = 0; j

if(seats[i1][j].equals(""))

count++;

}

}

return count;

}

/*

* This method should return the name of the tallest customer in the theater.

* The row/column in the array of heights corresponds to the row/column of the name

* of each customer.

* The arrays are guaranteed to be the same size.

*/

public static String getTallestCustomer(int [][] heights, String [][] names) {

int tallest = 0;

String name = "";

for (int i=0; i

for (int j=0; j

if (heights[i][j] > tallest) {

tallest = heights[i][j];

name = names[i][j];

}

}

}

return name;

}

/*

* This method searches the theater for a place to put a new customer.

* It searches an entire row before moving to the next row.

* i.e. It starts at row 0, column 0, then moves to row 0, column 1, and does not move to row 1 until it has searched

* all of row 0.

* A height of -1 in the heights array, or a name of "" in the names array means there is no customer in a seat (you

* do not have to check for both conditions).

*

* When it finds an empty seat, it puts the customer there if:

* the customer is not more than 3 units taller than the alien in the seat behind (row - 1) the empty seat and

* the alien in front of that seat (row + 1) is not more than three units taller than the customer.

*

* To place a customer, change the heights and names arrays to hold the customer's height and name.

* Return true if a seat was found for the customer (the arrays were changed).

* Return false otherwise.

*

*

*/

public static boolean putCustomerInEmptySeat(String name, int height, int [][] heights, String[][]names) {

for (int i = 0; i

for (int j = 0; j

if ((heights[i][j] == -1)

&& (heights[i - 1][j] + 3 >= height)

&& (heights[i + 1][j]

heights[i][j] = height;

names[i][j] = name;

return true;

}

}

}

return false;

}

/**

* This method returns the average height of all the patrons in the theater.

* If you get an error that says you returned NaN it means you divided by zero.

*/

public static double getAverageHeight(int [][] heights) {

int sum = 0;

int count = 0;

for (int i=0; i

for (int j=0; j

if (heights[i][j]!=-1) {

sum += heights[i][j];

count++;

}

}

}

if (count==0)

return 0.0;

double average = (double) sum / count;

return average;

}

/**

* This method should first compute the average height, and then it should go through

* the array of heights and create a space separated String with all of the names of customers with heights

* that are above average.

* You are allowed to have a trailing space.

* Example for this input (heights are in parenthesis)

* This is the input (heights are in parenthesis):

* column 0 | column 1 | column 2 | column 3 | column 4 | column 5 | column 6 | column 7 | column 8

* ------------------------------------------------------------------------------------------------------------------------------------------------------

* row 0 [ Qbycaj(81) | Plfnc(26) | Qkome(35) | (-1) | Smilreqoh(83) | Awvnmloiw(43) | (-1) | Cgaaa(100) | Blaladl(99) ]

*

* The average height is 66.71428571428571 (so if your getAverageHeight doesn't yet work, don't try this method).

* The expected return string is:

* " Qbycaj Smilreqoh Cgaaa Blaladl"

* OR

* "Qbycaj Smilreqoh Cgaaa Blaladl "

*/

public static String getAllAboveAverageHeight(int [][] heights, String[][] names) {

String a = "";

double average = getAverageHeight(heights);

for (int i=0; i

for (int j=0; j

if (heights[i][j] > average) {

a += names[i][j] + " ";

}

}

}

return a;

}

}

CONSOLE (ERROR)

JAVA Here is my code with the instructions as comments. I am

Exception in thread "main" java.lang.ArrayIndexOutOfBounds Exception: Index 2 out of bounds for length 2 at Theater.getNumberOfFreeSeats (Theater.java:69 at TheaterTests.testNumberOfFreeSeats (TheaterTests.java:1114 at TheaterTests.testGetNumberOfFreeSeats (TheaterTests.java: 699 at TheaterTests.main(TheaterTests.java:10

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!