In: Computer Science
IN JAVA PLEASE
Program 4: Is there a Prius version? Did you know that the average Boeing 747 airplane uses approximately 1 gallon of fuel per second? Given the speed of the airplane, that means it gets 5 gallons to the mile. No, not 5 miles to the gallon, 5 gallons to the mile. You may be questioning why such a horribly inefficient machine is allowed to exist, but you’ll be happy to find out that, because this airplane hold 568 people, it averages about 0.01 gallons per person – (100 miles per gallon per person). Your job is to design (pseudocode) and implement (source code) a program that asks the user for a distance the plane has to fly (i.e. the length of the trip) and also asks the cost of jet fuel (which is currently $1.80 per gallon). The program should then calculate the total fuel charges to make the trip. Next, ask the user how many people will fly, as well as the average cost of a ticket. Finally, print the total profit made (or lost) and the average gas mileage per person. Document your code and properly label the input prompts and the outputs as shown below.
Sample run 1: Enter the flight distance: 1000 Enter the current cost of jet fuel: $2 The flight will cost $10000.0 in fuel. Enter the number of passengers: 5 Enter the average cost of a ticket: 1000 You will make a profit of $-5000.0 You averaged 1.0 miles per person per gallon!
Sample run 2: Enter the flight distance: 500 Enter the current cost of jet fuel: $3 The flight will cost $7500.0 in fuel. Enter the number of passengers: 500 Enter the average cost of a ticket: 300 You will make a profit of $142500.0 You averaged 100.0 miles per person per gallon!
Sample run 3: Enter the flight distance: 4200 Enter the current cost of jet fuel: $1.80 The flight will cost $37800.0 in fuel. Enter the number of passengers: 550 Enter the average cost of a ticket: 600 You will make a profit of $292200.0 You averaged 110.0 miles per person per gallon!
// do comment if any problem arises
//code
import java.util.Scanner;
public class Airplane {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the flight distance: ");
// read distance to travel
int distance = sc.nextInt();
System.out.print("Enter the current cost of jet fuel: $");
// read cost of fuel per gallon
float fuel_cost = sc.nextFloat();
// calculate total cost to operate flight
float flight_cost = (float) distance * fuel_cost * 5;
System.out.println("The flight will cost $" + flight_cost + " in fuel.");
System.out.print("Enter the number of passengers: ");
// read number of passengers that are boarding in flight
int passengers = sc.nextInt();
System.out.print("Enter the average cost of a ticket: ");
// read cost of each ticket
float ticket_cost = sc.nextFloat();
// calculate total cost produced by tickets
float Ticket_total = ticket_cost * (float) passengers;
// calculate profit of flight
float profit = Ticket_total - flight_cost;
System.out.println("You will make a profit of $" + profit);
// calculate miles per person per gallon
float miles_per_person = (float) passengers / 5;
System.out.println("You averaged " + miles_per_person + " miles per person per gallon!");
}
}
Output of 3 sample runs: