In: Computer Science
Assignment Purpose
The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment)
Instructions
Seat Ticket Price
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 40 50 50 50 50 50 50 40 20
80 50 50 80 80 80 80 50 50 30
Seating Arrangement: Seats No.
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
Hint: Basically, you will search the 2D array for the input price, and as soon as you find a matching price, you will replace the price at that location with a 0.
Sample output 1
Please pick a price or press Q to quit: 80
Checking for the availability……
Your seat is confirmed! Your seat number is 81. Enjoy your movie
Please pick a price or press Q to quit: 80
Checking for the availability……
No seat at this price is available. Sorry!
Please pick a price or press Q to quit: Q
Thank you for using our online ticketing
service. Goodbye!
Sample output 2
Please pick a price or press Q to quit: 10
Checking for the availability……
Your seat is confirmed! Your seat number is 1. Enjoy your movie
Please pick a price or press Q to quit: 20
Checking for the availability……
Your seat is confirmed! Your seat number is 33. Enjoy your movie
Please pick a price or press Q to quit: 20
Checking for the availability……
Your seat is confirmed! Your seat number is 34. Enjoy your movie
Please pick a price or press Q to quit: 200
Please pick a valid price. Valid prices are $10, $20, $30, $40, $50, and $80
Please pick a price or press Q to quit: Q
Thank you for using our online ticketing service. Goodbye!
Code to copy along with screenshots of code and
output are provided.
Please refer to screenshots to understand the indentation of
code.
If you have any doubts or issues. Feel free to ask in
comments
Please give this answer a like, or upvote. This will be very
helpful for me.
================================================================
Screenshots of Code :
Screenshots of Output :
Code to copy:
import java.util.Scanner;
public class Program {
// declaring a 2d array to store prices of seats
static int[][] seatPrices=
{
{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, 40, 50, 50,
50, 50, 50, 50, 40, 20},
{80, 50, 50, 80,
80, 80, 80, 50, 50, 30}
};
// function to read price, validate and return
price
static int readPrice()
{
// decalring string to store
choice
String choice;
// creating scanner object to take
input from user
Scanner sc = new
Scanner(System.in);
System.out.print("Please pick a
price or press Q to quit: ");
// taking input
choice = sc.next();
if(choice.equals("q") ||
choice.equals("Q"))
{
// if user
enters 'q' or 'Q'
System.out.println("Thank you for using our online ticketing
service. Goodbye!");
// return
-1
return -1;
}
else if(choice.equals("10") ||
choice.equals("20") || choice.equals("30") || choice.equals("50")
|| choice.equals("80") )
{
// if user
enters valid price, then return as Integer
return
Integer.parseInt(choice);
}
else
{
// in case of invalid input
System.out.println("Please pick a valid price. Valid prices are
$10, $20, $30, $50, and $80 ");
// return
0
return 0;
}
}
// function to check availability of seat
static int seatAvailablity(int price)
{
System.out.println("Checking for
the availability……");
// iterating through 2d array using
for loop
for(int i = 0;i<9; i++)
{
for(int j =
0;j<10;j++)
{
if(price == seatPrices[i][j])
{
// if seat found then set value of that seat to
0
seatPrices[i][j] = 0;
// and return seat
number
return
(i*10)+(j+1) ;
}
}
}
// otherwise return -1 (if no seat
found)
return -1;
}
// function to print confirmation of seat
static void printConfirmation(int seatNumber)
{
if(seatNumber == -1)
{
// if seatNumber
is -1
System.out.println("No seat at this price is available.
Sorry");
}
else
{
// otherwise
print confirmation of seat
System.out.println("Your seat is confirmed! Your seat number is "+
seatNumber + ". Enjoy your movie");
}
}
// main method
public static void main(String args[])
{
// integer variable to hold price and seat number
int price;
int seatNumber;
// infinite while loop
while(true)
{
// calling readPrice() function and
storing price into price variable
price = readPrice();
if(price == -1)
{
// if -1 , means user entered q ,
so break
break;
}
else if(price == 0)
{
// price = 0 , means user entered
invalid choice, do nothing
}
else
{
// else call seatAvailibilty and
store seatNumber to seatNumber varaible
seatNumber =
seatAvailablity(price);
// call prinConfirmation
printConfirmation(seatNumber);
}
}
}
}
==================XXXXX=====================================