The program should prompt the user to enter the number of ga

The program should prompt the user to enter the number of gallons used and the number of miles driven for each of the 3 tanks of gas. The program should then calculate and display the miles per gallon obtained for each tank. Once processing is complete for the 3 tanks, the program will calculate the overall mileage(total gallons/total miles) and display a friendly \" Goodbye\" message as shown below. Once you create, compile, link and run your program, your program should present the following dialog to the user: Welcome to the mileage calculator. This program will calculate the miles per gallon for you for three tanks of gas after you have entered the gallons used and miles driven. Enter the number of gallons used for tank #1: 12.8 Enter the number of miles driven: 287.1 *** The miles per gallon for this tank is 22.4 Enter the number of gallons used for tank #2: 10.3 Enter the number of miles driven: 200.2 *** The miles per gallon for this tank is 19.4 Enter the number of gallons used for tank #3: 5.2 Enter the number of miles driven: 120.9 *** The miles per gallon for this tank is 23.3 Your overall average miles per gallon for three tanks is 21.5 Thank you for using the program. Goodbye.

Solution

MileageCalc.java

import java.text.DecimalFormat;
import java.util.Scanner;


public class MileageCalc {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Welcome to the mileage calculator\");
       System.out.println(\"This program will calculate th miles per galloon for you for three tanks of gas after you have entered the gallons used miles driven\");
       double sum = 0;
       DecimalFormat df = new DecimalFormat(\"0.0\");
       for(int i=1; i<=3; i++){
       System.out.print(\"Enter the number of gallons used for tank #\"+i+\": \");
       double gallons = scan.nextDouble();
       System.out.print(\"Enter the number of miles drven: \");
       double miles = scan.nextDouble();
       double milesPerGallon = miles/gallons;
       System.out.println(\"*** The miles per gallon for ths tank is \"+df.format(milesPerGallon));
       sum = sum + Double.valueOf(df.format(milesPerGallon));
       }
       System.out.println(\"Your overall average miles per gallon for three tanks is \"+df.format((sum/3)));
       System.out.println(\"Thank you for using this program. Goodbye\");
      
   }

}

Output:

Welcome to the mileage calculator
This program will calculate th miles per galloon for you for three tanks of gas after you have entered the gallons used miles driven
Enter the number of gallons used for tank #1: 12.8
Enter the number of miles drven: 287.1
*** The miles per gallon for ths tank is 22.4
Enter the number of gallons used for tank #2: 10.3
Enter the number of miles drven: 200.2
*** The miles per gallon for ths tank is 19.4
Enter the number of gallons used for tank #3: 5.2
Enter the number of miles drven: 120.9
*** The miles per gallon for ths tank is 23.2
Your overall average miles per gallon for three tanks is 21.7
Thank you for using this program. Goodbye

 The program should prompt the user to enter the number of gallons used and the number of miles driven for each of the 3 tanks of gas. The program should then c
 The program should prompt the user to enter the number of gallons used and the number of miles driven for each of the 3 tanks of gas. The program should then c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site