Homework 8
Due: Nov 30 (109 Dec 1), prior to 11:59:59 PM
In this assignment, you will continue the work you started in Lab and develop various programs that analyze data about flights into and out of Philadelphia International Airport.
This assignment is broken up into five parts; you will create one Java class in each:
- A library class that reads flight information from a data file.
- A program that computes the number of flights that operated on a given day.
- A program that determines which of a specified collection of airports had the most flights to that destination. (This part is Extra Credit for 109; it is required for 113)
- A program that computes average delay in a year for a specific flight.(This part is Extra Credit for 109; it is required for 113)
- A program that computes the percentage of delayed flights that recovered from a departure delay. (That is, what percentage of flights that departed late arrived either early or on-time.) (This part is Extra Credit for 109; it is required for 113.).
In completing this assignment, you will learn to:
- Define a custom class in Java
- Read data from an input file using a Java library
- Work with arrays of objects
- Write a Java program consisting of multiple source code files
Code Understandability and Readability
This statement is repeated for the previous homeworks. It still applies
In addition to writing code that correctly implements the specification, you are also asked to write code that is easy to read and understand.
In particular, part of your score on this assignment will be determined by:
Variable naming: Variables should have meaningful names that indicate what they represent, using full English words or common abbreviations, e.g. "wins" or "votes" instead of "w" or "vot".
Appearance: The code should be formatted so that indentation and spacing make it easy to understand which parts of the code are within the bodies of if-statements and loops. Additionally, there should be spacing between variables and operators to make it easy to read each individual line of code.
Before You Begin
This assignment assumes that you have completed this week's lab and that you have a fully defined Flight class, as well as a AllFlights class. If you have not completed that Lab, be sure to do so right away!
Part 1: Reading flights from file
In this week's lab, you implemented the AllFlights class with a method called readFlights() that reads data and prints it. Start by adjusting the readFlights method so that it creates an array sized to hold the number of flights in the data file. Rather than simply printing data, create a Flight objects and store them in the array. Then return the filled array from readFlights.
Now add another method to the AllFlights class that is also called readFlights and also returns an array of Flight objects, but takes a String as its input. That is, its signature should be:
public static Flight[] readFlights(String file)
The file parameter represents the name of the file that contains the data. Instead of opening a Scanner with System.in open it with a File object. This was described in class (Oct 11).
Note that you will need to wrap the method in a try..catch as opening a file for reading can throw an exception that Java required you to catch. To jog your memory, the method should contain code like the following:
try {
Scanner scnr = new Scanner(new File(filename));
} catch (Exception except) {
System.out.println("Problem: " + except);
}
You will certainly need more code; this does nothing useful.
This version of readFlights() should work exactly the same way as the one you wrote in the Lab. (Also, you need to add some exception handling.)
Complete the above implementation in the FlightReader class. Then modify the main() method as shown below:
public static void main(String[] args) {
Flight[] flights = readFlights(args[0]);
for (int i=0; i<flights.length; i++) {
System.out.println(f[i]);
}
}
Compile your program and then run it; be sure that testFlights.txt is in the same folder/directory as your FlightReader.java
After this change the line to start you program should be:
java FlightReader testFlights.txt
which is only one character different from the line to run the program as written in lab!
Part 2: Number of flights on a specified day
Now that you have a method that can convert an input file to an array of Flight objects, you are ready to start writing some programs that serve different purposes but all rely on the library that reads flight data from a file.
The input data we will use for the remaining parts of the assignment is a file containing all the flights flying into and out of Philadelphia International Airport (PHL) in the year 2008. The data is available at:
/home/gtowell/Public/CS113/HW8/phl-flights.txt
The file contains data for almost 200,000 flights! All remaining programs in this assignment will use this data file.
In this part of the assignment, you are asked to write a program called FlightCounter that determines the number of flights in the data file for a given date. The date is specified as a runtime argument in [month]/[day]/[year] format, e.g. “6/10/2008”.
Your FlightCounter program must use the FlightReader.readFlights() method from Part 1 to get all the Flight objects from the file containing PHL flight data, and should not read the input from the file itself. That is, your FlightCounter program should include code like this:
String filename = "phl-flights.txt”;
Flight[] flights = AllFlights.readFlights(filename);
Then you should determine how many of the Flight objects in the array represent flights on the same date as the one specified by the runtime argument. Hint! Use the getDate() method on the Flight object to check if the dates are the same (being sure to do string comparisons correctly).
To demonstrate that your FlightCounter program works correctly, run your program for the following input dates. Your program output should be as follows:
java FlightCounter 6/10/2008
Number of flights on 6/10/2008: 565
java FlightCounter 7/14/2008
Number of flights on 7/14/2008: 593
java FlightCounter 12/25/2008
Number of flights on 12/25/2008: 462
Be sure to include your program output for these inputs in the Readme that you submit with this assignment!
Part 3: Largest number of flights
In this part of the assignment, you will write a program called MaxFlights that reads one or more airport codes as runtime arguments and then determines which of the airports has the largest number of flights as their destination. (See the examples below.)
As above, your MaxFlights program must use the AllFlights.readFlights() method to get all the Flight objects from the URL containing the PHL flight data, and should not read the input itself.
Then, for each of the airport codes that have been specified as runtime arguments, you should count how many of the Flight objects in the array have that airport code as their destination, and display the one that has the most flights, as well as the number of flights.
You do not have to handle the case in which there are no runtime arguments, but your program should work regardless of the number of runtime arguments (specified airports), as long as there is at least one. Also, you do not have to handle the case in which two destination airports have the same number of flights.
To demonstrate that your MaxFlights program works correctly, run your program for the following inputs. Your program output should be as follows:
java MaxFlights ORD MDW DTW
ORD has the most flights: 6732
java MaxFlights LAX PHX MCO ORD JFK DFW SEA SFO
MCO has the most flights: 6937
Be sure to include your program output for these inputs in the Readme that you submit with this assignment!
Part 4: Average delay for a specified flight number
In this part of the assignment, you will write a program called AverageDelay that computes the average flight arrival delay for a given flight number, which is specified as a runtime argument.
As above, your AverageDelay program must use the AllFlights.readFlights() method to get all the Flight objects from the URL containing the flight data, and should not read the input itself.
Then you should compute the average arrival delay for all Flight objects in the array that have the same flight number as the one specified as the runtime argument. Note that you only need to compute the average arrival delay, and not average departure delay.
The average delay should be displayed as an integer; you should truncate the value when displaying it, e.g. if the average delay is calculated as 5.75, it should be displayed as 5.
If no flights with the specified flight number are found, then the program should indicate that the flight number is not valid.
To demonstrate that your AverageDelay program works correctly, run your program using the large input file for flight numbers 22, 259, 1233, and 1300. Your program output should be as follows:
java AverageDelay 22
Flight 22 has an average arrival delay of 92
java AverageDelay 259
Flight 259 has an average arrival delay of 8
java AverageDelay 1233
Flight 1233 has an average arrival delay of -3
java AverageDelay 1300
1300 is not a valid flight number
Be sure to include your program output for these inputs in the Readme that you submit with this assignment!
EXTRA CREDIT! Percentage of flights that recover from delays from a specified airport
For an additional 6 points (out of 100), write a program named RecoveredFlights that reads an airport code as a runtime argument and displays the percentage of delayed flights originating at that airport that recovered from the delay, as defined below.
For our purposes, we will use the following definitions:
- delayed flight
- one that has a departure delay greater than 0
- recovered flight
a delayed flight that has an arrival delay less than or equal to 0.
That is, a recovered flight is one that was delayed when it departed, but arrived early or on time. Your program should display the result of dividing the number of recovered flights by the number of delayed flights as a percentage.
The percentage should be displayed as a floating point number between 0 and 100 to two digits of precision; your program should round the value when displaying it, e.g. if the percentage is calculated as 85.3275, it should be displayed as 85.33%.
If there were no flights with the specified airport code as their origin and a delay greater than 0, then the program should show a message indicating that there were no delayed flights from that airport.
As with previous parts of this assignment, your RecoveredFlights program must use the AllFlights.readFlights() method from Part 1 to get all the Flight objects from the data file and should not read the input itself.
To demonstrate that your RecoveredFlights program works correctly, run your program for airport codes JFK, DFW, and YYZ. Your program output should be as follows:
java RecoveredFlights JFK
34.94% of delayed flights from JFK recovered
java RecoveredFlights DFW
22.70% of delayed flights from DFW recovered
java RecoveredFlights YYZ
There were no delayed flights from YYZ
Be sure to include your program output for these inputs in the Readme that you submit with this assignment!
Submitting
Create a readme
Use VSC to create another file in your HW8 directory. This file should be named "Readme". The contents of this file should follow this sample Be sure that the Readme has, in addition to the usual stuff, the program outputs as requested above.
You should have 6 or 7 files in your HW8 directory: Flight.java, AllFlights.java, FlightReader.java (from the lab associated with this homework), AverageDelay.java, MaxFlights.java, FlightCounter.java, and Readme.
Submit
If you did this work on your own computer
You will first need to copy the files from you own computer to a lab computer. To do so, you can go into the lab as with HW2 or use ssh. Either way, you will need to create
HW6 directory within you CS113 directory on the Unix machines. Recall, the can be done with the following commands:
cd
cd CS113
mkdir HW8
Once you have made the HW8 directory in Unix, open a terminal on you own computer and in that terminal use "cd" to navigate to the directory containing your work for this assignment. Assuming you use the same directory structure on your own computer and in the lab, this process can be accomplished with the following commands
cd
cd CS113
cd HW8
Then use the scp command to copy each of the files you want to submit from your computer to the lab. For example:
scp Readme UNIX_NAME@goldengate.cs.brynmawr.edu:CS113/HW8/Readme
As always, when you read "UNIX_NAME" put in your UNIX user name. Also, with each scp command you will need to enter your UNIX password.
Actually submit
Open a terminal in UNIX (again, you can use SSH to do so from your laptop) and execute the following Unix commands (assuming you put HW8 directory into a CS113 directory in your home directory).
cd
cd CS113
/home/gtowell/bin/submit -c 113 -d HW8 -p 8
In response to the submit command you should see a series of messages ending with:
Submitting archive...
Submission complete! Submission timestamp is 2023-08-08-15-30-28-EDT.