Solving the Theater Seating Lab. Instructions: 1 Chapter 6: P 6.24 (modified) Im
ID: 3690198 • Letter: S
Question
Solving the Theater Seating Lab. Instructions:
1
Chapter 6: P 6.24 (modified)
Implement a Theater Seating chart using a 2 dimensional array as shown on page 311 using the instructions in this presentation.
Write a program that:
Displays the seating of the Theater
and then column.
Prompts the user to pick a seat by row
2
Sold seats are shown as “X”. Sold seats cannot be selected.
After a seat is selected, the program will display (refresh) the seating available showing the selected seat as “X” and prompt for the next user to select a seat.
If a selected seat has already been sold, output that it is not available and prompt for another selection.
If all seats are sold (i.e. all “X”s), display “Theater Sold Out”, display the theater seating, and do not accept input (end the program).
3
The seating should be shown to the user in this order.
Higher priced seating
4
Sold seating should be shown as “X” with no dollar sign
Sold seat Row 1 Column 5
5
Sold seating cannot be selected again.
Sold seat selected
6
public static void main(String[] args) {
// initial values - rows and then columns
int[][] seats = { { 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 },
{ 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
{ 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 } };
You may find it easier to Reverse the seating in the array. Why ?
7
1. 2.
There should be 2 methods:
Checks to see if there are available seats Displays the current theater seating
Use a 2 dimensional array
Initialize the array
Set up a loop for user input and calls to the methods
8
Scanner in = new Scanner(System.in);
// initialize the array - rows and then columns
int[][] seats = { { 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 }, { 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
{ 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 } };
9
boolean quitFlag = false;
while (allSeatsTaken(seats) == false) {
showTheater(seats);
System.out.println("Pick a seat by row and then column or <q> to quit: ");
if (!in.hasNextInt()) {
quitFlag = true;
}
else
10
else {
int seatRow = in.nextInt(); seatRow = seatRow - 1; if((seatRow > 8) || (seatRow < 0))
seatRow = 5;
int seatColumn = in.nextInt(); seatColumn = seatColumn - 1; if((seatColumn > 9) || (seatColumn < 0))
seatColumn = 5;
Maintain boundaries
Maintain boundaries
Sold Flag
if (seats[seatRow][seatColumn] == 99) System.out.println("That seat is taken, choose another");
else
seats[seatRow][seatColumn] = 99;
11
if(allSeatsTaken(seats)) {
showTheater(seats);
System.out.println("The Theater is Sold Out!"); }
} }
System.out.println("Thank you ... !!"); }
Theater Full ?
12
public static void showTheater(int [][] seats) {
System.out.println("
for (int r = 8; r >= 0; r--) {
Available Theater Seating");
System.out.println(); for(int c = 0; c < 10; c++) {
if(seats[r][c] == 99)
System.out.print(" X "); else
System.out.print(" $" + seats[r][c]); }
} }
Start at 8
13
public static boolean allSeatsTaken(int [][] seats) {
boolean theaterFull = true;
for (int r = 8; r >= 0; r--) {
for(int c = 0; c < 10; c++) {
if(seats[r][c] != 99) {
theaterFull = false; return false;
} }
}
return theaterFull; }
Start at 8
Explanation / Answer
Hi, I have done coding and rearranged given code. Please test it.
import java.util.Scanner;
public class TheaterSeating {
public static void showTheater(int[][] seats) {
System.out.println("Available Theater Seating");
for (int r = 8; r >= 0; r--) {
System.out.println();
for (int c = 0; c < 10; c++) {
if (seats[r][c] == 99)
System.out.print(" X ");
else
System.out.print(" $" + seats[r][c]);
}
}
}
public static boolean allSeatsTaken(int[][] seats) {
boolean theaterFull = true;
for (int r = 8; r >= 0; r--) {
for (int c = 0; c < 10; c++) {
if (seats[r][c] != 99) {
theaterFull = false;
return false;
}
}
}
return theaterFull;
}
public static void main(String[] args) {
// initial values - rows and then columns
int[][] seats = { { 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 },
{ 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
{ 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 } };
Scanner in = new Scanner(System.in);
boolean quitFlag = false;
while (allSeatsTaken(seats) == false) {
showTheater(seats);
System.out.println(" Pick a seat by row and then column or <q> to quit: ");
int seatRow = -1;
int seatColumn = -1;
if (!in.hasNextInt()) {
quitFlag = true;
} else {
seatRow = in.nextInt();
seatRow = seatRow - 1;
if ((seatRow > 8) || (seatRow < 0))
seatRow = 5;
seatColumn = in.nextInt();
seatColumn = seatColumn - 1;
if ((seatColumn > 9) || (seatColumn < 0))
seatColumn = 5;
}
if (seats[seatRow][seatColumn] == 99)
System.out.println("That seat is taken, choose another");
else
seats[seatRow][seatColumn] = 99;
if (allSeatsTaken(seats)) {
showTheater(seats);
System.out.println("The Theater is Sold Out!");
}
}
System.out.println("Thank you ... !!");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.