Completed Method

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

The new model car is below.

Completed Method

Notice how the oldest odometer reading is forgotten as the newest one is recorded. After the change, (endMiles - startMiles) will be the number of miles traveled between the previous fillup and this one.

class Car
{
  // data
  private int startMiles;   // Stating odometer reading
  private int endMiles;     // Ending odometer reading
  private double gallons;   // Gallons of gas used between the readings

  // constructor
  Car(  int first, int last, double gals  )
  {
    startMiles = first ;
    endMiles   = last ;
    gallons    = gals ;
  }

  // methods
  double calculateMPG()
  {
    return (endMiles - startMiles)/gallons ;
  }

  void fillUp(int newOdo, double fillUpGals )
  {

    startMiles = endMiles;

    endMiles   = newOdo;

    gallons    = fillUpGals;

  }
}

Now let us consider what a fillUp method for an entire Fleet will look like.

QUESTION 13:

(Design decision: ) What four parameters will the Fleet's fillUp method have?