Question

JAVA ONLY Design two classes: Flight and Itinerary. The Flight class stores the information about a...

JAVA ONLY

Design two classes: Flight and Itinerary. The Flight class stores the information about a flight with the

following members:

1. A data field named flightNo of the String type with getter method.

2. A data field named departureTime of the GregorianCalendar type with getter and setter methods.

3. A data field named arrivalTime of the GregorianCalendar type with getter and setter methods.

4. A constructor that creates a Flight with the specified number, departureTime, and arrivalTime.

5. A method named getFlightTime() that returns the flight time in minutes.

The Itinerary class stores the information about itinerary with the following members:

1. A data field named flights of the List<Flight> type. The list contains the flights for the itinerary in increasing order

of departureTime.

2. A constructor that creates an Itinerary with the specified fights.

3. A method named getTotalTravelTime() that returns the total travel time in minutes from the depature time of the

first flight to the arrival time of the last flight in the itinerary.

4. A method named getTotalFlightTime() that returns the sum of the flight time for all flights in the itinerary. The

flight time of a flight is defined as the time from the departure time of the flight until its arrival time.

Test with this code:

public static void main(String[] args) {

List<Flight> flights = new ArrayList<>();

flights.add(new Flight("US230",

new GregorianCalendar(2014, 4, 5, 5, 5, 0),

new GregorianCalendar(2014, 4, 5, 6, 15, 0)));

flights.add(new Flight("US235",

new GregorianCalendar(2014, 4, 5, 6, 55, 0),

new GregorianCalendar(2014, 4, 5, 7, 45, 0)));

flights.add(newFlight("US237",

new GregorianCalendar(2014, 4, 5, 9, 35, 0),

new GregorianCalendar(2014, 4, 5, 12, 55, 0)));

Itinerary itinerary = new Itinerary(flights);

System.out.println(itinerary.getTotalTravelTime());

System.out.println(itinerary.getTotalFlightTime());

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

============

import java.util.ArrayList;

import java.util.GregorianCalendar;

import java.util.List;

class Flight {

private String flightNo;

private GregorianCalendar departureTime;

private GregorianCalendar arrivalTime;

public Flight(String flightNo, GregorianCalendar departureTime, GregorianCalendar arrivalTime) {

this.flightNo = flightNo;

this.departureTime = departureTime;

this.arrivalTime = arrivalTime;

}

/**

* @return the departureTime

*/

public GregorianCalendar getDepartureTime() {

return departureTime;

}

/**

* @param departureTime the departureTime to set

*/

public void setDepartureTime(GregorianCalendar departureTime) {

this.departureTime = departureTime;

}

/**

* @return the arrivalTime

*/

public GregorianCalendar getArrivalTime() {

return arrivalTime;

}

/**

* @param arrivalTime the arrivalTime to set

*/

public void setArrivalTime(GregorianCalendar arrivalTime) {

this.arrivalTime = arrivalTime;

}

/**

* @return the flightNo

*/

public String getFlightNo() {

return flightNo;

}

public long getFlightTime() {

return (long)(arrivalTime.getTimeInMillis() - departureTime.getTimeInMillis())/60000;

}

}

class Itinerary {

private List<Flight> flights;

public Itinerary(List<Flight> flights) {

this.flights = flights;

}

public long getTotalTravelTime() {

return (flights.get(flights.size()-1).getArrivalTime().getTimeInMillis() - flights.get(0).getDepartureTime().getTimeInMillis())/60000;

}

public long getTotalFlightTime() {

long flightTime = 0;

for (Flight f : flights) {

flightTime += f.getFlightTime();

}

return flightTime;

}

}

public class Main {

public static void main(String[] args) {

List<Flight> flights = new ArrayList<>();

flights.add(new Flight("US230",

new GregorianCalendar(2014, 4, 5, 5, 5, 0),

new GregorianCalendar(2014, 4, 5, 6, 15, 0)));

flights.add(new Flight("US235",

new GregorianCalendar(2014, 4, 5, 6, 55, 0),

new GregorianCalendar(2014, 4, 5, 7, 45, 0)));

flights.add(new Flight("US237",

new GregorianCalendar(2014, 4, 5, 9, 35, 0),

new GregorianCalendar(2014, 4, 5, 12, 55, 0)));

Itinerary itinerary = new Itinerary(flights);

System.out.println(itinerary.getTotalTravelTime());

System.out.println(itinerary.getTotalFlightTime());

}

}

Add a comment
Know the answer?
Add Answer to:
JAVA ONLY Design two classes: Flight and Itinerary. The Flight class stores the information about a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • create test cases class Flight { private String flightNo; private Calendar departureTime; private Calendar arrivalTime;   ...

    create test cases class Flight { private String flightNo; private Calendar departureTime; private Calendar arrivalTime;    public Flight(String flightNo, Calendar departureTime, Calendar arrivalTime) { this.flightNo = flightNo; this.departureTime = departureTime; this.arrivalTime = arrivalTime; }    public int getFlightTime() { return (int)(arrivalTime.getTimeInMillis() - departureTime.getTimeInMillis()) / (1000 * 60); }    public Calendar getDepartureTime() { return departureTime; } public Calendar getArrivalTime() { return arrivalTime; }    public void setArrivalTime(Calendar arrivalTime) { this.arrivalTime = arrivalTime; } public void setDepartureTime(Calendar departureTime) { this.departureTime =...

  • Use "Eclipse IDE for Java EE Developers" Design a class named MyDate. The class contains: The...

    Use "Eclipse IDE for Java EE Developers" Design a class named MyDate. The class contains: The Date fields year, month, and day that represent a date. month is 0-based, i.e., 0 is for January. A no argument constructor that creates a MyDate object for the current date. A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. A constructor that constructs a MyDate object with the specified year, month, and day....

  • java code Design a class named QuadraticEquation for a quadratic equation with real coefficients ax? +...

    java code Design a class named QuadraticEquation for a quadratic equation with real coefficients ax? + bx + x = 0 where a = 0. The class contains: (a) Private data fields a, b, and c that represent three coefficients. (b) A constructor taking arguments for a, b, and c. (c) Getter and setter methods for each attribute field. (d) A method named getDiscriminant() that returns the discriminant, 62 - 4ac. (e) A method named hasReal Solution that determines If...

  • please write in Java and include the two classes Design a class named Fan (Fan.java) to...

    please write in Java and include the two classes Design a class named Fan (Fan.java) to represent a fan. The class contains: An int field named speed that specifies the speed of the fan. A boolean field named fanStatus that specifies whether the fan is on (default false). A double field named radius that specifies the radius of the fan (default 5). A string field named color that specifies the color of the fan (default blue). A no-arg (default) constructor...

  • Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name

    Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name, flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight objects.

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • (The Account class) Design a class named Account that contains: A private int data field named...

    (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default...

  • This is in Java The Problem: A common task in computing is to take data and...

    This is in Java The Problem: A common task in computing is to take data and apply changes (called transactions) to the data, then saving the updated data. This is the type of program you will write.             You will read a file of flight reservation data to be loaded into an array with a max size of 20. A second file will hold the transactions that will be applied to the data that has been stored in the array....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT