Question

In: Computer Science

Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of...

Drivers are concerned with the mileage

obtained by their automobiles. One driver has kept track of

several trips by recording miles driven and gallons used for

each trip. Develop a java program that uses a while statement

to input the miles driven and gallons used 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 tankfuls up to this point.

Enter miles driven (-1 to quit): 287

Enter gallons used: 13

MPG this trip: 22.076923

Total MPG: 22.076923

Enter miles driven (-1 to quit): 200

Enter gallons used: 10

MPG this trip: 20.000000

Total MPG: 21.173913

Enter the miles driven (-1 to quit): 120

Enter gallons used: 5

MPG this trip: 24.000000

Total MPG: 21.678571

Enter the miles used (-1 to quit): -1

Solutions

Expert Solution

(If you still have any doubts regarding this answer please comment I will definitely help)

Key points to remember:

  1. In the printing of MPG and Total_MPG we will get many digits after decimal point. But we need only upto 6 decimal places so we will use
  2. String.format("%.6f", Total_MPG) and String.format("%.6f",MPG) to get o/p upto 6 decimal places
  3. Total_MPG is equal to sum of all miles / sum of gallons upto that point. That is why in every iteration we are summing the miles and the gallons

Code Explanation:

  • Import the required packages
  • java.lang.*, java.util.*,java.io.*;
  • Create a class with any name. Class name and your file name should be same.
  • Inside the class create main function
  • declare MPG and Total_MPG as double variables
  • declare miles, gallons, Total_miles, Total_gallons as int variables and initialize Total_miles, Total_gallons to 0
  • Create an object sc by using Scanner class present in util package
  • Use an infinite while loop
  • As to enter miles. If miles is -1 then break the loop
  • else add miles to Total_miles
  • Then ask to enter gallons
  • add gallons to Total_gallons.
  • Calculate MPG = miles/gallons and print it
  • Then calculate Total_MPG = Total_miles / Total_gallons and print it.
  • The loop iterates until we enter miles is -1. If -1 is entered loop exists and program stops running.

Code:

import java.lang.*;
import java.util.*;
import java.io.*;
public class Main
{
   public static void main(String[] args)
   {
  
   double MPG, Total_MPG;
   int gallons, miles, Total_miles = 0, Total_gallons = 0;
   Scanner sc = new Scanner(System.in);
       while(true)
       {
       System.out.print("Enter miles driven (-1 to quit): ");
       miles = sc.nextInt();
       if(miles == -1)
       break;
       Total_miles = Total_miles + miles;
       System.out.print("Enter gallons used: ");
       gallons = sc.nextInt();
       Total_gallons = Total_gallons + gallons;
       MPG = (double) miles/gallons;
       System.out.printf("MPG this trip: "+String.format("%.6f", MPG));
       Total_MPG = (double) Total_miles/Total_gallons;
       System.out.printf("\nTotal MPG: "+String.format("%.6f", Total_MPG)+"\n");
       }
   }
}

Sample O/P1:

Enter miles driven (-1 to quit): 287                                                                                            

Enter gallons used: 13                                                                                                          

MPG this trip: 22.076923                                                                                                        

Total MPG: 22.076923                                                                                                            

Enter miles driven (-1 to quit): 200                                                                                            

Enter gallons used: 10                                                                                                          

MPG this trip: 20.000000                                                                                                        

Total MPG: 21.173913                                                                                                            

Enter miles driven (-1 to quit): 120                                                                                            

Enter gallons used: 5                                                                                                           

MPG this trip: 24.000000                                                                                                        

Total MPG: 21.678571                                                                                                            

Enter miles driven (-1 to quit): -1  

Sample O/P2:

Enter miles driven (-1 to quit): 155                                                                                          

Enter gallons used: 20                                                                                                        

MPG this trip: 7.750000                                                                                                       

Total MPG: 7.750000                                                                                                           

Enter miles driven (-1 to quit): 257                                                                                          

Enter gallons used: 30                                                                                                        

MPG this trip: 8.566667                                                                                                       

Total MPG: 8.240000                                                                                                           

Enter miles driven (-1 to quit): -1

Code Screenshot:

Sample O/P1 screenshot:

Sample O/P2 screenshot:


Related Solutions

Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of...
Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of fuel by recording the kilometres driven and the litres used for each tankful. Develop a python program that prompts the user to input the kilometres driven and litres used for each tankful. The program should calculate and display the kilometres per litre obtained for each tankful. Read inputs for at least 10 such readings. After processing all input information, the program...
Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of...
Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of fuel by recording the kilometres driven and the litres used for each tankful. Develop a python program that prompts the user to input the kilometres driven and litres used for each tankful. The program should calculate and display the kilometres per litre obtained for each tankful. Read inputs for at least 10 such readings. After processing all input information, the program...
Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of...
Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of fuel by recording the kilometres driven and the litres used for each tankful. Develop a python program that prompts the user to input the kilometres driven and litres used for each tankful. The program should calculate and display the kilometres per litre obtained for each tankful. Read inputs for at least 10 such readings. After processing all input information, the program...
Performance Measurement – MCE – Chapter 11 Management of the Linden Co. has kept track of...
Performance Measurement – MCE – Chapter 11 Management of the Linden Co. has kept track of the process time to complete an order. The following average times for the most recent month.                                            Days Wait time                         7.4 Inspection time               .6 Process time                    2.1 Move time                          .7 Queue time                      3.5 Compute the following: Delivery cycle time Throughput time Manufacturing cycle efficiency What percentage of time is spent on non-value-added activities?
An analyst has kept track of number of defect produced by five workers on five different...
An analyst has kept track of number of defect produced by five workers on five different machines the result are shown below for a run of 400 units per machine M1 M2 M3 M4 M5 1 1 7 4 8 2 2 7 4 5 2 1 3 3 4 3 2 3 4 9 2 8 8 5 5 0 3 2 4 3 USE excel and determine the set of assisments that will minimize the toatal number of...
A professor has kept track of test scores for students who have attended every class and...
A professor has kept track of test scores for students who have attended every class and for students who have missed one or more classes. below are scores collected so far. perfect: 80,86,85,84,81,92,77,87,82,90,79,82,72,88,82 missed 1+:61,80,65,64,74,78,62,73,58,72,67,71,70,71,66 1. Evaluate the assumptions of normality and homoscedasticity 2. conduct a statistical test to assess if exam scores are different between perfect attenders and those who have missed class 3. What is the meaning of the 95% confidence interval given from the R code. What...
.A machine cost $200,000 and has a salvage value of $100,000 if kept for one year....
.A machine cost $200,000 and has a salvage value of $100,000 if kept for one year. The salvage value will decrease by $50,000 in years 2 and 3 and remain zero after year 3. The operating costs are $50,000 the first year and increase by $50,000 per year. So operating costs in year two will be $100,000, and in year three $150,000 and so on. How long should the equipment be kept so that annual cost is minimized if the...
4.A machine cost $200,000 and has a salvage value of $100,000 if kept for one year....
4.A machine cost $200,000 and has a salvage value of $100,000 if kept for one year. The salvage value will decrease by $50,000 in years 2 and 3 and remain zero after year 3. The operating costs are $50,000 the first year and increase by $50,000 per year. So operating costs in year two will be $100,000, and in year three $150,000 and so on. How long should the equipment be kept so that annual cost is minimized if the...
The maker of Tangerine automobiles has two assembly plants: the one in Metropolis producers 36% of...
The maker of Tangerine automobiles has two assembly plants: the one in Metropolis producers 36% of the vehicles; and the one in Gotham produces the other 64% of the vehicles. They then all are shipped to a distribution site, where they undergo extensive quality tests. It seems that 85% of the vehicles from Metropolis pass the test; but only 75% of vehicles from Gotham pass the test. a- What is the probability that a randomly chosen vehicle shipped to the...
One of the girls on your college track team has diabetes. She has shared with you...
One of the girls on your college track team has diabetes. She has shared with you some of her fears and concerns. You ask her if you can share this information with the other team members and she gives you the permission to do so. Design a lecture for the team members that will help them recognize the signs, symptoms, and complications of diabetic problems.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT