In: Computer Science
*****IN JAVA****
Implement a theater seating chart 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 code snippet that:
- uses a for loop to print the array with spaces between the seat prices
- prompts users to pick a row and a seat using a while loop and a sentinel to stop the loop.
- outputs the seat price to the user.
Source Code:
Output:
Code in text format (See above images of code for indentation):
import java.util.*;
/*class definition*/
public class seatingChart
{
/*main method*/
public static void main(String[] args)
{
/*Scanner class to read input from the user*/
Scanner scnr=new Scanner(System.in);
/*2D array initialization*/
int[][] prices={{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}
};
/*variables*/
int i,j,k=0,row,seat;
/*print array with spaces*/
for(i=0;i<9;i++)
{
for(j=0;j<10;j++)
{
System.out.print(prices[i][j]+" ");
}
System.out.println();
}
/*using while loop */
while(true)
{
/*prompt the user to enter row number*/
System.out.print("Enter a row number(1-9) or -1 to exit: ");
row=scnr.nextInt();
/*use sentinel to exit*/
if(row==-1)
break;
/*read seat number*/
System.out.print("Enter a seat number(1-10) : ");
/*display price of seat*/
seat=scnr.nextInt();
System.out.println("The price of seat is: "+prices[row-1][seat-1]);
}
}
}