Directions In this assignment you will create a single java

Directions: In this assignment, you will create:

a single .java file that contains your main() methd and your calculateMPG method as described below

A JUnit test class for your program as described below

The first part of your bonus assignment is to create a method that calculates fuel efficiency. It must meet the following requirements:

It must take distance traveled and fuel consumed as parameters, and return the number of miles per gallon. All three of these numbers should be doubles.

Your method should return 0.0 if:

Distance traveled equals 0 and fuel consumed equals 0.

Your method should return -1 if:

Either value is negative OR

Fuel consumed equals 0 and distance traveled is positive

Otherwise, return the number of miles traveled per gallon of fuel used.

Solution

import java.util.Scanner;

public class MPG {

public static void main(String[] args){

double miles; // Miles driven

double gallons; // Gallons of fuel

double mpg; // Miles-per-gallon

// Create a Scanner object for keyboard input.

Scanner keyboard = new Scanner(System.in);

/**

* Method should calculate miles per gallon

*

* @param miles

* @param gallons

* @return

*/

// Describe to the user what the program will do.

System.out.println(\"This program will calculate miles per gallon\");

// Get the miles driven.

System.out.print(\"Enter the miles driven: \");

miles = keyboard.nextDouble();

// Get gallons of fuel

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

gallons = keyboard.nextDouble();

// call calculateMilesPerGallon to run calculation

mpg = calculateMilesPerGallon(miles, gallons);

// Display the miles-per-gallon.

System.out.print(\"The miles-per-gallon is \" + mpg);

}

static double calculateMilesPerGallon(double miles, double gallons) {

if (miles==0 && gallons==0)

{

return 0.0;

}

else if(miles<0||gallons<0){

return -1;

}

else if(miles>0 && gallons==0){

return -1;

}

else{

Double result=miles / gallons;

return result ;

}

}

}

Directions: In this assignment, you will create: a single .java file that contains your main() methd and your calculateMPG method as described below A JUnit tes
Directions: In this assignment, you will create: a single .java file that contains your main() methd and your calculateMPG method as described below A JUnit tes

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site