QuestionQuestion

Arrays of Objects

We are interested in designing and implementing a class named Flight that represents an airline flight. Assume that the Time class developed in Lab 6 for AlarmClock project is available. (That is, reuse that code.) The code for the Time class is posted along with this assignment on Blackboard. Specifically note the existence of toString, equals and compareTo methods in the Time class.

1. Write code for a fully encapsulated Flight class with the following specification:

a. There are six instance variables – airlineName, flightNumber, originCity, departureTime, destinationCity, and arrivalTime. Use appropriate type. (The flightNumber does not have to be of type int.)

b. Write a constructor that accepts six parameters and initializes all instance variables.

c. Write six getter methods.

d. Write the code for toString method that constructs and returns a one-line description as shown in the following four examples:

JB 045 ROC   6:00 AM JFK 07:21 AM

JB 027 JFK   7:55 AM TPA 10:41 AM

AA 058 ROC   6:30 AM ORD 08:10 AM

AA 057 ORD   8:50 AM ROC 11:30 AM

Note that each item in the above lines is separated from the previous by 2 blank characters. Also remember that the Time class has a toString method to produce string description of the time.

e. Write the code for equals method. This method has one parameter named anotherFlight of type Flight. The method returns true if an only if the Flight object on which the method is invoked has the same airLineName, flightNumber, and originCity as the parameter anotherFlight 

2. Develop a class named FlightTester that has one main method.

Declare four variables flight1, flight2, flight3, and flight4 of type Flight, i.e., they can hold references to Flight objects.

Create four Flight objects as per the above examples and assign the references to flight1, flight2, flight3, and flight4, respectively.

Check if flight3 and flight4 are the same using the equals method and print either the message that says "Same flight" or "Not same flight"

3. Declare a variable named schedule as an array of Flight objects.

Create an array of 256 Flight objects and assign the reference to the variable schedule.

Declare an int variable named numFlights and initialize it to 4

Assign flight1, flight2, flight3, and flight4 to the first four components of schedule.

Use appropriate getter method to print the destination city of the very first flight in the array schedule.

Write code to inspect the first numFlights components of the array schedule and print information only for flights originating at Rochester, NY.

Write code to inspect the first numFlights components of the array schedule and print information only for flights that arrive at Rochester, NY before Noon, i.e., 12:00 PM.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public class Flight {

    private String airlineName;
    private String flightNumber; // The flightNumber does not have to be of type int
    private String originCity;
    private Time departureTime;
    private String destinationCity;
    private Time arrivalTime;

    /**
    * b. Write a constructor that accepts six parameters and initializes all
    * instance variables
    *
    * @param airlineName
    * @param flightNumber
    * @param originCity
    * @param departureTime
    * @param destinationCity
    * @param arrivalTime
    */
    public Flight(String airlineName, String flightNumber, String originCity, Time departureTime, String destinationCity, Time arrivalTime) {
       this.airlineName = airlineName;
       this.flightNumber = flightNumber;
       this.originCity = originCity;
       this.departureTime = departureTime;
       this.destinationCity = destinationCity;
       this.arrivalTime = arrivalTime;
    }

    // c. Write six getter methods.
    public String getAirlineName() {
       return airlineName;
    }

    public String getFlightNumber() {
       return flightNumber;
    }

    public String getOriginCity() {
       return originCity;...

By purchasing this solution you'll be able to access the following files:
Solution.docx.

$42.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats