Answered step by step
Verified Expert Solution
Link Copied!

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 ch03_ex1_MPG that's in the ex_starts 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 400 miles and 15.8 gallons. Note that the miles
per gallon value is 25.31645569620253.
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 400 miles and 15.8 gallons. Make sure the code
rounds the miles per gallon correctly by comparing it to the value in step 3.
Adjust the code so it rounds the miles per gallon value to two decimal places.
Run the application and enter 400 miles and 15.8 gallons. Make sure the code
rounds the miles per gallon correctly by comparing it to the value in step 3.
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 400 miles and 15.8 gallons. Compare the miles
per gallon value to the value in step 3 and note that it uses half-even rounding.
Adjust the code so it rounds the miles per gallon value to two decimal places.
Run the application and enter 400 miles and 15.8 gallons. Compare the miles
per gallon value to the value in step 3 and note that it uses half-even rounding.
---------------------
import java.util.Scanner;
public class MPGApp {
public static void main(String[] args){
System.out.println("Welcome to the Miles Per Gallon calculator");
System.out.println(); // print a blank line
Scanner sc = new Scanner(System.in);
String input;
String choice ="y";
while (choice.equalsIgnoreCase("y")){
System.out.print("Enter miles driven: ");
input = sc.nextLine();
double miles = Double.parseDouble(input);
System.out.print("Enter gallons of gas used: ");
input = sc.nextLine();
double gallons = Double.parseDouble(input);
double mpg = miles/gallons;
System.out.println("Miles per gallon is "+ mpg +".");
System.out.println();
System.out.print("Calculate another MPG?(y/n): ");
choice = sc.nextLine();
System.out.println();
}
}
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

More Books

Students also viewed these Databases questions