Homework 3, Part A: Flights

Learning Goals

Task: Flights and Stopovers Flight Class

Implement a class called Flight (here is the Flight javadoc) to represents an airline flight.

Step 1: Instance variables

The following properties represent a Flight object:

  1. the name of the airline,
  2. the flight number,
  3. the flight’s origin city, and
  4. the flights’s destination city.

Step 2: Constructor

Create a constructor for a Flight object. This should take as input all the needed information to create a flight. Think carefully: how many parameters should it have? What types should these parameters be?

Step 3: Instance methods

  1. toString(). As with any other class you write, define this method to provide the means of printing the contents of the object in an informative and concise way. Don’t forget to use this method as you test your code in the main().

  2. isAnAlternative(), a predicate method that takes as input a flight and returns true iff (if and only if) the invoker flight’s origin and destination are the same as the input flight’s origin and destination.

  3. isAStopOver(), a predicate method that takes as input a flight and returns true iff (if and only if) the invoker flight’s destination is the same as the input flight’s origin.

Step 4: Static methods

Create the following methods:

  1. readFlight(), a static method that asks and collects from the user all the information regarding a flight. Using this information, it creates a Flight, and returns it. This method takes as input a Scanner.

  2. Make sure to include a main() method, for testing purposes. As always, make sure you test all the methods you have defined, and to test each method before moving onto the next one.

It is important that you write code in your main() method to test your program thoroughly. In it create a few Flight objects, and test all your methods on them. Save the results of your testing into FlightTesting.txt to be submitted with your source code. You can take a look at the result of our test code for inspiration:

Testing manual entry
New Flight f1 entered:AA123 from BOS to LAX 

Testing constructor
New Flight f2 created:DL55 from LAX to SFO 

Testing constructor
New Flight f3 created:OA1234 from SFO to ATH 

Testing getAirline(f1):(AA)->: AA

Testing getFlightNumber(f1):(123)->: 123

Testing getOrigin(f1):(BOS)->: BOS

Testing getDestination(f1):(LAX)->: LAX

Flights AA123 from BOS to LAX  and DL55 from LAX to SFO  share stopover city (TRUE)->: true

Flights DL55 from LAX to SFO  and AA123 from BOS to LAX  share stopover city (FALS)->: false

Flights DL55 from LAX to SFO  and OA1234 from SFO to ATH  share stopover city (TRUE->): true

Testing setDestination(f1):BOS

Testing setOrigin(f1):SFO

Testing changes to Flight f1:AA123 from SFO to BOS
 
Flights AA123 from SFO to BOS  and DL55 from LAX to SFO  share stopover city (FALSE)->: false

Flights DL55 from LAX to SFO  and AA123 from SFO to BOS  share stopover city (TRUE)->: true

Step 5: Unit-Testing your code (optional)

Use the code below to test your implementation. To do this, create a new class, FlightTest.java, and replace its content with the code below. After compiling all of your code, write click on the class in the project viewer, and select “Test All.” You will be using this code in the next homework, so it’s good to make sure it’s correct!

FlightTest.java:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.Assert;

/**
 * The test class FlightTest.
 *
 * @author  TM
 * @version 2022.09.23
 */
public class FlightTest
{
    private Flight f1, f2, f3;
    
    /**
     * Default constructor for test class FlightTest
     */
    public FlightTest()
    {

    }

    /**
     * Sets up the test fixture.
     *
     * Called before every test case method.
     */
    @BeforeEach
    public void setUp()
    {
        f1 = new Flight("AA", 123, "BOS", "LAX");
        f2 = new Flight("DL", 55, "LAX", "SFO");
        f3 = new Flight("OA", 1234, "SFO", "ATH");

    }

    /**
     * Tears down the test fixture.
     *
     * Called after every test case method.
     */
    @AfterEach
    public void tearDown()
    {
    }
    
    @Test
    public void testConstructor(){
        assertEquals(f1.getAirline(), "AA");
        assertEquals(f1.getFlightNumber(), 123);
        assertEquals(f1.getOrigin(), "BOS");
        assertEquals("LAX", f1.getDestination());
    }

    @Test
    public void testGetAirline(){
        assertEquals(f1.getAirline(), "AA");
        assertEquals(f2.getAirline(), "DL");
        assertEquals(f3.getAirline(), "OA");
    }

    @Test
    public void testGetOrigin()
    {
        assertEquals(f1.getOrigin(), "BOS");
        assertEquals(f2.getOrigin(), "LAX");
        assertEquals(f3.getOrigin(), "SFO");
    }
    
    @Test
    public void testStopOver()
    {
        assertEquals(f1.isAStopOver(f2),true);
        assertEquals(f2.isAStopOver(f1),false);
        assertEquals(f2.isAStopOver(f3),true);
    }
    
    @Test
    public void testSetOrigin(){
        f1.setOrigin("SFO");
        assertEquals(f1.getOrigin(), "SFO");
        assertEquals(f1.isAStopOver(f2),true);
        assertEquals(f2.isAStopOver(f1),true);
    }
    

    @Test
    public void testGetDestination()
    {
        assertEquals("LAX", f1.getDestination());
        assertEquals("SFO", f2.getDestination());
        assertEquals("ATH", f3.getDestination());
    }
}

What to submit


Homework 3, Part B: Point

Goals

Task: Points and Distances

Point Class

Create a class, named Point to represent a point on the Cartesian plane. Study our own Point documentation.

Step 1: Instance Variables

A point in the Cartesian plane has x and y coordinates, both of type double.

Step 2: Constructors

Provide two constructors:

Step 3: toString()

As with any other class you write, define a toString() method to provide the means of printing the contents of the object in an informative and concise way.

Step 4: Getters and Setters

Include appropriate getters and setters. Make sure to check the javadoc to see which getters and setters we expect.

Step 5: Instance Methods

Define the following instance methods:

Notes and Tips:

What to submit:


Submission Checklist