4.17 (Gas Mileage) Drivers are concerned with the mileage their automobiles get.
ID: 3853381 • Letter: 4
Question
4.17 (Gas Mileage) Drivers are concerned with the mileage their automobiles get. One driver has kept track of several trips by recording the miles driven and gallons used for each tankful. Develop a Java application that will input the miles driven and gallons used (both as integers) for each trip. The program should calculate and display the miles per gallon obtained for each trip and print the combined miles per gallon obtained for all trips up to this point. All averaging calculations should produce floating-point results. Use class Scanner and sentinel-controlled repetition to obtain the data from the user.
Explanation / Answer
the java programming code for the given program is
import java.util.Scanner;
public class gas1
{
public void inputtrip()
{
Scanner sc = new Scanner( System.in );
int miles = 0;
int gallons = 0;
double milespergallon = 0.0;
int totalmiles = 0;
int totalgallons = 0;
double totalmilespergallon = 0.0;
int trips = 0;
System.out.print( "Enter trip 1 mileage (as integer) or -1 to quit: ");
miles = sc.nextInt();
if ( miles != -1 )
{
System.out.print( "Enter trip 1 gallons (as integer): ");
gallons = sc.nextInt();
trips++;
}
// While the user has not entered sentinel
while ( miles != -1 )
{
totalmiles += miles;
totalgallons += gallons;
totalmilespergallon = (double) totalmiles / totalgallons;
milespergallon= (double) miles / gallons;
System.out.printf( "Trip %d's mpg (miles per gallon) is %.1f ", trips, milespergallon);
if ( trips > 1 )
{
System.out.printf( " Total miles of your %d trips is %d ", trips, totalmiles );
System.out.printf( " Total gallons of your %d trips is %d ", trips, totalgallons );
System.out.printf( " Combined mpg if your %d trips is %.1f ", trips, totalmilespergallon);
}
trips++;
System.out.printf( " Enter trip %d mileage (as integer) or -1 to quit: ", trips );
miles = sc.nextInt();
if ( miles != -1 )
{
System.out.printf( "Enter trip %d gallons (as integer): ", trips );
gallons = sc.nextInt();
}
}
if ( totalmiles != 0 )
{
System.out.printf( " Final total miles driven is: %d ", totalmiles );
System.out.printf( "Final total gallons used is: %d ", totalgallons );
System.out.printf( "Final combined mpg is: %.1f ", totalmilespergallon);
}
else
System.out.printf( "No trip information was entered. " );
} // end method inputtrip.
//main method.
public static void main(String []args)
{
gas1 g=new gas1();
g.inputtrip();
}
} // end class gas1
the output is:
Enter trip 1 mileage (as integer) or -1 to quit: 130
Enter trip 1 gallons (as integer): 34
Trip 1's mpg (miles per gallon) is 3.8
Enter trip 2 mileage (as integer) or -1 to quit: 650
Enter trip 2 gallons (as integer): 56
Trip 2's mpg (miles per gallon) is 11.6
Total miles of your 2 trips is 780
Total gallons of your 2 trips is 90
Combined mpg if your 2 trips is 8.7
Enter trip 3 mileage (as integer) or -1 to quit: 345
Enter trip 3 gallons (as integer): 20
Trip 3's mpg (miles per gallon) is 17.3
Total miles of your 3 trips is 1125
Total gallons of your 3 trips is 110
Combined mpg if your 3 trips is 10.2
Enter trip 4 mileage (as integer) or -1 to quit: 200
Enter trip 4 gallons (as integer): 75
Trip 4's mpg (miles per gallon) is 2.7
Total miles of your 4 trips is 1325
Total gallons of your 4 trips is 185
Combined mpg if your 4 trips is 7.2
Enter trip 5 mileage (as integer) or -1 to quit: -1
Final total miles driven is: 1325
Final total gallons used is: 185
Final combined mpg is: 7.2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.