Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Modify the Miles Per Gallon application In this exercise, you'll modify an application that calculates miles per gallon based on the miles driven and gallons
Modify the Miles Per Gallon application
In this exercise, you'll modify an application that calculates miles per gallon
based on the miles driven and gallons of gas entered by a user so it displays
accurate results.
Test the application
Open the project named chexMPG that's in the exstarts directory.
Then, review the code for this project.
Run the application and test it with a range of values. Note that the miles per
gallon value sometimes has several decimal places.
Run the application and enter miles and gallons. Note that the miles
per gallon value is
Use the Math class for rounding
Use the round method of the Math class to round the miles per gallon value
to a whole number before displaying it To do that, you may need to perform
an explicit cast.
Run the application and enter miles and gallons. Make sure the code
rounds the miles per gallon correctly by comparing it to the value in step
Adjust the code so it rounds the miles per gallon value to two decimal places.
Run the application and enter miles and gallons. Make sure the code
rounds the miles per gallon correctly by comparing it to the value in step
Use the NumberFormat class for rounding
Modify the application so it uses the NumberFormat class to round the miles
per gallon value to three decimal places.
Run the application and enter miles and gallons. Compare the miles
per gallon value to the value in step and note that it uses halfeven rounding.
Adjust the code so it rounds the miles per gallon value to two decimal places.
Run the application and enter miles and gallons. Compare the miles
per gallon value to the value in step and note that it uses halfeven rounding.
import java.util.Scanner;
public class MPGApp
public static void mainString args
System.out.printlnWelcome to the Miles Per Gallon calculator";
System.out.println; print a blank line
Scanner sc new ScannerSystemin;
String input;
String choice y;
while choiceequalsIgnoreCasey
System.out.printEnter miles driven: ;
input scnextLine;
double miles Double.parseDoubleinput;
System.out.printEnter gallons of gas used: ;
input scnextLine;
double gallons Double.parseDoubleinput;
double mpg milesgallons;
System.out.printlnMiles per gallon is mpg ;
System.out.println;
System.out.printCalculate another MPGyn: ;
choice scnextLine;
System.out.println;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started