Q5 Create a Flight class that uses the Plane and Time class
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. Included below is an overall UML diagram that describes the three classes you will be constructing. It provides a useful summary of all of the methods you are expected to implement, and their corresponding types and visibility. Notice that one private method is listed here (formatDigits in Time) that isn\'t mentioned above. This is a method that was in our solution, you may not need it in your answer. Conversely, you may implement additional private methods that you find useful.
Solution
//Plane.java
public class Plane {
private String name;
//Constructor with name
public Plane(String name) {
this.name=name;
}
//Returns name
public String getName(){
return name;
}
}
------------------------------------------------------------------------------
//Flight.java
import java.sql.Time;
import java.text.NumberFormat;
public class Flight {
private Plane plane;
private int flightNumber;
private double cost;
private Time departure;
private long duration;
private String source;
private String destination;
//Constructor with 7 arguments
public Flight(Plane plane,
int flightNumber,
double cost,
Time departure,
long duration,
String source,
String destination) {
this.plane=plane;
this.flightNumber=flightNumber;
this.cost=cost;
this.departure=departure;
this.duration=duration;
this.source=source;
this.destination=destination;
}
// Returns the Plane that operates this flight.
public String getPlane() {
return plane.getName();
}
//: Returns the flight number.
public int getNumber(){
return flightNumber;
}
//: Returns the flight cost.
public double getCost(){
return cost;
}
//: Returns the destination Airport.
public String getDestination(){
return destination;
}
//: Returns the departure Time.
public Time getDeparture(){
return departure;
}
//: Returns a Time object with the arrival time (computed from the departure time and duration).
public Time getArrival() {
long arrivalTime=getDeparture().getTime()+departure.getTime();
return new Time(arrivalTime);
}
//: Returns a Airport object for the departure location.
public String getSource() {
return source;
}
//: Returns a String representing an overview of the flight.
public String toOverviewString(){
String flightDesc=\"\";
flightDesc+=\"Plane Name : \"+plane.getName()+
\"\ Flight Number :\"+flightNumber+
\"\ Cost = \"+cost+
\"\ Departure = \"+departure+
\"\ Duration = \"+duration+
\"\ Source = \"+source+
\"\ Destination = \"+destination;
return flightDesc;
}
//Use NumberFormat to display the price. See the sample output for an example.
//Returns a String representing the flight\'s detail information.
public String toDetailedString(){
NumberFormat nf=NumberFormat.getCurrencyInstance();
String flightDesc=\"\";
flightDesc+=\"Plane Name : \"+plane.getName()+
\"\ Flight Number :\"+flightNumber+
\"\ Cost = \"+nf.format(cost)+
\"\ Departure = \"+departure+
\"\ Duration = \"+duration+
\"\ Source = \"+source+
\"\ Destination = \"+destination;
return flightDesc;
}
}
------------------------------------------------------------------------------
//FlightTester.java
import java.sql.Time;
public class FlightTester {
public static void main(String[] args) {
//Create an instance of Flight class
Flight flight=new Flight(new Plane(\"Boeing\"),
777, 100, new Time(12, 0, 0), 60, \"Boston\", \"Dubai\");
System.out.println(\"Flight Information\");
//print flight object
System.out.println(flight.toDetailedString());
}
}
------------------------------------------------------------------------------
Sample Output:
Flight Information
Plane Name : Boeing
Flight Number :777
Cost = $100.00
Departure = 12:00:00
Duration = 60
Source = Boston
Destination = Dubai


