View the step-by-step solution to:

You have freedom to choose the number and types of instance variables you use, the approaches to the calculations, and other such internal design...

You have freedom to choose the number and types of instance variables you use, the approaches to the calculations, and other such internal design considerations. This is the most fundamental concept of encapsulation in an object-oriented system. It doesn't matter exactly how your class works, as long as it works properly whenever the public accessor and mutator methods are invoked. There are many possible designs, and there is no single "right" or "best" solution that meets all of the functional expectations.

However, we're also taking this opportunity to start to practice development in the idiomatic style of Java, so please be sure to review the project grading rubric and follow its guidelines to maximize your score from the human review process. Of course you may always ask for help in the forum at any time!

Note that our FuelMonitor class isn't a complete stand-alone program. It can't be launched on its own, as it doesn't include a main() method. Further, no methods accept any user input, nor methods produce any user output. This is intentional! This class is meant to be just one small component of a much larger system.


This is what I have so far for the FuelMonitor:

public class FuelMonitor

{

private int eachTripMileage;

private int lifetimeMileage;

private int greenModeMileage;

private int sportModeMileage;

private double eachTripFuelUsage;

private double lifetimeFuelUsage;

private double sportModeFuelUsage;

private double greenModeFuelUsage;

private double fuelLevel;

   

  /**

   * Creates a new FuelMonitor object.

   *

   * The constructor will initialize the lifetime and trip miles counters to zero, and initialize

   * the fuel tank to be as specified. The first trip is implicitly started upon construction.

   *

   * @param initialFuel the starting amount of fuel in the tank, in gallons.

   */

public FuelMonitor(double initialFuel)

  {

  

   lifetimeMileage = 0;

   eachTripMileage = 0;

   greenModeMileage = 0;

   sportModeMileage = 0;

   fuelLevel = 0;

     

  }


  /**

   * Indicate miles driven and gallons used in green mode.

   *

   * This method is called periodically by other components, as miles are driven and/or fuel is

   * used, while the vehicle is in green mode.

   *

   * @param miles number of miles driven.

   * @param gallons amount of fuel used.

   */

   public void useInGreenMode(int miles, double gallons)

  {

  

     lifetimeMileage += miles;

     lifetimeFuelUsage += gallons;

     greenModeMileage += miles;

     greenModeFuelUsage += gallons;

     eachTripMileage += miles;

     eachTripFuelUsage += gallons;

     fuelLevel -= gallons;

 

  }


  /**

   * Indicate miles driven and gallons used in sport mode.

   *

   * This method is called periodically by other components, as miles are driven and/or fuel is

   * used, while the vehicle is in sport mode.

   *

   * @param miles number of miles driven.

   * @param gallons amount of fuel used.

   */

   public void useInSportMode(int miles, double gallons)

  {

  

   lifetimeMileage = lifetimeMileage + miles;

   lifetimeFuelUsage +=gallons;

   sportModeMileage = sportModeMileage + miles;

   sportModeFuelUsage += gallons;

   eachTripMileage += miles;

   eachTripFuelUsage += gallons;

   fuelLevel -= gallons;

    

  }


  /**

   * Indicate that fuel has been added to the vehicle.

   *

   * This method is called when the fuel tank has been partially or completely refilled.

   *

   * @param gallons amount of fuel added.

   */

   public void addFuel(double gallons)

  {

  

     fuelLevel += gallons;

    

  }


  /**

   * Indicate that the trip meter has been reset.

   *

   * This method is called when the driver initiates a trip reset. This action completes the

   * current trip and begins a new trip.

   */

   public void resetTrip()

  {

  

   eachTripMileage = 0;

   eachTripFuelUsage = 0;

   sportModeMileage = 0;

   sportModeFuelUsage = 0;

   greenModeMileage = 0;

   greenModeFuelUsage = 0;

    

  }


  /**

   * Report the number of miles driven on the current trip.

   *

   * This includes all miles driven this trip, regardless of mode, since the last trip reset.

   *

   * @return miles driven this trip.

   */

   public int getTripMiles()

  {

  

   return greenModeMileage + sportModeMileage;

  

  }


  /**

   * Report the average fuel efficiency of the current trip, in miles per gallon (MPG).

   *

   * This includes all trip miles, consistent with getTripMiles(), and all fuel used during the

   * current trip. The result is rounded down (truncated) to a multiple of 0.1 MPG.

   *

   * Academic Note: This method won't be called until at least some fuel has been used this trip.

   *

   * @return truncated fuel efficency this trip.

   */

   public double getTripMPG()

  {

  

   double mpg = eachTripMileage / eachTripFuelUsage * .01;

   return mpg;

     

  }


  /**

   * Report the amount of fuel remaining, in gallons.

   *

   * The value returned is exact, not truncated.

   *

   * @return fuel currently in the tank.

   */

   public double getFuelRemaining()

  {

     

    return fuelLevel;

     

  }


  /**

   * Report the estimated number of miles remaining, in sport mode, with the current fuel.

   *

   * The estimate is based on the actual fuel efficiency achieved during the sport mode portion of

   * this trip, and the fuel remaining according to getFuelRemaning(). The value returned is

   * rounded down (truncated) to a multiple of 10 miles.

   *

   * Academic Note: This method won't be called until at least some fuel has been used this trip

   * in sport mode.

   *

   * @return estimated sport miles remaining.

   */

   public int getSportMilesRemaining()

  {

     

   int result = findRemainingMile(fuelLevel, sportMileTrip, sportModeUsage);

   return result;

  }


  /**

   * Report the estimated number of miles remaining, in green mode, with the current fuel.

   *

   * The estimate is based on the actual fuel efficiency achieved during the green mode portion of

   * this trip, and the fuel remaining according to getFuelRemaning(). The value returned is

   * rounded down (truncated) to a multiple of 10 miles.

   *

   * Academic Note: This method won't be called until at least some fuel has been used this trip

   * in green mode.

   *

   * @return estimated green miles remaining.

   */

  public int getGreenMilesRemaining()

  {

    // TODO: Your implementation goes here

    return 0; // TODO: Replace this placeholder

  }


  /**

   * Report the total number of miles driven.

   *

   * This is the sum of all miles driven in any mode since this object was constructed.

   *

   * @return lifetime miles driven.

   */

  public int getLifetimeMiles()

  {

    // TODO: Your implementation goes here

    return 0; // TODO: Replace this placeholder

  }


  /**

   * Report the average fuel efficiency, in miles per gallon, across all trips.

   *

   * The return value is rounded down (truncated) to a multiple of 0.1 MPG.

   *

   * Academic Note: This method won't be called until at least some fuel has been used.

   *

   * @return lifetime average miles per gallon.

   */

  public double getLifetimeMPG()

  {

    // TODO: Your implementation goes here

    return 0; // TODO: Replace this placeholder

  }

   

  //helper method to get reamining mileage

   

  public double findRemainingMile(double fuelLevel, int eachTripMileage, double fuelUsedMode)

  {

   double mpg = eachTripMileage / fuelUsedMode;

   int mile = (int) mpg * fuelLevel;

   mile = mile * .1;

   return mile;

  

  }


}



This is my FuelMonitorTester which should be good to go:

public class FuelMonitorTester {


  public static void main(String[] args) {


  

    System.out.println("-- Constructing new FuelMonitor object FM(14.5)");

    FuelMonitor monitor = new FuelMonitor(14.5);

    System.out.println("Trip miles (expected: 0) was: " + monitor.getTripMiles());

    System.out.println("Lifetime miles (expected: 0) was: " + monitor.getLifetimeMiles());

    System.out.println("Fuel remaining (expected: 14.5) was: " + monitor.getFuelRemaining());


    System.out.println();

    monitor.useInGreenMode(5, 0.123);

    System.out.println("-- After G(5, 0.123)");

    System.out.println("Trip miles (expected: 5) was: " + monitor.getTripMiles());

    System.out.println("Lifetime miles (expected: 5) was: " + monitor.getLifetimeMiles());


    // We've used 0.123 gallons, so 14.5 - 0.123 = 14.377 gallons remain

    System.out.println("Fuel remaining (expected: 14.377) was: " + monitor.getFuelRemaining());


    // Trip and Lifetime MPG are 5 / 0.123 = 40.6504... truncated to 40.6

    System.out.println("Trip MPG (expected: 40.6) was: " + monitor.getTripMPG());

    System.out.println("Lifetime MPG (expected: 40.6) was: " + monitor.getLifetimeMPG());


    // Green miles remaining is 40.6504... * 14.377 = 584.4309... truncated to 580

    System.out.println("Green miles remaining (expected: 580) was: " + monitor.getGreenMilesRemaining());


    System.out.println();

    monitor.useInSportMode(23, 1.234);

    monitor.useInGreenMode(150, 3.14);

    System.out.println("-- After S(23, 1.234) then G(150, 3.14)");

    System.out.println("Trip miles (expected: 178) was: " + monitor.getTripMiles());

    System.out.println("Lifetime miles (expected: 178) was: " + monitor.getLifetimeMiles());


    // We've used an additional 4.374 gallons, so 14.377 - 4.374 = 10.003 gallons remain

    System.out.println("Fuel remaining (expected: 10.003) was: " + monitor.getFuelRemaining());


    // Trip and Lifetime MPG are 178 / 4.497 = 39.5819... truncated to 39.5

    System.out.println("Trip MPG (expected: 39.5) was: " + monitor.getTripMPG());

    System.out.println("Lifetime MPG (expected: 39.5) was: " + monitor.getLifetimeMPG());


    // Green MPG this trip is 155 / 3.263 = 47.5023...

    // Green miles remaining is 10.003 * 47.5023... = 475.1655 truncated to 470

    System.out.println("Green miles remaining (expected: 470) was: " + monitor.getGreenMilesRemaining());


    // Sport MPG this trip is 23 / 1.234 = 18.6386...

    // Sport miles remaining is 10.003 * 18.6383... = 186.4417... truncated to 180

    System.out.println("Sport miles remaining (expected: 180) was: " + monitor.getSportMilesRemaining());


    System.out.println();

    monitor.addFuel(5.55);

    monitor.useInSportMode(0, 0.5);

    monitor.useInSportMode(42, 2.4);

    monitor.useInGreenMode(5, 0.01);

    System.out.println("-- After F(5.55), S(0, 0.5), S(42, 2.4), then G(5, 0.01)");

    System.out.println("Trip miles (expected: 225) was: " + monitor.getTripMiles());

    System.out.println("Lifetime miles (expected: 225) was: " + monitor.getLifetimeMiles());


    // We've added a net 2.64 gallons, so 10.003 + 2.64 = 12.643 gallons remain

    System.out.println("Fuel remaining (expected: 12.643) was: " + monitor.getFuelRemaining());


    // Trip and Lifetime MPG are 225 / (2.91 + 4.497) = 30.3767... truncated to 30.3

    System.out.println("Trip MPG (expected: 30.3) was: " + monitor.getTripMPG());

    System.out.println("Lifetime MPG (expected: 30.3) was: " + monitor.getLifetimeMPG());


    // Green MPG this trip is 160 / 3.273 = 48.8848...

    // Green miles remaining is 12.643 * 48.8848... = 618.0507... truncated to 610

    System.out.println("Green miles remaining (expected: 610) was: " + monitor.getGreenMilesRemaining());


    // Sport MPG this trip is 65 / 4.134 = 15.7233...

    // Sport miles remaining is 12.643 * 15.7233... = 198.7893... truncated to 190

    System.out.println("Sport miles remaining (expected: 190) was: " + monitor.getSportMilesRemaining());


    System.out.println();

    monitor.resetTrip();

    monitor.useInSportMode(20, 1.111);

    monitor.useInGreenMode(345, 10);

    System.out.println("-- After R(), S(20, 1.111), then G(345, 10)");

    System.out.println("Trip miles (expected: 365) was: " + monitor.getTripMiles());

    System.out.println("Lifetime miles (expected: 590) was: " + monitor.getLifetimeMiles());


    // We've used 11.111 gallons, so 12.643 - 11.111 = 1.532 gallons remain

    System.out.println("Fuel remaining (expected: 1.532) was: " + monitor.getFuelRemaining());


    // Trip MPG is 365 / 11.111 = 32.8503... truncated to 32.8

    System.out.println("Trip MPG (expected: 32.8) was: " + monitor.getTripMPG());


    // Lifetime MPG is 590 / (7.407 + 11.111) = 31.8609... truncated to 31.8

    System.out.println("Lifetime MPG (expected: 31.8) was: " + monitor.getLifetimeMPG());


    // Green MPG this trip is 345 / 10 = 34.5

    // Green miles remaining is 1.532 * 34.5 = 52.854 truncated to 50

    System.out.println("Green miles remaining (expected: 50) was: " + monitor.getGreenMilesRemaining());


    // Sport MPG this trip is 20 / 1.111 = 18.0018...

    // Sport miles remaining is 1.532 * 18.0018... = 27.5788... truncated to 20

    System.out.println("Sport miles remaining (expected: 20) was: " + monitor.getSportMilesRemaining());


  }


}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question