Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

write a java program that does the following: Activity Objectives At the end of this activity, students should be able to: 1. Create their own

write a java program that does the following: image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Activity Objectives At the end of this activity, students should be able to: 1. Create their own Exception class that inherits Java class Exception 2. Use Java Exception Handling mechanisms to throw and catch exceptions 3. Access text files for reading and writing Activity You are asked to write a program that assigns passengers to seats in an airplane. An example of seat numbering is shown in figure 1 for a small airplane (9 rows numbered from 1 to 9, and 8 seats per row labeled 'A' to 'H'). The program displays the seat pattern with an 'X marking the seats already assigned and '.' for free seats. The program should prompt the user to select one of the following operations: reserve a seat, free a seat, or quit the program. The program prompts the user to enter a seat number and reads it as a string, for example "1A" or "70". If the user wants to reserve a seat that has already been reserved, the program should display the message "Seat unavailable" and prompt the user to enter another seat number. If the user wants to free a seat that is not assigned, the program should display the message "Seat already free" and return back to the menu. If the user enters a seat that is not formed by a digit followed by a letter, or a digit not in the range (1-9), or a letter not in the range(A-H), the program should display the message "Invalid Seat number and prompt the user to enter another seat number. ABCDEFGH . . . ) COVOUAWNP Figure 1: Sample airplane seat map Your program should include the following: 1. A class InvalidseatException that inherits Java class Exception. The class must have two constructors, a default constructor and one with a string parameter for the message of the exception. 2. A class Airplane described in the UML diagram below. Airplane -seatMap : charl 2D array of characters holding X' for occupied seats and '' for free seats +Airplane() Creates the array seatMap with size 9 x 8 and initializes all the elements to '.' +Airplane(String filename) Creates the array seatMap with size 9 x 8 and calls the private method readMapFromFile -readMapfromFile(String filename) Private method called by the above constructor to read data from the file and assign it to the elements of the array seatMap. If the file does not exist, all the elements of seatMap are initialized to the character .' -checkSeat(String seat): boolean Private method that returns true if seat is valid or throws an exception of type InvalidseatException otherwise. The method uses regular expressions to check the seat number +reserveSeat(String seat): boolean Returns true if the seat is reserved successfully (character 'X' assigned to seat), false if the seat is already occupied, or throws an InvalidseatException if checkSeat threw an exception +freeSeat(String seat): boolean Returns true if the seat is freed successfully (character: Assigned to seat), false if the seat is already free, or throws an InvalidseatException if checkSeat() threw an exception #saveMap(String filename) : void saves the contents of the array seatMap into the file "filename" +toString(): String Returns the content of the array seatMap the printable format shown in figure 1 checkseat follows the declare rule for the exception InvalidseatException reserveseat() and freeSeat() follow the declare rule for the exception InvalidseatException 3. A class AirplaneSeat Program with a main method to perform the following operations: a. Create an instance myAirplane of the class Airplane using the second constructor and passing the filename "seatsmap.txt" as the argument. b. Display the seat map of myAirplane by printing the return value of the method toString(). c. Display a short menu to select one of the three operations: 1 to reserve a seat, 2 to free a seat, and 3 to quit the program. d. If the user selects the operation 1 or 2, prompt the user to enter a seat number e. In a try block, call the methods reserveSeat() for operation 1 or freeSeat() for operation 2. Display appropriate messages depending on the return value of each method. If the user enters an invalid seat number, the methods reserveSeat and freeSeat() follow the declare rule for the exception thrown by checkSeat() and therefore your program should catch the exception and prompt the user to enter another seat number. f. Repeat steps b to e as long as the user selection is not equal to 3 8. If the user selects operation 3, call the method saveMap) to save the content of the airplane seat map into the file 'seatsmap.txt'. Once your program runs correctly for all the cases (see sample runs below), submit the files InvalidSeatException.java, Airplane.java, and AirplaneSeat Program.java on courseSite. Here are three consecutive sample runs of the program to use as a guide for testing your program. ---- G H Run #1 ----- A B C D E F . . . . . . . Please select an operation: 1: Reserve a seat 2: Free a seat 3: Ouit Enter a seat number (row[ 1-9 column A-H]): Seat 2A successfully reserved. 2A A B C D E F G H Please select an operation: 1: Reserve a seat 2. Free a seat 3. Quit Enter a seat number (Tow[1-9 ]column[ A-H)): 5E Seat SE successfully reserved. A . B . C . D . E . . F . . G . . H . . Uw Please select an operation: 1: Reserve a seat 2: Free a seat 3: Ouit G2 Enter a seat number (row[1-9]column(A-1): Invalid Seat number (row (1-9) and column (A-H). Please try again. A B C D E F G H 4K Enter a seat number (row[ 1-9) and column (A-H]): Invalid Seat number (row (1-9) and column (A-H]). Please try again. A B C D E F G H Enter a seat number (row[ 1-9] and column[ A-H]): 9H Seat 9H successfully reserved. A B C D E F G H JOUWN Please select an operation: 1: Reserve a seat 2: Free a seat 3: Quit Enter a seat number (row[ 1-9]column(A-H]): BC Seat already free. A B C D E F G X . . . . . Please select an operation: 1. Reserve a seat 2: Pree a seat 31 Quit Enter a seat number (row[1-9 ]column[A-H]): 58 Seat successfully freed. C D . E . F . G . H . Please select an operation: 1: Reserve a seat 2: Free a seat 3: Quit --- Run #2 ------ (Note that the seat map at the end of run 1 is restored from the file seatsmap.txt) A B C D E F G H Please select an operation: 1: Reserve a seat 2: Free a seat 3: Quit Enter a seat number (row[ 1-9 ]column A-H]): Seat 8D successfully reserved. A B C D E F G H Please select an operation 1: Reserve a seat 2: Pree seat 31 Quit Enter a seat number (row[1-9]column[A-H]): 4C Seat 4C successfully reserved. A B C D E F G H Please select an operation: 1: Reserve a seat 2: Free a seat 3: Quit Enter a seat number (row[ 1-9 ]column[A-]): 8C Seat 8C successfully reserved. A . B . C . D . E . F . G . H . XX - - Please select an operation: 1: Reserve a seat 2: Free a seat 3: Quit - Run #3 --- (Note the seat map from run 2 is restored from the file seatsmap.txt) A B C D E F G H WWW Please select an operation: 1: Reserve a seat 2: Free a seat 3: Quit . Enter a seat number (row[1-9 ]column[ A-H]): 4C Seat successfully freed. A B C D E F G H can WN . Please select an operation: 1: Reserve a seat 2: Free a seat 3: Quit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions