Write a program which defines a class Flight . The program calculates the fuel required by the flight for a particular distance.
Accept for flight number, destination, distance and calculate the quantity of fuel required as per following distance fuel <=1000 500
>1000 and <=2000 1100 >2000 2200
Display for flight number, destination, distance and Quantity of fuel required in tabular form
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a flight number: ");
int number = scanner.nextInt();
System.out.print("Enter a flight destination: ");
String destination = scanner.next();
System.out.print("Enter a flight distance: ");
int distance = scanner.nextInt();
Flight flight = new Flight(number, destination, distance);
flight.display();
}
}
class Flight {
private final int number;
private final String destination;
private final int distance;
public Flight(int number, String destination, int distance) {
this.number = number;
this.destination = destination;
this.distance = distance;
}
public void display() {
System.out.println("\nFlight info:");
System.out.println("Number:\t\t\t" + number);
System.out.println("Destination:\t" + destination);
System.out.println("Distance:\t\t" + distance);
System.out.println("Fuel required:\t" + fuelRequired());
}
private int fuelRequired() {
if (distance <= 1000) {
return 500;
} else if (distance <= 2000) {
return 1100;
} else {
return 2200;
}
}
}
Comments
Leave a comment