Class Flight


public class Flight extends Object
Flight.java
Implements a class named Flight that represents an airline flight. It contains instance data that represent the name of the airline, the flight number, the flights origin and destination cities. A static method readFlight() is used to get values from the keyboard.
In addition to the getters and setters, the class provides a String representation of Flight object.
The main shows the use of a driver/testing program
Version:
Sept 10, 2022 Feb 2024: SK added isAnAlternative(). Changed isAStopOver() from static to instance
Author:
CS230 Staff
  • Constructor Details

    • Flight

      public Flight(String airline, int flNum, String from, String to)
      Creates Flight objects, given their characteristics
      Parameters:
      airline - The name of the airline
      flNum - The flight number
      from - The city of origin
      to - The city of destination
  • Method Details

    • getAirline

      public String getAirline()
      returns this flight's for airline
      Returns:
      The airline name
    • setAirline

      public void setAirline(String airline)
      Sets this flight's airline
      Parameters:
      airline - The updated name of the airline for this flight
    • getFlightNumber

      public int getFlightNumber()
      Getter for flightNum of this flight
      Returns:
      The flight number
    • setFlightNumber

      public void setFlightNumber(int flNum)
      Setter for flightNum of this flight
      Parameters:
      flNum - The number for this flight
    • getOrigin

      public String getOrigin()
      returns the origin of this flight
      Returns:
      The origin city of the flight
    • setOrigin

      public void setOrigin(String fromCity)
      Sets the origin of this flight to the input one
      Parameters:
      fromCity - The origin city of the flight
    • getDestination

      public String getDestination()
      Gets the destination of this flight
      Returns:
      The destination city of this flight
    • setDestination

      public void setDestination(String toCity)
      Sets the destination of this flight
      Parameters:
      toCity - The destination city of this flight
    • isAStopOver

      public boolean isAStopOver(Flight f)
      Checks whether the destination of this flight is the same as origin of the input one. It returns true if this is the case, false otherwise
      Parameters:
      f - the flight to check its origine
      Returns:
      true if this flight's destination is the same as the origin of the input flight.
    • isAnAlternative

      public boolean isAnAlternative(Flight other)
      Checks whether the invoking flight and the input one are alternatives, ie the have the same origin and the same destination. Returns true if so, false otherwise.
      Parameters:
      f - the flight to check its origine and destination for being equal to the ones of the invokig flight
      Returns:
      true if this flight's origin and destination are the same as the ones in the input flight.
    • toString

      public String toString()
      Returns a String representation of this flight
      Overrides:
      toString in class Object
      Returns:
      The state of this flight, as a string
    • readFlight

      public static Flight readFlight(Scanner s)
      Reads from the keyboard the properties of a Flight. Returns the Flight object. Used in main(). Notice that this is a "static" method, because it is to be used by the static main().
      Parameters:
      s - the Scanner object to be used for reading user input
      Returns:
      The flight as entered by the user
    • main

      public static void main(String[] args)
      Driver, used to test this class.