Lab 4: Instance Variables
Due: 10pm Thursday February 22
Purpose: In this assignment, you will learn to use instance variables in a class. You will define and use get/set methods to access and perform operations on these variables. You will also reuse constructs you have learned in previous labs, including reading user input, printing, loops, and methods.
Your main function will create two objects of type AirlineReservations. It will ask the user for the flight code of each flight and the maximum number of passengers the flight can have. The main function will then repeatedly ask the user to choose a flight and an action. You will quit when the user chooses the exit action.
The following sample shows some of the expected behaviors.Airline Tickets Reservation System
Enter the code of the first flight : Flight1
Enter the maximum amount of passengers : 20
Enter the code of the second flight : Flight2
Enter the maximum amount of passengers : 30
== System Starting ==
Please enter the code of the flight : Flight1
Choose Action :
Enter 1 to cancel reservations
Enter 2 to book reservations
Enter 3 for current flight status
Enter 4 to exit
2
Enter number of seats to book :
12
Flight Flight1, Booked seats 12 (max: 20)
Please enter the code of the flight :
Your program must include at least the following methods with
the specified signatures. You may feel free to use any names you want for the data members of the class.
public AirlineReservations() {
//your code and comments here
}
public AirlineReservations(String code, int maxP) {
//your code and comments here
}
public void setCode(String code) {
// your comments and code here
}
public String getCode() {
// your comments and code here
}
public void setMaxPassengers(int maxP) {
// your comments and code here
}
public int getMaxPassengers() {
// your comments and code here
}
public int getPassengers() {
// your comments and code here
}
public void setPassengers(int npass) {
// your comments and code here
}
public void cancelReservations(int nreservations) {
// your comments and code here
}
public void bookReservations(int nreservations) {
// your comments and code here
}
Note that cancelReservations() and bookReservations() should make sure the number of passengers does not drop below zero and does not exceed the maximum allowed number of passengers, respectively. In case this happens, they should print an appropriate message informing the user, and they should not modify the current state of the object.
When the user is asked to select a specific action, if the user enters integer values other that 1-4, the program should print the menu again. You may assume that the user always enters a meaningful string for the flight code, and an integer for the action, and thus need not write this error checking code.
10pts Extra Credit: Restructure the code in your main method so as to print first
the menu of actions. When the user wants to cancel some seats or look
at the status of a flight, then the system asks him to insert the
flight code in order to find the flight on which to operate on. On the
other hand, when the user wants to book some seats, then the program
has to call the bestFlight method, passing to it as arguments the two flight objects and the number of wanted seats.
public static AirlineReservations bestFlight(int nreservations, AirlineReservations flight1, AirlineReservations flight2) {bestFlight returns null if no flight can accommodate the number of reservations the user requests. Otherwise, it returns the flight which currently has the maximum number of seats available and which also can accommodate the reservations. If both flights can do equally well in accomodating the seats, choose randomly one of the two. The code in your main method should handle all cases properly, printing the appropriate error messages whenever needed.
// your comments and code here
}
Tip 1: You may now use the continue or the break statements in your while loops. When the program executes a continue statement in a while or for loop, it skips the rest of the statements in the loop, and starts executing the loop test. When the program executes a break statement in a while or for loop, it skips the rest of the statements in the loop, and starts executing at the first statement after the loop. These constructs reduce the nesting you need for an if-then-else inside the loop. For example, the following two loops implement the same logic, but the first one nests less deeply than the second and is thus easier to read.
while (moreWork) {
if (!stdin.hasNextInt()) {
System.out.println("Please enter an integer");
s = stdin.next();
continue;
}
object.doWork();
}
while (moreWork) {
if (!stdin.hasInt()) {
System.out.println("Please enter an integer");
s = stdin.next();
} else {
object.doWork();
}
}
Submission and Grading:
Always include your names, slip days, and a comment at the top of your file. Here is the template for AirlineReservations.java.
/**
* @author name 1: discussion section time:
* CS account user name:
* Section Unique Number:
* slip days used on this assignment: ??/4
* total slip days used: ??/6
*
* @author name 2: discussion section time:
* CS account user name:
* Section Unique Number:
* slip days used on this assignment: ??/4
* total slip days used: ??/6
*
* On our honor, we followed the pair programming rules of splitting
* keyboard time evenly and 80% or more joint development, and we have
* neither read nor copied code, nor have we shown or given our code to
* others.
*
* @version Date
*
* Extra Credit attempted (Yes/No):
*
* Describe what this program does, including any extra credit attempted.
*
*
*/
Submit your version of AirlineReservations.java using the turnin program . If you need help goto turnin program help.
If you attempt the extra credit, then the AirlineReservations.java should also contain the bestFlight method and the previous version of main() in comments. Also, any other methods you decide to add, in case you decide to attempt the extra credit, should be properly documented.
Half your score will come from "external correctness" which means the output of the program exactly matches the example specification.
The remaining score comes from "internal correctness" which means you used the specified Java constructs correctly, you captured the redundancy and structure correctly, you commented the header and code appropriately, and you followed the basic Java style guidelines.
If you are submitting without a partner, please include your initial partner and the email correspondence with the instructor or with the TA in your header.