In this assignment you will practice creating classes and en
In this assignment you will practice creating classes and enumerations in the context of a larger system. Overall, we are building pieces of an airline flight booking system. The classes that we are constructing in this assignment will take care of representing the flight data in Java. The final booking system should be able to searching for flights from a list of commercial airlines (e.g. American, United, or Delta), and locate various routes between two airports. The output is somewhat inspired by Google Flights. Don\'t panic! We go will through this system step by step. For the sake of simplifying the problem, we will make a couple of assumptions: There is only one timezone. All flights occur in the same day. Already provided for you is a file called Driver.java. A \"driver\" is a common term for the main program that manages a set of objects, and manipulates them. It contains the entry point (main) for an entire project. The file you have been provided contains five static methods that can be called from main. These are testing methods that will run sample operations on the enumerations and classes you will be constructing. Initally, they are all commented out. As you implement parts of the assignment, you should uncomment lines in the test methods, in order to see how your program behaves. Also attached is the file sample_output.txt. This file contains the sample output for running this Driver with all of the included tests. You should check the output of your program against the sample output. Plan to test your files one at a time, for example: make sure to test your Airline and Airport enumerations before you start on Plane. Don\'t start working on Plane until those two classes work completely. Time is independent of Airline, Airport, and Plane, but is complicated enough that you\'ll want the experience of implementing those other questions before you started it.
Q1: Create an Airline enumeration. This is a datatype that represents airlines in our system. Include three values: American, United, and Delta. This should be a single file containing the enum declaration. [2 points]
Q2: Create a smart Airport enumeration. This is a datatype that represents airports in our system, and has the ability to change from airport code and city name. Include five values: PHX, LAX, SFO, NRT, and SIN. Also add a static method called getAirportCity. This should be a single file containing the enum declaration, with the method declaration nested inside. (Hint: ExampleSmartEnum contains an example of including a method into an enumeration.) [8 points] getAirportCity(...) will take an Airport enumeration value as a parameter, and return a String containing the city where that airport is located (e.g., \"Phoenix\", \"Los Angeles\", \"Tokyo\", \"San Francisco\"). Use a switch statement. Have a default case that returns \"Unknown City\". Do not implement the switch statement for SIN (Singapore) - we want to test the default case first.
Q3: Create a Plane class. This class will represent a plane in our system. It should contain a constructor, two instance variables (an Airline enumeration, and a String) and three methods (getAirline, getModel, toString) [10 points] The constructor will take an Airline and String and use them to set up the instance variables. getAirline(): will return the Airline of the plane (e.g., Airline.United). getModel(): will return a String containing the model of the plane (e.g., \"Boeing 737\"). toString(): will return the name of the plane\'s airline (e.g., \"United\"), followed by a star (\" * \"), and the model of the plane (e.g., \"Boeing 737\").
Q4: Create a Time class. This class will represent a point in time, such as a departure time. It should contain 2 constructors, 2 instance variables (hour and minute), and 10 methods (see below). All methods but toString should be in terms of the 24 hour format. [30 points] default constructor: Creates a Time object for 12:00AM. overloaded constructor: Creates a Time object at a specific hour and minute. getHour(): Returns an integer representing the hour of the Time object. getMinute(): Returns an integer representing the minute of the Time object. addHours(...): Updates the object by moving it forward a number of hours. addMinute(...): Updates the object by moving it forward a number of minutes. (Hint: Be careful that you don\'t allow minutes to be more than 59.) addTime(...): Updates the object by moving it forward by the hour and minute from another Time object. getCopy(...): Returns a new Time object that has the same hour and minute of the existing Time object. isEarlierThan(...): Returns true if this Time object is earlier than another Time object. isSameTime(...): Returns true if this Time object is the same as another Time object. isLaterThan(...): Returns true if this Time object is later than another Time object. toString(): Returns a string representing the Time object. Uses 12 hour AM/PM format and pads minutes to be two digits. See the sample output for an example.
Q5: Create a Flight class that uses the Plane and Time class. This class will represent a flight between two airports, using a specific Plane, and departing at a specific Time. It should contain a constructor, 7 instance variables (plane, flight number, cost, departure, duration, source, destination), and 9 methods (see below). [25 points] overloaded constructor: Creates a Flight object that is setup up with a Plane, a flight number, a cost, a departure Time, a duration time, a source Airport, and a destination Airport. getPlane(): Returns the Plane that operates this flight. getNumber(): Returns the flight number. getCost(): Returns the flight cost. getDestination(): Returns the destination Airport. getDeparture(): Returns the departure Time. getArrival(): Returns a Time object with the arrival time (computed from the departure time and duration). getSource(): Returns a Airport object for the departure location. toOverviewString(): Returns a String representing an overview of the flight. Use NumberFormat to display the price. See the sample output for an example. toDetailedString(): Returns a String representing the flight\'s detail information. See the sample output for an example.
<driver.java file>
/**
*
* @author (your name), Acuna
*/
public class Driver {
public static void main(String[] args) {
//Please note that these tests are not comprehensive.
//testAirline();
//testAirport();
//testPlane();
//testTime();
//testFlight();
}
public static void testAirline() {
System.out.println(\"==testAirline()==\");
/*
//Test 1: create enum variables
Airline a1 = Airline.American;
Airline a2 = Airline.United;
Airline a3 = Airline.Delta;
Airline a4 = Airline.United;
//Test 2: display and compare variables
System.out.println(\"a1: \" + a1);
System.out.println(\"a2 == a3: \" + (a1 == a2));
System.out.println(\"a2 == a4: \" + (a2 == a4));
*/
}
public static void testAirport() {
System.out.println(\"==testAirport()==\");
/*
//Test 1: create enum variables
Airport a1 = Airport.PHX;
Airport a2 = Airport.LAX;
Airport a3 = Airport.SFO;
Airport a4 = Airport.NRT;
Airport a5 = Airport.SIN;
//Test 2: display and compare variables
System.out.println(\"a1: \" + a1);
System.out.println(\"a2 == a3: \" + (a1 == a2));
System.out.println(\"a2 == a4: \" + (a2 == a4));
System.out.println(\"a1: \" + Airport.getAirportCity(a1));
System.out.println(\"a3: \" + Airport.getAirportCity(a3));
System.out.println(\"a5: \" + Airport.getAirportCity(a5));
*/
}
public static void testPlane() {
System.out.println(\"==testPlane()==\");
/*
//Test 1: create plane objects
Plane p1 = new Plane(Airline.Delta, \"Boeing 717\");
Plane p2 = new Plane(Airline.United, \"Airbus A321\");
//Test 2: display plane information.
System.out.println(p1.getAirline());
System.out.println(p1.getModel());
System.out.println(p1);
System.out.println(p2);
*/
}
public static void testTime() {
System.out.println(\"==testTime()==\");
/*
//Test 1: use default constructor.
Time t1 = new Time();
//Test 2: use overloaded constructor.
Time t2 = new Time(9, 0);
Time t3 = new Time(1, 15);
//Test 3: use clone operation.
Time t4 = t3.getCopy();
//Test 4: run toString on AM times.
System.out.println(new Time(0, 5));
System.out.println(new Time(1, 15));
System.out.println(new Time(2, 45));
System.out.println(new Time(10, 5));
System.out.println(new Time(11, 15));
//Test 5: run toString on PM times.
System.out.println(new Time(12, 45));
System.out.println(new Time(13, 5));
System.out.println(new Time(22, 15));
System.out.println(new Time(23, 45));
//Test 6: run toString on object from default constructor.
System.out.println(\"t1: \" + t1);
//Test 7: testing addTime operation
System.out.println(\"t2: \" + t2);
t2.addTime(t3);
System.out.println(\"t2: \" + t2);
//Test 8: testing addMinutes operation
t2.addMinutes(181);
System.out.println(\"t2: \" + t2);
//Test 9: testing8 addHours operation
t2.addHours(2);
System.out.println(\"t2: \" + t2);
//Test 10: testing cloned copy.
t4.addHours(1);
System.out.println(\"t3: \" + t3);//original
System.out.println(\"t4: \" + t4);//clone
//Test 11: testing isEarlierThan.
System.out.println(\"t3 < t4: \" + t3.isEarlierThan(t4));
System.out.println(\"t4 < t3: \" + t4.isEarlierThan(t3));
System.out.println(\"t2 < t4: \" + t2.isEarlierThan(t4));
System.out.println(\"t4 < t2: \" + t4.isEarlierThan(t2));
System.out.println(\"t2 < t2: \" + t2.isEarlierThan(t2));
//Test 12: testing isLaterThan.
System.out.println(\"t2 > t4: \" + t2.isLaterThan(t4));
System.out.println(\"t4 > t2: \" + t4.isLaterThan(t2));
System.out.println(\"t4 > t4: \" + t4.isLaterThan(t4));
//Test 13: testing isSameTime.
System.out.println(\"t2 = t4: \" + t2.isSameTime(t4));
System.out.println(\"t4 = t4: \" + t4.isSameTime(t4));
System.out.println(\"t4 = 2:15AM: \" + t4.isSameTime(new Time(2, 15)));
*/
}
public static void testFlight() {
System.out.println(\"==testFlight()==\");
/*
//Test 1: create flights using different settings
Flight f1 = new Flight(new Plane(Airline.American, \"Airbus A321\"),
\"495\",
79,
new Time(11,5), 100,
Airport.PHX, Airport.LAX);
Flight f2 = new Flight(new Plane(Airline.Delta, \"Boeing 717\"),
\"1063\",
79,
new Time(7, 10),
95,
Airport.PHX,
Airport.LAX);
Flight f3 = new Flight(new Plane(Airline.American, \"Airbus A321\"),
\"400\",
44,
new Time(21, 25),
127,
Airport.PHX,
Airport.SFO);
Flight f4 = new Flight(new Plane(Airline.United, \"Boeing 787\"),
\"400\",
525,
new Time(10, 50),
715,
Airport.LAX,
Airport.NRT);
Flight f5 = new Flight(new Plane(Airline.United, \"Boeing 737\"),
\"414\",
59,
new Time(6, 50),
85,
Airport.LAX,
Airport.SFO);
System.out.println(f1.toDetailedString());
System.out.println();
System.out.println(f1.toOverviewString());
System.out.println();
System.out.println();
System.out.println(f5.toDetailedString());
System.out.println();
System.out.println(f5.toOverviewString());
*/
}
}
Solution
Please provide the sample_output.txt. This is required to implement toDetailedString() and toOverviewString() method.
public enum Airline {
American, United, Delta
}
public enum Airport {
PHX, LAX, SFO, NRT, SIN;
/**
* Returns the city corresponding to the city code
* @return
*/
public static String getAirportCity(Airport airport) {
switch(airport) {
case PHX:
return \"Phoenix\";
case LAX:
return \"Los Angeles\";
case SFO:
return \"San Francisco\";
case NRT:
return \"Tokyo\";
default:
return \"Unknown City\";
}
}
}
public class Plane {
//Instance variables
private Airline airline;
private String model;
/**
* Parameterized constructor
* @param airline
* @param model
*/
public Plane(Airline airline, String model) {
this.airline = airline;
this.model = model;
}
/**
* @return the airline
*/
public Airline getAirline() {
return airline;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
@Override
public String toString() {
return getAirline() + \" * \" + getModel();
}
}
public class Time {
//Instance variables
private int hour;
private int minute;
/**
* Default constructor
*/
public Time() {
this.hour = 0;
this.minute = 0;
}
/**
* Parameterized constructor
* @param hour
* @param minute
*/
public Time(int hour, int minute) {
this.hour = hour;
this.minute = minute;
}
/**
* @return the hour
*/
public int getHour() {
return hour;
}
/**
* @return the minute
*/
public int getMinute() {
return minute;
}
/**
* Updates the object by moving it forward a number of hours.
* @param hour
*/
public void addHours(int hour) {
this.hour += hour;
}
/**
* Updates the object by moving it forward a number of minutes.
* @param minute
*/
public void addMinute(int minute) {
this.minute += minute;
while (this.minute > 60) {
this.minute -= 60;
this.hour += 1;
}
}
/**
* Updates the object by moving it forward by the hour and minute from another Time object.
*/
public void addTime(Time another) {
addMinute(another.getMinute());
addHours(another.getHour());
}
/**
* Returns a new Time object that has the same hour and minute of the existing Time object.
*/
public Time getCopy() {
Time time = new Time();
time.hour = this.hour;
time.minute = this.minute;
return time;
}
/**
* Returns true if this Time object is earlier than another Time object.
*/
public boolean isEarlierThan(Time another) {
if(this.hour < another.hour)
return true;
else if((this.hour == another.hour) && (this.minute < another.minute))
return true;
return false;
}
/**
* Returns true if this Time object is the same as another Time object.
*/
public boolean isSameTime(Time another) {
if((this == another) || ((this.hour == another.hour) && (this.minute == another.minute))) {
return true;
}
return false;
}
/**
* Returns true if this Time object is later than another Time object.
*/
public boolean isLaterThan(Time another) {
return (!isEarlierThan(another) && !isSameTime(another));
}
/**
* Returns a string representing the Time object.
* Uses 12 hour AM/PM format and pads minutes to be two digits. See the sample output for an example.
*/
public String toString() {
return ((this.hour > 12) ? (this.hour - 12) : this.hour) + \":\" +
((this.minute < 10) ? (\"0\" + this.minute) : this.minute) +
((this.hour >= 12) ? \" PM\" : \" AM\");
}
}
public class Flight {
//Instance variables
private Plane plane;
private String flightNum;
private int cost;
private Time departure;
private int duration;
private Airport source;
private Airport destination;
/**
* Constructor
* @param plane
* @param flightNum
* @param cost
* @param departure
* @param duration
* @param source
* @param destination
*/
public Flight(Plane plane, String flightNum, int cost, Time departure, int duration, Airport source,
Airport destination) {
this.plane = plane;
this.flightNum = flightNum;
this.cost = cost;
this.departure = departure;
this.duration = duration;
this.source = source;
this.destination = destination;
}
/**
* Returns the Plane that operates this flight.
*/
public Plane getPlane() {
return this.plane;
}
/**
* Returns the flight number.
*/
public String getNumber() {
return this.flightNum;
}
/**
* Returns the flight cost.
*/
public int getCost() {
return this.cost;
}
/**
* Returns the destination Airport.
*/
public String getDestination() {
return Airport.getAirportCity(this.destination);
}
/**
* Returns the departure Time.
*/
public Time getDeparture() {
return this.departure;
}
/**
* Returns a Time object with the arrival time (computed from the departure time and duration).
*/
public Time getArrival() {
Time time = new Time();
time.addMinute(this.duration);
return time;
}
/**
* Returns a Airport object for the departure location.
*/
public String getSource() {
return Airport.getAirportCity(this.source);
}
/**
* Returns a String representing an overview of the flight.
* Use NumberFormat to display the price.
*/
public String toOverviewString() {
return \"\";
}
/**
* Returns a String representing the flight\'s detail information
* @return
*/
public String toDetailedString() {
return \"\";
}
}
/**
*
* @author (your name), Acuna
*/
public class Driver {
public static void main(String[] args) {
//Please note that these tests are not comprehensive.
testAirline();
testAirport();
testPlane();
testTime();
testFlight();
}
public static void testAirline() {
System.out.println(\"==testAirline()==\");
//Test 1: create enum variables
Airline a1 = Airline.American;
Airline a2 = Airline.United;
Airline a3 = Airline.Delta;
Airline a4 = Airline.United;
//Test 2: display and compare variables
System.out.println(\"a1: \" + a1);
System.out.println(\"a2 == a3: \" + (a1 == a2));
System.out.println(\"a2 == a4: \" + (a2 == a4));
}
public static void testAirport() {
System.out.println(\"==testAirport()==\");
//Test 1: create enum variables
Airport a1 = Airport.PHX;
Airport a2 = Airport.LAX;
Airport a3 = Airport.SFO;
Airport a4 = Airport.NRT;
Airport a5 = Airport.SIN;
//Test 2: display and compare variables
System.out.println(\"a1: \" + a1);
System.out.println(\"a2 == a3: \" + (a1 == a2));
System.out.println(\"a2 == a4: \" + (a2 == a4));
System.out.println(\"a1: \" + Airport.getAirportCity(a1));
System.out.println(\"a3: \" + Airport.getAirportCity(a3));
System.out.println(\"a5: \" + Airport.getAirportCity(a5));
}
public static void testPlane() {
System.out.println(\"==testPlane()==\");
//Test 1: create plane objects
Plane p1 = new Plane(Airline.Delta, \"Boeing 717\");
Plane p2 = new Plane(Airline.United, \"Airbus A321\");
//Test 2: display plane information.
System.out.println(p1.getAirline());
System.out.println(p1.getModel());
System.out.println(p1);
System.out.println(p2);
}
public static void testTime() {
System.out.println(\"==testTime()==\");
//Test 1: use default constructor.
Time t1 = new Time();
//Test 2: use overloaded constructor.
Time t2 = new Time(9, 0);
Time t3 = new Time(1, 15);
//Test 3: use clone operation.
Time t4 = t3.getCopy();
//Test 4: run toString on AM times.
System.out.println(new Time(0, 5));
System.out.println(new Time(1, 15));
System.out.println(new Time(2, 45));
System.out.println(new Time(10, 5));
System.out.println(new Time(11, 15));
//Test 5: run toString on PM times.
System.out.println(new Time(12, 45));
System.out.println(new Time(13, 5));
System.out.println(new Time(22, 15));
System.out.println(new Time(23, 45));
//Test 6: run toString on object from default constructor.
System.out.println(\"t1: \" + t1);
//Test 7: testing addTime operation
System.out.println(\"t2: \" + t2);
t2.addTime(t3);
System.out.println(\"t2: \" + t2);
//Test 8: testing addMinutes operation
t2.addMinute(181);
System.out.println(\"t2: \" + t2);
//Test 9: testing8 addHours operation
t2.addHours(2);
System.out.println(\"t2: \" + t2);
//Test 10: testing cloned copy.
t4.addHours(1);
System.out.println(\"t3: \" + t3);//original
System.out.println(\"t4: \" + t4);//clone
//Test 11: testing isEarlierThan.
System.out.println(\"t3 < t4: \" + t3.isEarlierThan(t4));
System.out.println(\"t4 < t3: \" + t4.isEarlierThan(t3));
System.out.println(\"t2 < t4: \" + t2.isEarlierThan(t4));
System.out.println(\"t4 < t2: \" + t4.isEarlierThan(t2));
System.out.println(\"t2 < t2: \" + t2.isEarlierThan(t2));
//Test 12: testing isLaterThan.
System.out.println(\"t2 > t4: \" + t2.isLaterThan(t4));
System.out.println(\"t4 > t2: \" + t4.isLaterThan(t2));
System.out.println(\"t4 > t4: \" + t4.isLaterThan(t4));
//Test 13: testing isSameTime.
System.out.println(\"t2 = t4: \" + t2.isSameTime(t4));
System.out.println(\"t4 = t4: \" + t4.isSameTime(t4));
System.out.println(\"t4 = 2:15AM: \" + t4.isSameTime(new Time(2, 15)));
}
public static void testFlight() {
System.out.println(\"==testFlight()==\");
//Test 1: create flights using different settings
Flight f1 = new Flight(new Plane(Airline.American, \"Airbus A321\"),
\"495\",
79,
new Time(11,5), 100,
Airport.PHX, Airport.LAX);
Flight f2 = new Flight(new Plane(Airline.Delta, \"Boeing 717\"),
\"1063\",
79,
new Time(7, 10),
95,
Airport.PHX,
Airport.LAX);
Flight f3 = new Flight(new Plane(Airline.American, \"Airbus A321\"),
\"400\",
44,
new Time(21, 25),
127,
Airport.PHX,
Airport.SFO);
Flight f4 = new Flight(new Plane(Airline.United, \"Boeing 787\"),
\"400\",
525,
new Time(10, 50),
715,
Airport.LAX,
Airport.NRT);
Flight f5 = new Flight(new Plane(Airline.United, \"Boeing 737\"),
\"414\",
59,
new Time(6, 50),
85,
Airport.LAX,
Airport.SFO);
System.out.println(f1.toDetailedString());
System.out.println();
System.out.println(f1.toOverviewString());
System.out.println();
System.out.println();
System.out.println(f5.toDetailedString());
System.out.println();
System.out.println(f5.toOverviewString());
}
}
SAMPLE OUTPUT:
==testAirline()==
a1: American
a2 == a3: false
a2 == a4: true
==testAirport()==
a1: PHX
a2 == a3: false
a2 == a4: false
a1: Phoenix
a3: San Francisco
a5: Unknown City
==testPlane()==
Delta
Boeing 717
Delta * Boeing 717
United * Airbus A321
==testTime()==
0:05 AM
1:15 AM
2:45 AM
10:05 AM
11:15 AM
12:45 PM
1:05 PM
10:15 PM
11:45 PM
t1: 0:00 AM
t2: 9:00 AM
t2: 10:15 AM
t2: 1:16 PM
t2: 3:16 PM
t3: 1:15 AM
t4: 2:15 AM
t3 < t4: true
t4 < t3: false
t2 < t4: false
t4 < t2: true
t2 < t2: false
t2 > t4: true
t4 > t2: false
t4 > t4: false
t2 = t4: false
t4 = t4: true
t4 = 2:15AM: true
==testFlight()==












