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

/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
import java.util.*;
public class Main
{
        public static void main(String[] args) 
        {
            Scanner scan = new Scanner( System.in );
            float miles,gallon,MPG;
            float totalMiles = 0, totalGallons = 0, totalMPG;
            /*infinite loop to read miles and gallons until -1 is entered*/
            /* in each iteration ask for miles and gallon, calculates MPG by dividing miles 
            by gallon. also maintain totalMiles and totalGallons to calculate totalMPG */
            while(true)
            {
                System.out.print("Enter Miles driven (-1 to quit) :");
                miles = scan.nextFloat();
                if(miles == -1)    /* if -1 is entered, break to come out of infinite loop*/
                    break;
                System.out.print("Enter Gallons used :");
                gallon = scan.nextFloat();
                MPG = miles/gallon;
                totalMiles = totalMiles + miles;
                totalGallons = totalGallons + gallon;
                totalMPG = totalMiles/totalGallons; 
                System.out.println("MPG this trip :" + MPG); /* print MPG and totalMPG on console*/
                System.out.println("Total MPG :" + totalMPG);
            }
                
        }
}

Please upvote if u like answer otherwise comment for clarification.


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...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT