Procedural Abstraction, Arrays, Algorithim Question 10 page 227 on Absolute C++
ID: 3619411 • Letter: P
Question
Procedural Abstraction, Arrays, AlgorithimQuestion 10 page 227 on Absolute C++
Write a program to assign passenger seats in an airplane. Assumea small airplane with a seat numbering as follows:
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
The program should display the seat pattern with an 'X' marking the seats already assigned. For example, after seats 1 A, 2 B, and 4 C are taken, the display should look like this:
1 X B C D
2 A X C D
3 A B C D
4 A B X D
5 A B C D
6 A B C D
7 A B C D
After displaying the seats available, the program prompts for the seat desired, the user types in a seat, and then the display of available seats is updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that seat is occupied and ask for another choice.
Help is appreciated, I am very stuck on this program
Explanation / Answer
please rate - thanks
import java.util.*;
public class main
{public static void main(String[] args) {
char[][] seats = new char[7][4];
int i,filled=0,row,col;
char c;
String input;
for (i=0;i<7;i++)
for(c='A';c<='D';c++)
seats[i][(int)(c-'A')]=c;
Scanner in = new Scanner(System.in);
printSeats(seats);
System.out.print("Enter seat selection(e.g.'1A')blank line to quit: ");
input=in.nextLine();
while((filled<28)&&(input.length()>0))
{row=(int)(input.charAt(0)-'1');
col=(int)(Character.toUpperCase(input.charAt(1))-'A');
if(row<0||row>6||col<0||col>3)
System.out.println("Invalid seat selection");
else if(seats[row][col]=='X')
System.out.println("Sorry-that seat is taken");
else
{filled++;
seats[row][col]='X';
}
printSeats(seats);
System.out.print("Enter seat selection(e.g.'1A')blank line to quit: ");
input=in.nextLine();
}
if(filled==28)
System.out.println("Plane full");
System.out.println("Final seat assignments: ");
printSeats(seats);
}
private static void printSeats(char[][] seats)
{int i,j;
for (i=0;i<7;i++)
{System.out.print((i+1)+" ");
for(j=0;j<4;j++)
System.out.print(seats[i][j]+" ");
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.