Question: Lab---searching Generics Create a class called Flight that represents a commercial flight. It should contain strings to represent the source city, destination city, and airline.
Lab---searching Generics
Create a class called Flight that represents a commercial flight. It should contain strings to represent the source city, destination city, and airline. It should also contain GregorianCalendar objects to represent departure and arrival times. Finally, it should implement the Comparable interface. Two flights should be considered equal if they have the same source and destination cities. If they are not equal, the comparison should be made based on the source cities alphabetically.
Make sure to write a toString() method to output the flight information. To output the strings, simply use their own toString() methods. The GregorianCalendar toString() method returns far more information that you will need so use the getTime() method instead. This method returns a Date object that has a toString() method that is much nicer than GregorianCalendars.
I have given you a driver called FlightDriver to test your class. This driver creates several Flight objects in an array. Create two additional Flight objects (target1 and target2) and then search for them using a linearSearch. Object target1 should be a flight in the array, whereas target2 should be a flight not in the array. Display a message indicating whether the flight was found or not.
Next, sort the array and search for target1 and target2 using a binarySearch. Display a message indicating whether the flight was found or not.
import java.util.GregorianCalendar;
public class FlightDriver {
public static void main(String[] args)
{
Flight[] flights =
{
new Flight("Philadelphia", "Las Vegas", "Southwest",
new GregorianCalendar(2007, 1, 12, 5, 0, 0),
new GregorianCalendar(2007, 1, 12, 10, 0, 0)),
new Flight("Trenton", "Blacksburg", "Trans-National Air",
new GregorianCalendar(2007, 7, 11, 13, 30, 0),
new GregorianCalendar(2007, 8, 11, 14, 30, 0)),
new Flight("Fairbanks", "Scotsdale", "Delta",
new GregorianCalendar(2007, 4, 4, 4, 0, 0),
new GregorianCalendar(2007, 4, 5, 0, 0, 0)),
new Flight("Reno", "Hartford", "American Airlines",
new GregorianCalendar(2008, 2, 24, 20, 0, 0),
new GregorianCalendar(2008, 2, 25, 3, 25, 0)),
new Flight("Ewing", "Houston", "Northwest",
new GregorianCalendar(2007, 4, 1, 13, 0, 0),
new GregorianCalendar(2007, 4, 1, 18, 20, 0))
};
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
