A theatre seating chart is implemented as a two-dimensional array of ticket pric
ID: 647955 • Letter: A
Question
A theatre seating chart is implemented as a 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 in Java that prompts users to pick either a seat or a price. Mark sold seats by changing the price to 0. When a user specifies a seat, make sure it is available. When a user specifies a price, find any seat with that price.
Explanation / Answer
import java.util.Scanner;
class Example{
public static void main(String arg[]){
int temp[][] = { {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 ,0 , 20 ,20 ,10 ,10},
{10, 10 ,20 ,20 ,20 ,0 , 20 ,20 ,10 ,10},
{10, 10 ,20 ,20 ,20 ,0 , 20 ,20 ,10 ,10},
{20, 20 , 0 , 0 ,40 ,40, 30, 30 ,20, 20},
{20, 30, 30, 40, 50, 50, 40, 30, 30 ,20},
{30, 40, 0, 50, 50, 50, 0 ,50 ,40, 30} };
double max=0,price=0;
int t = 0,j=0,i=0,field1=0,field2 = 0,choice=0;
do{
Scanner in = new Scanner(System.in);
for(i=0;i<9;i++){
for(j=0;j<10;j++){
System.out.print(temp[i][j]+" ");
}
System.out.println("");
}
System.out.println("Enter the seat Number to book:");
System.out.println("Enter the row");
field1 = in.nextInt();
if(field1>9)
System.out.println("wrong entry");
System.out.println("Enter the column");
field2 = in.nextInt();
if(field1>8)
System.out.println("wrong entry");
if(temp[field1][field2]==0)
System.out.println("seat already booked");
else{
temp[field1][field2]=0;
System.out.println("seat is booked");
}
System.out.println("Enter the price of seat to book:");
price=in.nextInt();
for(i=0;i<9;i++){
for(j=0;j<10;j++){
if(price==temp[i][j])
System.out.print(temp[i][j]+" ");
else
System.out.print(" * ");
}
System.out.println("");
}
System.out.println("Enter the choice to continue ");
choice=in.nextInt();
}while(choice==0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.