FlightRoute class represents flight route between departure and destination airp
ID: 3547949 • Letter: F
Question
FlightRoute class represents flight route between departure and destination airports. FlightRoute object contain following properties airline, airline ID, source airport code, source airport city, source departure time, source airport ID, destination airport code, destination airport city, destination arrival time, destination airport ID, code share, stops, and equipment. FlightRoute class implements the Comparable interface. Two flight routes should be considered equal if they have the same source and destination airport code. If they are not equal, the comparison should be made based on the source airport code alphabetically.
An incomplete class FlightRouteDriver is provided to test the FlightRoute class. To complete FlightRouteDriver program, write a bubbleSort() method and linearSearch() method. However, linearSearch method should return a list of Comparable objects that matching the target.
You are provided with FlightRouteData-US.txt file which contains flight route data between airports in United States, for the purpose of testing the FlightRoute class.
Codes:
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Scanner;
public class FlightRouteDriver
{
public static void main(String[] args) throws IOException
{
String routeFile = "FlightRouteData-US.txt";
ArrayList<Comparable> flRouteList = new ArrayList<Comparable>();
BufferedReader br = null;
String line = "";
br = new BufferedReader(new FileReader(routeFile));
while ((line = br.readLine()) != null) {
String[] dataSet = line.split(";");
FlightRoute flRoute = createFlightRoute(dataSet);
flRouteList.add(flRoute);
}
br.close();
System.out.println("Uploaded data file contains " + flRouteList.size() + " flight routes.");
//sorting list of flight route
System.out.println("Bubble Sort Algorithm running ..." );
bubbleSort(flRouteList);
System.out.println("Flight route data sorted..." );
//Getting user input on source and destination airport codes
Scanner userInput = new Scanner(System.in);
System.out.println("Enter Departure Airport code: ");
String departureAirport = userInput.next();
System.out.println("Enter Destination Airport code: ");
String destinationAirport = userInput.next();
userInput.close();
//creating a target flight for search
String[] target = new String[13];
target[0]="";
target[1]="0";
target[2]=departureAirport;
target[3]="";
target[4]="01:00";
target[5]="0";
target[6]=destinationAirport;
target[7]="";
target[8]="01:00";
target[9]="0";
target[10]="N";
target[11]="0";
target[12]="";
FlightRoute targetRoute = createFlightRoute(target);
ArrayList<Comparable> found = linearSearch (flRouteList, targetRoute);
System.out.println(" ***Flight Route search result***");
if(found == null)
System.out.println("Flight route not found.");
else
{
for (int i = 0; i<found.size(); i++)
System.out.println("Flight Routes found. Details are below. " + found.get(i));
}
}//end of main method
// -------------------------------------------------------------------
// Creates a FlightRoute object using specified String array object.
// -------------------------------------------------------------------
private static FlightRoute createFlightRoute(String[] dataSet)
{
String airline = dataSet[0];
int airlineID = Integer.parseInt(dataSet[1]);
String sourceAirportCode = dataSet[2];
String sourceAirportCity = dataSet[3];
Time sourceCityDepartureTime = Time.valueOf(dataSet[4]+":00");
int sourceAirportID = Integer.parseInt(dataSet[5]);
String destinationAirportCode = dataSet[6];
String destinationAirportCity = dataSet[7];
Time destinationCityArrivalTime = Time.valueOf(dataSet[8]+":00");
int destinationAirportID = Integer.parseInt(dataSet[9]);
char codeshare = dataSet[10].charAt(0);
int stops = Integer.parseInt(dataSet[11]);
String equipment = dataSet[12];
FlightRoute flRoute = new FlightRoute(airline,airlineID,sourceAirportCode,
sourceAirportCity,sourceCityDepartureTime, sourceAirportID,
destinationAirportCode,destinationAirportCity, destinationCityArrivalTime,
destinationAirportID,codeshare, stops, equipment );
return flRoute;
}//end of createFlightRoute method
// -------------------------------------------------------------------
// Sorts the specified list of objects using a bubble sort algorithm.
// -------------------------------------------------------------------
private static void bubbleSort(ArrayList<Comparable> flRouteList) {
for(int i=0; i < flRouteList.size();i++) {
for(int j=i+1;j<flRouteList.size(); j++ ) {
if(flRouteList.get(i).compareTo(flRouteList.get(j)) < 0 ) {
Comparable test = flRouteList.get(i);
flRouteList.set(i, flRouteList.get(j));
flRouteList.set(j, test);
}
}
}
}
//end of bubbleSort method
//--------------------------------------------------------------------------
// Searches the specified list of objects using a linear search algorithm.
// Returns a list of resulting objects matching the target.
//--------------------------------------------------------------------------
private static ArrayList<Comparable> linearSearch(
ArrayList<Comparable> flRouteList, FlightRoute targetRoute) {
for(Comparable com:flRouteList) {
if(((FlightRoute)com).compareTo(targetRoute) == 0) {
return flRouteList;
}
}
return null;
}
//end of linearSearch method
}//end of class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.