Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming 5th edition chapter 9 two dimensional arrays Write a program th

ID: 3819917 • Letter: J

Question

Java programming 5th edition chapter 9 two dimensional arrays

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)

This is what I have so far which isn't much. I'm at a complete loss

import java.util.*;

public class AssignSeats
{
static Scanner console = new Scanner(System.in);

public static void main(String[]args)
{
//Declare 2D Arrays
int rows = 13;
int seatCol = 6;
int matrix = new int[rows][seatCol];
  
  
  
}//end main
}//end class

Explanation / Answer

Please find code and sample output below. Code comments will be helpful.

import java.util.Scanner;

/**

* This class assign seats requested by user and provides option to book more

* seats.

*

* @author arunveersingh

*

*/

public class AssignSeats {

   public static void main(String[] args) {

       Plane plane = new Plane();

       plane.renderPlaneLayout();

       // Scanner to read the user inputs

       Scanner scanner = new Scanner(System.in);

       System.out.println("Which seat you want to book?");

       while (true) {

           String requestedSeat = scanner.nextLine();

           // Input received from scanner is converted to char Array so

           // alphabetical and numeric portion can be separated out. E.g. A1 -

           // Arrays do not understand A1, so we not break into [0][0]. See

           // below code to see what's exactly happening

           char[] charArray = requestedSeat.toCharArray();

           int column = 20;

           int row = 20;

           try {

               if (charArray[0] == 'A') {

                   column = Integer.parseInt(requestedSeat.replace("A", "")) - 1;

                   row = 0;

               }

               if (charArray[0] == 'B') {

                   column = Integer.parseInt(requestedSeat.replace("B", "")) - 1;

                   row = 1;

               }

               if (charArray[0] == 'C') {

                   column = Integer.parseInt(requestedSeat.replace("C", "")) - 1;

                   row = 2;

               }

               if (charArray[0] == 'D') {

                   column = Integer.parseInt(requestedSeat.replace("D", "")) - 1;

                   row = 3;

               }

               if (charArray[0] == 'E') {

                   column = Integer.parseInt(requestedSeat.replace("E", "")) - 1;

                   row = 4;

               }

               if (charArray[0] == 'F') {

                   column = Integer.parseInt(requestedSeat.replace("F", "")) - 1;

                   row = 5;

               }

               // If Seat is not available, provide more options to user.

               if (!initiateBookingProcess(plane, plane.getSeat(column, row))) {

                   System.out.println("This seat is not available, please try another seat?");

                   plane.renderPlaneLayout();

               } else {

                   // If seat is booked, inform user about seat type and let

                   // the user book another seat

                   System.out.println("Seat Booked: " + requestedSeat + " "

                           + plane.getSeat(column, row).toString());

                   plane.renderPlaneLayout();

                   System.out.println("Which seat you want to book now?");

               }

           } catch (Exception ex) {

               // If user try to provide invalid input user will be requested

               // to provide correct inputs

               plane.renderPlaneLayout();

               System.out.println("Wrong Input, try with correct input. Which Seat?");

           }

       }

   }

   private static boolean initiateBookingProcess(Plane plane, Seat seat) {

       return plane.bookSeat(seat);

   }

}

/**

* This class denotes a seat.

*

* @author arunveersingh

*

*/

class Seat {

   // first, business, economy class

   private String seatClass;

   // * or X

   // Default status is available - *

   private String seatStatus = "*";

   // Window - By default non - window.

   private String seatType = "NOW-WINDOW";

   public Seat(String seatClass, String seatType) {

       setSeatClass(seatClass);

       setSeatType(seatType);

   }

   public String getSeatClass() {

       return seatClass;

   }

   public void setSeatClass(String seatClass) {

       this.seatClass = seatClass;

   }

   /**

   * Overridden so as status of seat can be printed easily

   */

   @Override

