ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 04 May 2017 My Price 14.00

Constructing new FuelMonitor object

Need help coding a basic fuel monitor program in Java.

 

-- Constructing new FuelMonitor object
Fuel remaining (expected: 15.0) was: 15.0
Lifetime miles (expected: 0) was: 0
Trip miles (expected: 0) was: 0 -- After driving 5 miles in the city
Lifetime miles (expected: 5) was: 5
Trip miles (expected: 5) was: 5
Lifetime MPG (expected: 22.0) was: 22.0
Trip MPG (expected: 22.0) was: 22.0
Fuel remaining (expected: ~14.7727...) was: 14.772727272727273
Miles remaining (expected: 320) was: 320 -- After driving an additional 12 miles on the highway
Lifetime miles (expected: 17) was: 17
Trip miles (expected: 17) was: 17
Fuel remaining (expected: ~14.3442...) was: 14.344155844155845
Lifetime MPG (expected: 25.9) was: 25.9
Trip MPG (expected: 25.9) was: 25.9
Miles remaining (expected: 370) was: 370 -- After driving an additional 8 miles in the city
Lifetime miles (expected: 25) was: 25
Trip miles (expected: 25) was: 25
Fuel remaining (expected: ~13.9805...) was: 13.980519480519481
Lifetime MPG (expected: 24.5) was: 24.5
Trip MPG (expected: 24.5) was: 24.5
Miles remaining (expected: 340) was: 340 -- After reset/refuel (tank needed 1.01 gallons to completely fill)
Lifetime miles (expected: 25) was: 25
Trip miles (expected: 0) was: 0
Fuel remaining (expected: 15.0...) was: 15.0
Lifetime MPG (expected: 24.7) was: 24.7

 

**
* A basic vehicle Fuel Monitor.
*
* Objects of this class track a vehicle's fuel usage and miles-per-gallon
* (MPG) efficiency, over single trips and the lifetime of the object.
* The method implementations will not use Math.round() or any other library.
* Only primitive types int and double are needed.
* All calculations should be performed at double precision, with only the
* final result rounded (where applicable).
*/
public class FuelMonitor
{
// TODO: Your private instance variable declarations go here
/**
* Creates a new FuelMonitor object.
*
* The constructor will initialize the lifetime and trip miles counters
* to zero, and initialize the fuel tank to be completely full.
* The first trip is implicitly started upon construction.
*
* @param cityMPG estimate of miles per gallon during city driving.
* @param highwayMPG estimate of miles per gallon during highway driving.
* @param tankCapacity the capacity of the fuel tank in gallons.
*/
public FuelMonitor(int tankCapacity, int cityMPG, int highwayMPG)
{
// TODO: Your implementation goes here
}
/**
* Indicate miles actually driven in a city environment.
*
* The monitor assumes that the vehicle has achieved
* exactly the cityMPG value specified in the constructor
* during the miles specified here.
*
* NOTE: Your code may assume the parameter value is non-negative.
*
* @param miles number of city miles driven.
*/
public void driveCity(int miles)
{
// TODO: Your implementation goes here
}
/**
* Indicate miles actually driven in a highway environment.
*
* The monitor assumes that the vehicle has achieved
* exactly the highwayMPG value specified in the constructor
* during the miles specified here.
*
* NOTE: Your code may assume the parameter value is non-negative.
*
* @param miles number of highway miles driven.
*/
public void driveHighway(int miles)
{
// TODO: Your implementation goes here
}
/** * Report the amount of fuel remaining on this trip.
*
* Returns an estimate, based on the number of miles driven this trip,
* and the combination of environments (city or highway) in which
* those miles were driven.
*
* The amount of fuel used is city miles driven divided by city MPG,
* plus highway miles driven divided by highway MPG.
*
* NOTE: Your code may assume this estimate will never become negative.
*
* @return estimated amount of fuel remaining in the tank.
*/
public double getFuelRemaining()
{
// TODO: Your implementation goes here
return 0; // TODO: Replace this placeholder
}
/**
* Report the number of miles that can be driven with the remaining fuel.
*
* This is based on the same estimate as returned by getFuelRemaining.
*
* The return value is rounded down to the nearest multiple of 10.
*
* NOTE: Your code may assume this method is never called until at least
* one mile (city or highway) has been driven on the current trip.
*
* NOTE: Your code can assume this estimate will never become negative.
*
* @return estimated miles remaining in the fuel tank.
*/
public int getMilesRemaining()
{
// TODO: Your implementation goes here
return 0; // TODO: Replace this placeholder
}
/**
* Reset the trip and fill the fuel tank to capacity.
*
* Calling this method indicates the start of a new trip.
* The trip miles counter will be reset to zero.
* The fuel level will be set to its capacity, as in the constructor.
*
* @param gallons exact amount of fuel needed to completely fill tank.
*
*/
public void tripResetRefuel(double gallons)
{
// TODO: Your implementation goes here
}
/**
* Return total miles driven on this trip.
*
* This is the sum of city and highway miles driven
* (after the most recent call to tripResetRefuel, if any).
*
* @return miles driven on this trip
*/
public int getTripMiles()
{ // TODO: Your implementation goes here
return 0; // TODO: Replace this placeholder
}
/**
* Return lifetime miles driven.
*
* This is the sum of all city and all highway miles driven
* since this object was constructed.
*
* @return lifetime miles driven.
*/
public int getLifetimeMiles()
{
// TODO: Your implementation goes here
return 0; // TODO: Replace this placeholder
}
/**
* Return estimated effective miles per gallon for this trip.
*
* The estimate is based on the actual miles driven on this trip,
* divided by the estimated amount of fuel used (see getFuelRemaining).
*
* The return value is rounded down to the nearest multiple of 0.1 MPG.
*
* NOTE: Your code may assume this method is never called until at least
* one mile (city or highway) has been driven on the current trip.
* @return estimated average MPG for this trip.
*/
public double getTripMPG()
{
// TODO: Your implementation goes here
return 0; // TODO: Replace this placeholder
}
/**
* Return lifetime average of miles per gallon.
*
* For the current trip, estimate fuel usage as in getFuelRemaining.
*
* For all previous trips, if any, use the actual value of fuel used,
* as specified in the one or more calls to tripResetRefuel.
*
* Use the lifetime number of miles driven as in getLifetimeMiles.
*
* The return value is rounded down to the nearest multiple of 0.1 MPG.
*
* NOTE: Your code may assume this method is never called until at least
* one mile (city or highway) has been driven ever (since construction).
*
* @return lifetime average miles per gallon.
*/
public double getLifetimeMPG()
{
// TODO: Your implementation goes here
return 0; // TODO: Replace this placeholder
}
}

 

/**
* Basic test suite for the FuelMonitor class.
*
* This file is ignored by Web-CAT, but it will be graded by a human.
* As provided, this class only performs very basic testing of your
* FuelMonitor implementation. You will need to develop additional
* tests to ensure correctness of your system.
*
*/
//CHECKSTYLE:OFF
@SuppressWarnings("javadoc")
public class FuelMonitorTester
{
public static void main(String args) {
// create a new fuel monitor object
System.out.println("-- Constructing new FuelMonitor object");
FuelMonitor monitor = new FuelMonitor(15, 22, 28);
System.out.println("Fuel remaining (expected: 15.0) was: " +
monitor.getFuelRemaining());
System.out.println("Lifetime miles (expected: 0) was: " +
monitor.getLifetimeMiles());
System.out.println("Trip miles (expected: 0) was: " +
monitor.getTripMiles());
System.out.println();
monitor.driveCity(5);
System.out.println("-- After driving 5 miles in the city");
System.out.println("Lifetime miles (expected: 5) was: " +
monitor.getLifetimeMiles());
System.out.println("Trip miles (expected: 5) was: " +
monitor.getTripMiles());
// only city driving so far, so effective mpg should be exactly city mpg
System.out.println("Lifetime MPG (expected: 22.0) was: " +
monitor.getLifetimeMPG());
System.out.println("Trip MPG (expected: 22.0) was: " +
monitor.getTripMPG());
// we have used: 5 miles (city) / 22 mpg (city) = 0.2273...
System.out.println("Fuel remaining (expected: ~14.7727...) was: " +
monitor.getFuelRemaining());
// we've only driven in the city, so expect 22 mpg * fuel remaining =
325.0 (rounded down to 320)
System.out.println("Miles remaining (expected: 320) was: " +
monitor.getMilesRemaining());
System.out.println();
monitor.driveHighway(12);
System.out.println("-- After driving an additional 12 miles on the
highway");
System.out.println("Lifetime miles (expected: 17) was: " +
monitor.getLifetimeMiles());
System.out.println("Trip miles (expected: 17) was: " +
monitor.getTripMiles());
// we have used: 5 miles (city) / 22 mpg (city) = 0.2273...
// plus we used: 12 miles (hwy) / 28 mpg (hwy) = 0.4286...
// for a combined total of 0.6558... gallons used
System.out.println("Fuel remaining (expected: ~14.3442...) was: " +
monitor.getFuelRemaining());
// mpg (trip and lifetime) is 17 miles / 0.6558... = 25.92... (rounded down to 25.9)
System.out.println("Lifetime MPG (expected: 25.9) was: " +
monitor.getLifetimeMPG());
System.out.println("Trip MPG (expected: 25.9) was: " +
monitor.getTripMPG());
// we expect the 14.3442... gallons to average 25.92... mpg so miles =
371.8 (rounded down to 370)
System.out.println("Miles remaining (expected: 370) was: " +
monitor.getMilesRemaining());
System.out.println();
monitor.driveCity(8);
System.out.println("-- After driving an additional 8 miles in the
city"); System.out.println("Lifetime miles (expected: 25) was: " +
monitor.getLifetimeMiles());
System.out.println("Trip miles (expected: 25) was: " +
monitor.getTripMiles());
// we have used: 5 miles (city) / 22 mpg (city) = 0.2273...
// plus we used: 12 miles (hwy) / 28 mpg (hwy) = 0.4286...
// then we used: 8 miles (city) / 22 mpg (city) = 0.3636...
// for a combined total of 1.0195... gallons used
System.out.println("Fuel remaining (expected: ~13.9805...) was: " +
monitor.getFuelRemaining());
// mpg (trip and lifetime) is 25 miles / 1.0195... = 24.52... (rounded
down to 24.5)
System.out.println("Lifetime MPG (expected: 24.5) was: " +
monitor.getLifetimeMPG());
System.out.println("Trip MPG (expected: 24.5) was: " +
monitor.getTripMPG());
// we expect the remaining 13.9805... gallons to average 24.52... mpg so
miles = 342.8 (rounded down to 340)
System.out.println("Miles remaining (expected: 340) was: " +
monitor.getMilesRemaining());
System.out.println();
monitor.tripResetRefuel(1.01);
System.out.println("-- After reset/refuel (tank needed 1.01 gallons to
completely fill)");
System.out.println("Lifetime miles (expected: 25) was: " +
monitor.getLifetimeMiles());
System.out.println("Trip miles (expected: 0) was: " +
monitor.getTripMiles());
System.out.println("Fuel remaining (expected: 15.0...) was: " +
monitor.getFuelRemaining());
// mpg (lifetime only; no trip yet) is 25 miles / 1.01 = 24.75...
(rounded down to 24.7)
System.out.println("Lifetime MPG (expected: 24.7) was: " +
monitor.getLifetimeMPG());
}
}

Attachments:

Answers

(11)
Status NEW Posted 04 May 2017 04:05 AM My Price 14.00

-----------

(1)