An automobile is used for commuting purposes. Write a program that takes as input the distance of the commute in miles, the automobile’s fuel consumption rate in miles per gallon, and the price of a gallon of gas. The program should then output the cost of the commute.
import java.util.Scanner;
public class AutomobileDemo
{
public static void main(String[] args)
{
double distance, mpg, gasPrice;
Scanner scn = new Scanner(System.in);
System.out.print("Enter the distance in miles:");
distance = scn.nextDouble();
System.out.print("Enter rate of the car (mpg):");
mpg = scn.nextDouble();
System.out.print("Enter the cost of one gallon:");
gasPrice = scn.nextDouble();
System.out.println("Cost of the commute:" +(distance / mpg * gasPrice));
}
}
OUTPUT:
Enter the distance in miles:150
Enter rate of the car (mpg):60
Enter the cost of one gallon:15.5
Cost of the commute:38.75