   public String toString() {

       return "Seat [seatClass=" + seatClass + ", seatStatus=" + seatStatus + ", seatType="

               + seatType + "]";

   }

   public String getSeatStatus() {

       return seatStatus;

   }

   public void setSeatStatus(String seatStatus) {

       this.seatStatus = seatStatus;

   }

   public String getSeatType() {

       return seatType;

   }

   public void setSeatType(String seatType) {

       if (seatType != null)

           this.seatType = seatType;

   }

}

/**

* This class denotes a plane and which have seats in it.

*

* @author arunveersingh

*

*/

class Plane {

   // Constants

   private static final String WINDOW = "WINDOW";

   private static final String FIRST_CLASS = "FirstClass";

   private static final String BUSINESS_CLASS = "BusinessClass";

   private static final String ECONOMY_CLASS = "EconomyClass";

   // First time initialization of seats and its properties.

   // This is one of the way to initialize an two dimensional array.

   private static Seat[][] seats = {

           { new Seat(FIRST_CLASS, null), new Seat(FIRST_CLASS, null), new Seat(FIRST_CLASS, null),

                   new Seat(FIRST_CLASS, null), new Seat(FIRST_CLASS, null),

                   new Seat(FIRST_CLASS, WINDOW) },

           { new Seat(FIRST_CLASS, null), new Seat(FIRST_CLASS, null), new Seat(FIRST_CLASS, null),

                   new Seat(FIRST_CLASS, null), new Seat(FIRST_CLASS, null),

                   new Seat(FIRST_CLASS, WINDOW) },

           { new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, null),

                   new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, null),

                   new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, WINDOW) },

           { new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, null),

                   new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, null),

                   new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, WINDOW) },

           { new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, null),

                   new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, null),

                   new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, WINDOW) },

           { new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, null),

                   new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, null),

                   new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, WINDOW) },

           { new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, null),

                   new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, null),

                   new Seat(BUSINESS_CLASS, null), new Seat(BUSINESS_CLASS, WINDOW) },

           { new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, WINDOW) },

           { new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, WINDOW) },

           { new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, WINDOW) },

           { new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, WINDOW) },

           { new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, WINDOW) },

           { new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, null),

                   new Seat(ECONOMY_CLASS, null), new Seat(ECONOMY_CLASS, WINDOW) }

   };

   /**

   * This method will render the plane layout with symbols to denote the

   * status of seats using * or X

   */

   public void renderPlaneLayout() {

       System.out.println(setW5() + "A" + setW5() + "B" + setW5() + "C" + setW5() + "D" + setW5()

               + "E" + setW5() + "F");

       for (int i = 0; i < 13; i++) {

           System.out.print(i + 1 + setW5());

           for (int j = 0; j < 6; j++) {

               print(seats[i][j]);

           }

           System.out.println();

       }

   }

   /**

   * This method just creates a space so as renderign looks neat

   *

   * @return

   */

   private String setW5() {

       return " ";

   }

   void print(Seat seat) {

       System.out.print(seat.getSeatStatus() + setW5());

   }

   /**

   * This method change the status of the seat to book if it is not already

   * booked..

   *

   * @param seat

   * @return

   */

   public boolean bookSeat(Seat seat) {

       if (seat.getSeatStatus() != "X") {

           seat.setSeatStatus("X");

           return true;

       }

       return false;

   }

   /**

   * This method returns the seat on the basis of row and column number

   *

   * @param column

   * @param row

   * @return

   */

   Seat getSeat(int column, int row) {

       return seats[column][row];

   }

}

------------------

Output:

A B C D E F

1 * * * * * *

2 * * * * * *

3 * * * * * *

4 * * * * * *

5 * * * * * *

6 * * * * * *

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * * * *

11 * * * * * *

12 * * * * * *

13 * * * * * *

Which seat you want to book?

A1

Seat Booked: A1 Seat [seatClass=FirstClass, seatStatus=X, seatType=NOW-WINDOW]

   A B C D E F

1 X * * * * *

2 * * * * * *

3 * * * * * *

4 * * * * * *

5 * * * * * *

6 * * * * * *

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * * * *

11 * * * * * *

12 * * * * * *

13 * * * * * *

Which seat you want to book now?

A13

Seat Booked: A13 Seat [seatClass=EconomyClass, seatStatus=X, seatType=NOW-WINDOW]

   A B C D E F

1 X * * * * *

2 * * * * * *

3 * * * * * *

4 * * * * * *

5 * * * * * *

6 * * * * * *

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * * * *

11 * * * * * *

12 * * * * * *

13 X * * * * *

Which seat you want to book now?

A14

Wrong Input, try with correct input. Which Seat?

   A B C D E F

1 X * * * * *

2 * * * * * *

3 * * * * * *

4 * * * * * *

5 * * * * * *

6 * * * * * *

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * * * *

11 * * * * * *

12 * * * * * *

13 X * * * * *

F6

Seat Booked: F6 Seat [seatClass=BusinessClass, seatStatus=X, seatType=WINDOW]

   A B C D E F

1 X * * * * *

2 * * * * * *

3 * * * * * *

4 * * * * * *

5 * * * * * *

6 * * * * * X

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * * * *

11 * * * * * *

12 * * * * * *

13 X * * * * *

Which seat you want to book now?

F13

Seat Booked: F13 Seat [seatClass=EconomyClass, seatStatus=X, seatType=WINDOW]

   A B C D E F

1 X * * * * *

2 * * * * * *

3 * * * * * *

4 * * * * * *

5 * * * * * *

6 * * * * * X

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * * * *

11 * * * * * *

12 * * * * * *

13 X * * * * X

Which seat you want to book now?

F14

Wrong Input, try with correct input. Which Seat?

   A B C D E F

1 X * * * * *

2 * * * * * *

3 * * * * * *

4 * * * * * *

5 * * * * * *

6 * * * * * X

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * * * *

11 * * * * * *

12 * * * * * *

13 X * * * * X

A1

This seat is not available, please try another seat?

   A B C D E F

1 X * * * * *

2 * * * * * *

3 * * * * * *

4 * * * * * *

5 * * * * * *

6 * * * * * X

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * * * *

11 * * * * * *

12 * * * * * *

13 X * * * * X

C3

Seat Booked: C3 Seat [seatClass=BusinessClass, seatStatus=X, seatType=NOW-WINDOW]

   A B C D E F

1 X * * * * *

2 * * * * * *

3 * * X * * *

4 * * * * * *

5 * * * * * *

6 * * * * * X

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * * * *

11 * * * * * *

12 * * * * * *

13 X * * * * X

Which seat you want to book now?

Wrong Input, try with correct input. Which Seat?

   A B C D E F

1 X * * * * *

2 * * * * * *

3 * * X * * *

4 * * * * * *

5 * * * * * *

6 * * * * * X

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * * * *

11 * * * * * *

12 * * * * * *

13 X * * * * X

F5

Seat Booked: F5 Seat [seatClass=BusinessClass, seatStatus=X, seatType=WINDOW]

   A B C D E F

1 X * * * * *

2 * * * * * *

3 * * X * * *

4 * * * * * *

5 * * * * * X

6 * * * * * X

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * * * *

11 * * * * * *

12 * * * * * *

13 X * * * * X

Which seat you want to book now?

D10

Seat Booked: D10 Seat [seatClass=EconomyClass, seatStatus=X, seatType=NOW-WINDOW]

   A B C D E F

1 X * * * * *

2 * * * * * *

3 * * X * * *

4 * * * * * *

5 * * * * * X

6 * * * * * X

7 * * * * * *

8 * * * * * *

9 * * * * * *

10 * * * X * *

11 * * * * * *

12 * * * * * *

13 X * * * * X

Which seat you want to book now?

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote