Right, I have almost got it. Been modifying it from what I have gotten from you
ID: 3816673 • Letter: R
Question
Right, I have almost got it. Been modifying it from what I have gotten from you guys. The other answers I've gotten were not even in the same chapter and were a little out of my league and hard to follow but thanks from what help I have gotten I think I'm almost there. I just need help finishing it.
Here are the instructions and below is the program I have so far.
Java programming 5th edition chapter 9 two dimensional arrays
Nothing is private in this chapter and there is only one class
Write a program that can be used to assign seats for a commercial airplane. The airplane has 13 rows, with 6 seats in each row. Rows 1 and 2 are first class, rows 3 through 7 are business class, rows 8 through 13 are economy class.
The program should be menu driven (with a loop) as follows:
Main Menu
1.Display Seating Plan
2. Choose a seat
3. Exit
The seating plan should display:
//The lines are there just so you can see them
________A___B___C___D___E___F
Row 1___*___*____X___*____X__X
Row 2 __*___X____*___X____*__X
Row 3 __*___*____X___X____*__X
Etc.
The * indicates the seat is available. The X indicates the seat is taken.The chart will begin with all *'s as all seats are empty.
When choosing a seat, the user will enter the seat ID, for example C5, and the program will check to see if that seat is available. Is so, it will mark that seat as taken, display the seat ID with the class and seat type designation (for example, F4 is a Window seat in Business class). If not, it will display a 'seat taken' message and let the user choose another seat.
Test the program:
Display the seating plan (it should have all*)
Choose seats A9, B4, E1, D13, C7, F4, D13, D4, B9, E4, A12, B4
Display the seating plan again (make sure it is correct)
import java.util.*;
public class airplaneArray
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
//declare array
int row = 0;
int seat = 0;
char[][] airplane = new char[13][16];
String seatNumber = "";
int choice;
//initialize seating plane with all '*' characters
for (int a = 0; a < 13; a++)
{
for (int b = 0; b < 6; b++)
{
airplane [a][b] = '*';
}//end for loop
}//end for loop
printMenu();
choice = console.nextInt();
while (choice !=3)
{
switch (choice)
{
case 1:
printSeatingPlan(airplane);
break;
case 2:
chooseSeats(airplane, console);
break;
default:
System.out.println("Invalid Selection");
}//end switch
printMenu();
choice = console.nextInt();
}
}//end main
public static void printSeatingPlan(char[][] airplaneSeats)
{
System.out.println (" A B C D E F");
for (int row = 0; row < 13; row++)
{
System.out.print ("Row " + (row + 1));
if (row < 9)
{
System.out.print (" ");
}//end if statement that lines up seating plan
for (int seats = 0; seats < 6; seats++)
{
System.out.print (" " + airplaneSeats[row][seats]);
}//end for seats loop
System.out.println ();
}//end for row loop
}//end printSeatingPlan
//method to display menu
public static void printMenu()
{
System.out.print (" +++++++++++++++MENU+++++++++++++++");
System.out.println (" 1. Display Seating Plan");
System.out.println ("2. Choose a seat" );
System.out.println ("3. Exit" );
System.out.println ("++++++++++++++++++++++++++++++++++");
System.out.print(" Enter your choice (1,2,or 3): ");
}//end printMenu
//method to choose seats
public static void chooseSeats(char[][] airplaneSeats, Scanner input)
{
}
}//end class airplaneArray
Explanation / Answer
Code:
import java.util.*;
class airplaneArray
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
//declare array
int row = 0;
int seat = 0;
char[][] airplane = new char[13][16];
String seatNumber = "";
int choice;
//initialize seating plane with all '*' characters
for (int a = 0; a < 13; a++)
{
for (int b = 0; b < 6; b++)
{
airplane [a][b] = '*';
}//end for loop
}//end for loop
printMenu();
choice = console.nextInt();
while (choice !=3)
{
switch (choice)
{
case 1:
printSeatingPlan(airplane);
break;
case 2:
chooseSeats(airplane, console);
break;
default:
System.out.println("Invalid Selection");
}//end switch
printMenu();
choice = console.nextInt();
}
}//end main
public static void printSeatingPlan(char[][] airplaneSeats)
{
System.out.println (" A B C D E F");
for (int row = 0; row < 13; row++)
{
System.out.print ("Row " + (row + 1));
if (row < 9)
{
System.out.print (" ");
}//end if statement that lines up seating plan
for (int seats = 0; seats < 6; seats++)
{
System.out.print (" " + airplaneSeats[row][seats]);
}//end for seats loop
System.out.println ();
}//end for row loop
}//end printSeatingPlan
//method to display menu
public static void printMenu()
{
System.out.print (" +++++++++++++++MENU+++++++++++++++");
System.out.println (" 1. Display Seating Plan");
System.out.println ("2. Choose a seat" );
System.out.println ("3. Exit" );
System.out.println ("++++++++++++++++++++++++++++++++++");
System.out.print(" Enter your choice (1,2,or 3): ");
}//end printMenu
//method to choose seats
public static void chooseSeats(char[][] airplaneSeats, Scanner input)
{
System.out.println("Select the row and enter the row number ");
int row=input.nextInt();
System.out.println("Enter the column ");
char column=input.next().charAt(0);
int col=6;
switch (column)
{
case 'A':
col=0;
break;
case 'B':
col=1;
break;
case 'C':
col=2;
break;
case 'D':
col=3;
break;
case 'E':
col=4;
break;
case 'F':
col=5;
break;
default:
System.out.println("Invalid Selection");
}//end switch
if(airplaneSeats[row-1][col] != 'X' && col<6){
airplaneSeats[row-1][col]='X';
}
else{
System.out.println("ALREADY BOOKED .PLEASE SELECT ANOTHER SEAT");
}
}
}//end class airplaneArray
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.