Answer to Question #287920 in Java | JSP | JSF for Hdjejhehe

Question #287920

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


1
Expert's answer
2022-01-16T09:03:15-0500
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;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog