This is a problem in basic algebra. You are given (you ask the user for)
- The number of gallons of gas in the tank
- The fuel efficiency in miles per gallon
- The price of gas per gallon
and asked to find two things:
- cost per 100 miles and
- how far the car can go with the gas in the tank.
Compute each one separately and print the result.
Here is an outline of a class you might use.
/**
* A Car class for exercise E 4.10
*/
public class Car4_10
{
// instance variables
private double gallonsInTank;
private double fuelEfficiency;
private double pricePerGallon;
/**
* Constructor for objects of class Car4_10
*/
public Car4_10(double gallonsInTank, double fuelEfficiency, double pricePerGallon)
{
// initialize instance variables
}
public double getCostPerMile()
{
return /* compute cost per mile */
}
public double getDistanceTilEmpty()
{
return /* compute distance until empty */
}
}
Here is a sketch of a JUnit test method you might use.
@Test
public void testE4_10() {
Scanner scanner = new Scanner(System.in);
System.out.print("Gallons in tank: ");
double gallonsInTank = scanner.nextDouble();
// similarly for fuelEfficiency and pricePerGallon
Car4_10 car = new Car4_10(gallonsInTank, fuelEfficiency, pricePerGallon);
System.out.print("Expected cost for 100 miles: ");
double expectedCostFor100Miles = scanner.nextDouble();
assertEquals(expectedCostFor100Miles, 100 * car.getCostPerMile(), 0.01);
// similarly for distance 'til empty
}