CS 109/113 - Computer Science 1

Writing classes

In this lab you will:
  1. Use the Scanner class to read from a file via System.in
  2. Write a class
  3. Write several methods for that class
This lab is critical for this week's homework so be sure you finish it during your lab session, and ask for help if you need it!

For this and other Lab Activities, you are encouraged to collaborate with your classmates, especially when it comes to addressing any issues that come up, error messages that you receive, etc. Because this lab is critical for the homework, be sure you save a complete copy of the work you did.

Tracking Airline Flights

Keeping track of airplane flights is hard. There are a lot of flights, and there is a lot of information about each flight. In this lab you will work with real data concerning flights into or out of Philadelphia airport in 2008. There were almost 200,000 of them.

To handle all of that information through this lab and this week's homework you will develop a class to hold information about a single flight. Then you will put all of that information into an array.

For this lab you will work with a very small subset, 3 flights, of the actual data.

Part 1 -- Define a Flight class

Part 1: Define a Flight class First you will create a class that represents a single flight from one airport to another. This class will be used in the second part of this Lab and throughout this week's homework. Each flight contains information about: In this part of the assignment, implement a Flight class consisting of the following instance variables and methods: Two thoughts / requirements:
  1. All of the instance variables must be private
  2. The toString method must use the getDate method.
Once you have implemented the Flight class as described above, write a main() method for the Flight class that tests the Flight class by creating a new instance and then printing the object, for example:
   public static void main(String[] args) {
       Flight f = new Flight(2020, 11, 23, 113, 3, 8, "PHL", "SFO");
       System.out.println(f);
   }
Compile and run your program. Make sure that the output looks exactly like what you want.

Part 2: Create a flight data reader

Next, you will create another class, FlightReader, that enables other programs to read flight data about several flights. For example, here are the contents of a small data file, which I refer to as testFlights.txt: 3 2020 11 23 113 3 8 PHL SFO 2020 11 24 206 2 -3 ORD PHL 2020 11 25 231 0 4 PHL SEA You can create a file containing this data for yourself or copy it from
    /home/gtowell/Public/CS113/LAB7/testFlights.txt
using the standard cp or scp commands.

The file contains information for three flights. The first line indicates how many flights are recorded in the file. The remaining lines contain the data. Each line contains the following data, separated by a space, in exactly this order:
  1. year
  2. month
  3. day
  4. flight number
  5. departure delay
  6. arrival delay
  7. origin airport
  8. destination airport
Create a class called AllFlights that contains a static method called readFlights() that takes no input parameters. This method should work as follows: When creating Flight objects, the readFlights() method should read the data of the appropriate type using the Scanner instance opened in the first step.

Compile and run your program. Make sure that the output is as you expect.

What to hand in

Use your phone to take a picture of a terminal window showing the program you ran in the last step above (ie showing the formatted output for the testFlights.txt file). Send the picture to gtowell@brynmawr.edu