Write a program MilesPerGallonCalculator. The program prompts for the starting m
ID: 3559326 • Letter: W
Question
Write a program MilesPerGallonCalculator. The program prompts for the starting mileage on the odometer and then the ending mileage. Compute the distance traveled and display it. Then prompt for the gallons of gas consumed. If the gallons input by the user is zero or less use 1 for the gallons. Calculate the miles per gallon and display it with two places past the decimal point. Be sure to label your output. Use the command prompt (console) for input and output. You must create a Scanner object and an entire program. Do everything in main. Use an editor to create the program, then zip the .java file and drag and drop it below. Be sure to save your test frequently so that you will not be logged off for inactivity. Example: Enter starting mileage: 10564 Enter ending mileage: 10727 Distance travelled: 173 miles Enter gallons of gas: 8 Miles per gallon: 21.63Explanation / Answer
import java.util.Scanner;
class MilesPerGallonCalculator{
public static void main(String[] args){
double startMile,endMile,distance;
double milesPerGallon,gallons;
Scanner input=new Scanner(System.in);
System.out.print("Enter Starting Mileage: ");
startMile=input.nextDouble();
System.out.print("Enter Ending Mileage: ");
endMile=input.nextDouble();
distance=endMile-startMile;
System.out.println("Distance travelled: "+distance);
System.out.print("Enter gallons of gas :");
gallons=input.nextDouble();
if(gallons<=0)
gallons=1;
milesPerGallon=distance/gallons;
System.out.println("Miles per gallon: "+milesPerGallon);
input.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.