Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab---searching Generics Create a class called Flight that represents a commerci

ID: 3698593 • Letter: L

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. 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 GregorianCalendar’s.

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))

};

}

}

make sure Flight objects can be sorted as wanted, sorted alphabetically based on the source city or compare based on departure times, so i can choose in departure time mode or chose the alphabetical mode?

Explanation / Answer

package Calenders;

import java.util.Arrays;

import java.util.Collections;

import java.util.Comparator;

import java.util.GregorianCalendar;

import java.util.Scanner;

class Flight implements Comparable<Flight>{

String srcCity,destCity,airLine;

GregorianCalendar depTime,arrTime;

Flight(String srcCity,String destCity,String airLine,GregorianCalendar depTime,GregorianCalendar arrTime)

{

this.airLine=airLine;

this.arrTime=arrTime;

this.srcCity=srcCity;

this.destCity=destCity;

this.depTime=depTime;

}

public String getAirline()

{

return airLine;

}

public String getsrcCity()

{

return srcCity;

}

public String getdestCity()

{

return destCity;

}

public GregorianCalendar getarrTime()

{

return arrTime;

}

public GregorianCalendar getdepTime()

{

return depTime;

}

@Override

public int compareTo(Flight flt) {

// TODO Auto-generated method stub

return this.srcCity.compareTo(flt.getsrcCity());

}

@Override

public boolean equals(Object obj) {

// TODO Auto-generated method stub

// If the object is compared with itself then return true  

if (obj == this) {

return true;

}

/* Check if obj is an instance of Flight or not

"null instanceof [type]" also returns false */

if (!(obj instanceof Flight)) {

return false;

}

// typecast obj to Flight so that we can compare data members

Flight flt = (Flight) obj;

  

return (srcCity.equals(flt.getsrcCity()) && destCity.equals(flt.getdestCity()));

}

@Override

public String toString() {

String fInfo="Flight Info: Source: "+this.srcCity+", Destination: "+this.destCity+

", AirLine: "+this.airLine+", Departure : "+this.depTime.getTime()+", Arrival : "+this.arrTime.getTime();

return fInfo;

}

// This function returns index of element x in arr[]

static int linearSearch(Flight[] arr, int n, Flight x)

{

for (int i = 0; i < n; i++)

{

// Return the index of the element if the element

// is found

if (arr[i].equals(x))

return i;

}

  

// return -1 if the element is not found

return -1;

}

static int binarySearch(Flight arr[], int l, int r, Flight x)

{

if (r>=l)

{

int mid = l + (r - l)/2;

// If the element is present at the

// middle itself

if (arr[mid].equals(x))

return mid;

// If element is smaller than mid, then

// it can only be present in left subarray

if (arr[mid].compareTo(x)>0)

return binarySearch(arr, l, mid-1, x);

// Else the element can only be present

// in right subarray

return binarySearch(arr, mid+1, r, x);

}

// We reach here when element is not present

// in array

return -1;

}

}

//Class to compare Flights by deptimes

class DepTimeCompare implements Comparator<Flight>

{

public int compare(Flight f1, Flight f2)

{

if (f1.getdepTime().compareTo(f2.getdepTime()) < 0 ) return -1;

if (f1.getdepTime().compareTo(f2.getdepTime()) > 0) return 1;

else return 0;

}

}

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))

};

Flight target1=new Flight("Ewing", "Houston", "Northwest",

new GregorianCalendar(2007, 4, 1, 13, 0, 0),

new GregorianCalendar(2007, 4, 1, 18, 20, 0));

Flight target2=new Flight("Lwing", "Vegas", "Southwest",

new GregorianCalendar(2007, 4, 1, 13, 0, 0),

new GregorianCalendar(2007, 4, 1, 20, 20, 0));

System.out.println("Search using Linear search:");

int searchKey=Flight.linearSearch(flights,flights.length,target1);

if (searchKey!=-1) {

System.out.println(target1.getAirline()+" Flight is found at no-"+searchKey);

}

else

{

System.out.println(target1.getAirline()+" Flight is not found");

}

int searchKey1=Flight.linearSearch(flights,flights.length,target2);

if (searchKey1!=-1) {

System.out.println(target2.getAirline()+" Flight is found at no-"+searchKey);

}

else

{

System.out.println(target2.getAirline()+" Flight is not found");

}

System.out.println();

System.out.println("How do you want to sort the flights..choose");

System.out.println("1-alphabetically based on the source city");

System.out.println("2-compare based on departure times");

Scanner in=new Scanner(System.in);

String choice1=in.next();

int choice=0;

try {

choice = Integer.parseInt(choice1);

} catch (Exception e) {

System.out.println(" Invalid choice");

}

switch(choice)

{

case 1: System.out.println(" Sorted by source city");

Collections.sort(Arrays.asList(flights));

break;

case 2: System.out.println(" Sorted by departure time");

DepTimeCompare depCompare=new DepTimeCompare();

Collections.sort(Arrays.asList(flights),depCompare);

break;

default:System.out.println(" Invalid choice");

}

System.out.println("All flights info:");

for (int i = 0; i < flights.length; i++) {

System.out.println(flights[i].toString());

}

System.out.println();

Collections.sort(Arrays.asList(flights));

System.out.println("Search using binary search:");

int searchKey3=Flight.binarySearch(flights,0,flights.length-1,target1);

if (searchKey3!=-1) {

System.out.println(target1.getAirline()+" Flight is found at no-"+searchKey);

}

else

{

System.out.println(target1.getAirline()+" Flight is not found");

}

int searchKey4=Flight.binarySearch(flights,0,flights.length-1,target2);

if (searchKey4!=-1) {

System.out.println(target2.getAirline()+" Flight is found at no-"+searchKey);

}

else

{

System.out.println(target2.getAirline()+" Flight is not found");

}

}

}