A theater seating chart is implemented as two-dimensional array of ticket prices
ID: 3711743 • Letter: A
Question
A theater seating chart is implemented as two-dimensional array of ticket prices, like this:
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
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
20
20
30
30
40
40
30
30
20
20
20
30
30
40
50
50
40
30
30
20
30
40
50
50
50
50
50
50
40
30
Write a program that prompts the users to pick either a seat or price. Mark sold seats by changing the price the 0. When a user specifies a seat, make sure it is available. When a user specifies a price, find any seat with that price.
Note: Assume that if the user enters 1 for the row and 1 for the seat, the yellow marked sit will be sold. Java
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
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
20
20
30
30
40
40
30
30
20
20
20
30
30
40
50
50
40
30
30
20
30
40
50
50
50
50
50
50
40
30
Explanation / Answer
// Java program that prompts the user to pick either a seat or a price and allocate the seat accordingly
import java.util.Scanner;
public class TheatreSeating {
public static void main(String[] args) {
// create the seat price matrix
int seat_price[][] = new int[9][10];
for(int i=0;i<3;i++)
for(int j=0;j<seat_price[i].length;j++)
seat_price[i][j] = 10;
for(int i=3;i<6;i++)
for(int j=0;j<seat_price[i].length;j++)
{
if(j<2 || j>7)
seat_price[i][j] = 10;
else
seat_price[i][j] = 20;
}
for(int i=0;i<seat_price[6].length;i++)
{
if(i<2 || i>7)
seat_price[6][i] = 20;
else if(i<4 || i>5)
seat_price[6][i] = 30;
else
seat_price[6][i] = 40;
}
for(int i=0;i<seat_price[7].length;i++)
{
if(i==0 || i==seat_price[7].length-1)
seat_price[7][i] = 20;
else if(i<3 || i>6)
seat_price[7][i] = 30;
else if(i==3 || i==6)
seat_price[7][i] = 40;
else
seat_price[7][i] = 50;
}
for(int i=0;i<seat_price[8].length;i++)
{
if(i==0 || i==seat_price[8].length-1)
seat_price[8][i] = 30;
else if(i==1 || i==8)
seat_price[8][i] = 40;
else
seat_price[8][i] = 50;
}
Scanner scan = new Scanner(System.in);
int row,seat,price,choice;
System.out.println(" Menu");
System.out.println("1. Enter the seat");
System.out.println("2. Enter price");
System.out.print(" Choice(1-2) : ");
choice = scan.nextInt();
switch(choice)
{
case 1: System.out.print(" Enter row(0-10 ) : ");
row = scan.nextInt();
System.out.print(" Enter seat(0-9) : ");
seat = scan.nextInt();
if(seat_price[row][seat] != 0)
{
System.out.println(" Seat("+row+","+seat+") reserved for you " );
seat_price[row][seat]=0;
}else
System.out.println("Seat("+row+","+seat+") not avaiable " );
break;
case 2 : System.out.print(" Enter price(10,20,30,40,50) : " );
price = scan.nextInt();
seat = -1;
row = -1;
for(int i=0;i<seat_price.length;i++)
{
for(int j=0;j<seat_price[i].length;j++)
if(seat_price[i][j] == price)
{
seat_price[i][j]=0;
row = i;
seat = j;
break;
}
}
if(row != -1)
{
System.out.println("Seat("+row+","+seat+") reseved for you" );
}else
System.out.println("No seat for price "+price+" available" );
break;
default:
System.out.println("Invalid choice");
}
scan.close();
}
}
//end of program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.