Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Do not use global variables. Do not write OOP programs. 1. Modify the Miles Per Gallon program I have completed (For this program,

Do not use global variables. Do not write OOP programs.

1.

Modify the "

Miles Per Gallon

" program I have completed

(For this program, you don't need

vehicle information

variable.)

-

Step 1: Write method definition:

It takes

the

miles-driven

and

gallons-used

as the parameters, then

calculates and returns the

miles-per-gallon

-

Step 2: In the main method, the program does the following:

It prompts the user to enter the

miles-driven

and

gallons-used

.

It calls the method with appropriate arguments to get the result if the input is valid. Otherwise,

prints an error message.

It prints the result and

formats the result to 2 decimal places.

If the MPS is more than 24, it prints a message indicating that it is above national average.

Otherwise, prints a message indicating that it is equal to or below national average

-

Step 3

Use a loop to continue to run the program until the user choose not to. You can use any kind of

loop structure.

In the end, print the average MPG.

Sample Run:

Enter the number of miles driven (-1 to end):

123.45

Enter the gallons of gas used:

5.6

The car's miles-per-gallon (MPG) is 22.04

The car's MPG is below the national average.

Enter the number of miles driven (-1 to end):

56.6

Enter the gallons of gas used:

1.5

The car's miles-per-gallon (MPG) is 37.73

The car's MPG is above the national average.

Enter the number of miles driven (-1 to end):

-12

Enter the gallons of gas used:

3

Invalid input. The values should be greater than zero.

Enter the number of miles driven (-1 to end):

34

Enter the gallons of gas used:

-45

Invalid input. The values should be greater than zero.

Enter the number of miles driven (-1 to end):

250.89

Enter the gallons of gas used:

5.0

The car's miles-per-gallon (MPG) is 50.18

The car's MPG is above the national average.

Enter the number of miles driven (-1 to end):

-1

Average MGP: 35.61

import java.util.Scanner;

public class MPG {

public static void main(String[] args) {

Scanner console = new Scanner(System.in);

// declare variables

double Miles, Gas, mpg;

String Vehicle;

// read input

System.out.print("Enter the vehicle information: ");

Vehicle = console.next();

System.out.print("Enter the number of miles Driven:");

Miles = console.nextDouble();

System.out.print("Enter the gallons of gas used:");

Gas = console.nextDouble();

if (Miles < 0 || Gas < 0) {

System.out.println(" Invalid input");

}

else {

// calcaulte mpg

mpg = Miles / Gas;

System.out.printf("%.2f", mpg);

// print output

System.out.println("Miles-per-gallon (MPG):" + mpg);

if (mpg > 24)

System.out.println("The car's MPG is above the national average.");

else if (mpg == 24)

System.out.println("The car's MPG is equal to the national average.");

else

System.out.println("The car's MPG is below the national average.");

}

}

}

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

Transport Operations

Authors: Allen Stuart

2nd Edition

978-0470115398, 0470115394

Students also viewed these Programming questions

Question

6. How should it position Kai Shii?

Answered: 1 week ago