Question

Labs CSIT 210 Introduction to Programming LAB 9B Arrays Change the program AirlineDriver.java to assign passenger seats in an airplane. Chesapeake Airlines is a small commuter airline with seats for...

Labs
CSIT 210 Introduction to Programming
LAB 9B Arrays
Change the program AirlineDriver.java to assign passenger seats in an airplane.
Chesapeake Airlines is a small commuter airline with seats for 20 passengers in 5 rows of 4 seats each. Here is the layout:
1ABCD 2ABCD 3ABCD 4ABCD 5ABCD
The user enters the row (1 – 5) and the seat (A – D). The seat entry may be lowercase.
Use this code to enter the seat:
System.out.print(" Enter row and seat "); seat = scan.nextLine();
r = seat.?? c = seat.??
What will you need to do to change r to a valid row index?
What will you need to do to change the character (A, B, C, D) in the variable c to the column index in the array? Check the program example on page 347. I have included the LetterCount program in the Chapter Seven Activities.
The program checks the array to see if the seat is available. An X in the array indicates the seat is not available.
If the seat is available, assign an X to that position in the array. If it’s unavailable, display a message to the passenger. Use the displaySeats method after each row/seat entry.
After some seats are assigned, the layout may look like this:
1XBCX 2ABCD 3AXCX 4ABCD 5ABCD
1

Labs
CSIT 210 Introduction to Programming
Continue processing requests until the user enters -1 for seat.
2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA PROGRAM:

package test.airline;

import java.util.Scanner;

public class AirlineDriver {
   private static final int ROW_COUNT = 5;
   private static final int SEAT_COUNT_PER_ROW = 4;
   private String[][] passengerSeats = null;
  
   //INITILIZES THE COBSTRUCTOR WITH ARRAY
   public AirlineDriver(){
       passengerSeats = new String[ROW_COUNT][SEAT_COUNT_PER_ROW];
      
       for(int i = 0; i< ROW_COUNT; i++){
           passengerSeats[i] = new String[]{"A","B","C","D"};
       }
   }

   public static void main(String[] args){
       AirlineDriver ad = new AirlineDriver();
       String seat = null;
       String row = null;
       Scanner scan = new Scanner(System.in);
       boolean isContinue = true;
      
       while(isContinue){ //CONTINUE LOOPING
           System.out.println("Enter row and seat"); // ENTER ROW AND SEAT NUMBER FROM USER
           row = scan.next();
           seat = scan.next();
          
           int r = Integer.parseInt(row) -1; // User will enter between 1 to 5
           //however to make it proper array index I have subtracted 1 from it to make it zero based
           String c = null;
           if(!seat.equals("-1")){//if user enters -1 for the seat the while lop will terminate
               if(seat.length()==1 && Character.isLowerCase(seat.charAt(0))){
                   c = seat.toUpperCase();
               }
              
               boolean isAvailable = false;
               for(int i = 0 ; i < SEAT_COUNT_PER_ROW; i++){
                   if(!ad.passengerSeats[r][i].equals("X") && c.equals(ad.passengerSeats[r][i])){ //the seat is available and matches with user requested seat
                       ad.passengerSeats[r][i] = "X";//mark the seat as occupied with X
                       isAvailable = true;
                       break;//break out of the loop
                   }
               }
              
               if(!isAvailable){ //if not available show message
                   System.out.println("The seat is not available right now.");
               }
              
               ad.display(ad.passengerSeats);// display passenger seat situation
           }else{
               isContinue = false;
           }
          
          
       }
      
      
   }
  
  
   private void display(String[][] passengersSeat){
       StringBuilder str = new StringBuilder();
       for(int i = 0; i< passengersSeat.length;i++){
           str.append(i+1).append(String.join("",passengersSeat[i]));
           str.append(" ");
       }
      
       System.out.println(str.toString());
   }
  

}

OUTPUT

Enter row and seat
1 a
1XBCD 2ABCD 3ABCD 4ABCD 5ABCD
Enter row and seat
2 b
1XBCD 2AXCD 3ABCD 4ABCD 5ABCD
Enter row and seat
3 c
1XBCD 2AXCD 3ABXD 4ABCD 5ABCD
Enter row and seat
4 d
1XBCD 2AXCD 3ABXD 4ABCX 5ABCD
Enter row and seat
2 b
The seat is not available right now.
1XBCD 2AXCD 3ABXD 4ABCX 5ABCD
Enter row and seat
3 -1

EXPLANATION:

COMMENTS GIVEN within the code.

Add a comment
Know the answer?
Add Answer to:
Labs CSIT 210 Introduction to Programming LAB 9B Arrays Change the program AirlineDriver.java to assign passenger seats in an airplane. Chesapeake Airlines is a small commuter airline with seats for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have to write a C program to assign seats on each flight of the airline’s...

    I have to write a C program to assign seats on each flight of the airline’s only plane (capacity: 40 seats, in 10 rows). For the sake of simplicity, assume that each row has 4 seats labeled A, B, C, D. Your program should display the following menu of alternatives: Please type 1 for "first class" Please type 2 for "business class" Please type 3 for “economy class”. If the person types 1, then your program should assign a seat...

  • 6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you...

    6.15 Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered. Test cases are available in this document. Remember, in addition...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT