w3resource

Java: Airplane class to check flight status and delay

Java OOP: Exercise-12 with Solution

Write a Java program to create a class called "Airplane" with a flight number, destination, and departure time attributes, and methods to check flight status and delay.

Sample Solution:

Java Code:

// Airplane.java
import java.time.LocalTime;

public class Airplane {
  private String flightNumber;
  private String destination;
  private LocalTime scheduledDeparture;
  private int delayTime;

  public Airplane(String flightNumber, String destination, LocalTime scheduledDeparture) {
    this.flightNumber = flightNumber;
    this.destination = destination;
    this.scheduledDeparture = scheduledDeparture;
    this.delayTime = 0;
  }

  public String getFlightNumber() {
    return flightNumber;
  }

  public void setFlightNumber(String flightNumber) {
    this.flightNumber = flightNumber;
  }

  public String getDestination() {
    return destination;
  }

  public void setDestination(String destination) {
    this.destination = destination;
  }

  public LocalTime getScheduledDeparture() {
    return scheduledDeparture;
  }

  public void setScheduledDeparture(LocalTime scheduledDeparture) {
    this.scheduledDeparture = scheduledDeparture;
  }

  public int getDelayTime() {
    return delayTime;
  }

  public void delay(int minutes) {
    this.delayTime = minutes;
    this.scheduledDeparture = this.scheduledDeparture.plusMinutes(minutes);
  }

  public void checkStatus() {
    if (delayTime == 0) {
      System.out.println("Flight " + flightNumber + " is on time.");
    } else {
      System.out.println("Flight " + flightNumber + " is delayed by " + delayTime + " minutes.");
    }
  }
}

The above class represents an airplane with a flight number, destination, and scheduled departure time. It has getter and setter methods for these attributes. The class also two methods “delay()” and “checkStatus()” to delay the flight and check its status. The “delay()” method takes an integer value representing the number of minutes the flight will be delayed and updates the scheduled departure time accordingly. By using “checkStatus()” method, you can determine whether the flight has been delayed or is on time.

// Main.java
import java.time.LocalTime;

public class Main {
  public static void main(String[] args) {
    Airplane flight1 = new Airplane("CDE345", "London", LocalTime.of(10, 30));
    Airplane flight2 = new Airplane("KUI765", "New York", LocalTime.of(14, 0));
    Airplane flight3 = new Airplane("JUY456", "Paris", LocalTime.of(14, 0));
    System.out.println("Flight Status:");
    flight1.checkStatus();
    flight2.checkStatus();
    flight3.checkStatus();
    flight1.delay(40);
    flight2.delay(110);
    System.out.println("\nCurrent Flight Status:");
    flight1.checkStatus();
    flight2.checkStatus();
    flight3.checkStatus();
  }
}

In the main() function, we create three "Airplane" objects and set flight numbers, destinations and scheduled departure times. It then calls the "checkStatus()" method to display the initial flight status of each flight. The program then delays flight1 and flight2 by calling the "delay()" method on these objects, and then calls the "checkStatus()" method again to display the updated flight status of each flight.

Sample Output:

Flight Status:
Flight CDE345 is on time.
Flight KUI765 is on time.
Flight JUY456 is on time.

Current Flight Status:
Flight CDE345 is delayed by 40 minutes.
Flight KUI765 is delayed by 110 minutes.
Flight JUY456 is on time.

Flowchart:

Flowchart: Java  OOP Exercises: Airplane class to check flight status and delay.
Flowchart: Java  OOP Exercises: Airplane class to check flight status and delay.
Flowchart: Java  OOP Exercises: Airplane class to check flight status and delay.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Library class with add and remove books methods.
Java OOP Next: Inventory Management.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.