Program in Java. Write a program in Java that assigns seats on a small airplane
ID: 3780372 • Letter: P
Question
Program in Java.
Write a program in Java that assigns seats on a small airplane (30 seats: 10 rows with 3 seats each). Your program should display a menu of alternatives:
Please choose an option:
1 for “first class” 2 for “economy”
3 to view seating chart 0 to exit
If the person types 1, then your program should ask the user for a seat number in the first class section (seats 1-9). If the person types 2, then your program should ask the user for a seat number in the economy section (seats 10-30).
Your program needs to validade that the user entered a valid seat number and check if the seat is empty before assigning it.
Your program should not assign a seat that has already been assigned.
If the person types 3, then your program should display a seat map of the plane, with each empty seat represented by an ‘o’ and each occupied seat represented by an ‘x’. You should display the seats on the plane in the following orientation.
Front
x x x
x x o
o o o
x x x
x x x
x x x
x o o
o o o
x o x
o o o
Back
Your program should loop until the user enters 0, even if the plane is already full.
1. Use a 1-dimensional boolean array to represent the seating chart of the plane. Initialize all elements of the array to false to indicate that all seats are empty. As each seat is assigned, set the corresponding element of the array to true to indicate that the seat is no longer available.
2. You must write a function viewSeatingChartVertical that takes the array as a parameter and displays the seating chart. This function does not return anything.
3. You must call the above function from your main().
Please see the sample output for clarification on the program behavior.
Your program should:
1. Print your own name – that is a simple System.out.println(“your name”); with your own name.
2. Display the menu, get a number from the user, perform the requested action, then display the menu again, and continue this loop until the user chooses option 0.
3. Validate that the number entered by the user is a valid option. You may assume the user will always enter an integer, therefore you do not need to check for invalid characters. But you need to check whether the integer is within the valid range. If the user enters an invalid option (-1, 34, 55 ….), display an error message, then display the menu again.
4. Add an option to the menu – and the corresponding function, viewSeatingChartHorizontal – to display the plane horizontally. Your plane may point either to the left or to the right, but the seats must be printed in the correct order (a rotated version of the vertical layout), for example:
x o o x x x o o x o
Front x x o x x x o o o o Back
x x o x x x x o x o
OR
o x o x x x x o x x
Back o o o o x x x o x x Front
o x o o x x x o o x
Remember:
You must include comments describing major steps of the program.
See sample output on the bottom
Sample Output (single run)
----jGRASP exec: java airplane
Hello. My name is Blank.
Please choose an option:
1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 4
Your choice must be a number between 0 and 3.
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 1
Which seat would you like (1-9): 10
Seats in first class must be between 1 and 9.
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 2
Which seat would you like (10-30): 5
Seats in economy class must be between 10 and 30.
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 1
Which seat would you like (1-9): 1
Seat 1 successfully assigned.
Please choose an option:
1 for "first class"
2 for "economy"
3 to view seating chart
0 to exit
? 1
Which seat would you like (1-9): 3
Seat 3 successfully assigned.
Please choose an option: 1 for "first class 2 for "economy"
3 to view seating chart 0 to exit
? 1
Which seat would you like (1-9): 1
Seat 1 is already occupied.
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 1
Which seat would you like (1-9): 4
Seat 4 successfully assigned.
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 2
Which seat would you like (10-30): 10
Seat 10 successfully assigned.
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 2
Which seat would you like (10-30): 11
Seat 11 successfully assigned.
Please choose an option: 1 for "first class"2 for "economy"
3 to view seating chart 0 to exit
? 2
Which seat would you like (10-30): 21
Seat 21 successfully assigned.
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 2
Which seat would you like (10-30): 22
Seat 22 successfully assigned.
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 1
Which seat would you like (1-9): 3
Seat 3 is already occupied.
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 1
Which seat would you like (1-9): 5
Seat 5 successfully assigned.
Please choose an option:
for "first class"
for "economy"
to view seating chart
0 to exit ? 2
Which seat would you like (10-30): 22
Seat 22 is already occupied.
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 3
^
/
/
/
/
| x o x |
| x x o |
| o o o |
| x x o |
| o o o |
| o o o |
| o o x |
| x o x |
| o o o |
| o o o |
/
/
|||
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 2
Which seat would you like (10-30): 24
Seat 24 successfully assigned.
Please choose an option:
for "first class"
for "economy"
to view seating chart
0 to exit ? 3
^
/
/
/
/
| x x x |
| x x o |
| o o o |
| x x o |
| o o o |
| o o o |
| o o x |
| x o x |
| o o o |
| o o o |
/
/
|||
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 1
Which seat would you like (1-9): 2
Seat 2 successfully assigned.
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 3
^
/
/
/
/
| x x x |
| x x o |
| o o o |
| x x o |
| o o o |
| o o o |
| o o x |
| x o x |
| o o o |
| o o o |
/
/
|||
Please choose an option: 1 for "first class" 2 for "economy"
3 to view seating chart 0 to exit
? 0
Bye!
----jGRASP: operation complete.
Explanation / Answer
Please refer to comments in code itself and execute the program for verifying the output.
Save code in Flight.java file.
Steps to execute this program:
1. javac Flight.java
2. java Flight
Program:
import java.util.Arrays;
import java.util.Scanner;
public class Flight {
// this function will print the message to choose for option
public static String ChooseOption(){
String str = "Please choose an option: 1 for "first class" 2 for "economy" 3 to view seating chart "vertically" 4 to view seating chart "horizontally" 0 to exit";
return str;
}
//this function will print seats vertically
public static void PrintSeats(boolean [] seats, int size){
System.out.println(" ^");
System.out.println(" / \");
System.out.println(" / \");
System.out.println("/ \");
System.out.println("/ \");
for(int i = 0;i <10; i++){
System.out.print("|");
for(int j = 0;j <3;j++){
char chr= 'o';
if (seats[i*3 + j] ) {
chr = 'x';
}
if (j == 2)System.out.print(chr);
else System.out.print(chr +" ");
}
System.out.println("|");
}
System.out.println("\ /");
System.out.println("\ /");
System.out.println("|||");
}
//this function will print seats horizontally
public static void viewSeatingChartHorizontal(boolean[] seats, int size){
for(int j = 0;j < 3;j++){
if (j == 1)System.out.print("Front ");
for(int i = 0;i < 10;i++){
char chr = 'o';
boolean tmp = seats[i*3 + j];
if (tmp){
chr = 'x';
}
if (i == 9)System.out.print(chr);
else System.out.print(chr +" ");
}
if (j == 1)System.out.print(" Back");
System.out.println();
}
}
public static void main(String[]args){
System.out.println("Hello. My name is Sud.");
System.out.println(ChooseOption());
Scanner scan = new Scanner(System.in);
boolean[] Seats = new boolean[31];
for(int i = 0;i < 30;i++)Seats[i] = true;
int choice = scan.nextInt();
while(choice != 0){
switch(choice){
case 1: // this in case of choice 1
System.out.println("Which seat would you like (1-9):");
int seat = scan.nextInt(); // reading user's input
if (seat <0 || seat > 9){ // checking seat range
System.out.println("Seats in first class must be between 1 and 9.");
}else{
if (Seats[seat - 1 ]){
Seats[seat - 1] = false;
System.out.println("Seat " + seat + " successfully assigned.");
}else{
System.out.println("Seat "+ seat + " is already occupied.");
}
}
break;
case 2:
System.out.println("Which seat would you like (10-30):");
seat = scan.nextInt();
if (seat <10 || seat > 30){
System.out.println("Seats in economy class must be between 10 and 30.");
}else{
if (Seats[seat - 1]){
Seats[seat - 1] = false;
System.out.println("Seat " + seat + " successfully assigned.");
}else{
System.out.println("Seat "+ seat + " is already occupied.");
}
}
break;
case 3:
PrintSeats(Seats, 30); // printing seats vertically
break;
case 4:
viewSeatingChartHorizontal(Seats, 30); // printing seats horizontally
break;
default:
System.out.println("Your choice must be a number between 0 and 3.");
}
System.out.println(ChooseOption());
choice = scan.nextInt();
}
System.out.println("Bye!.");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.