Car Instrument Simulator

The problem

For this exercise, you will design a set of classes that work together to simulate a car's fuel gauge and odometer. The classes you will design are the following:

The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are as follows:

  • To know the car’s current amount of fuel, in gallons.
  • To report the car’s current amount of fuel, in gallons.
  • To be able to increment the amount of fuel by 1 gallon. This simulates putting fuel in the car. ( The car can hold a maximum of 15 gallons.)
  • To be able to decrement the amount of fuel by 1 gallon, if the amount of fuel is greater than 0 gallons. This simulates burning fuel as the car runs.

The Odometer Class: This class will simulate the car’s odometer. Its responsibilities are as follows:

  • To know the car’s current mileage.
  • To report the car’s current mileage.
  • To be able to increment the current mileage by 1 mile. The maximum mileage the odometer can store is 999,999 miles. When this amount is exceeded, the odometer resets the current mileage to 0.
  • To be able to work with a FuelGauge object. It should decrease the FuelGauge object’s current amount of fuel by 1 gallon for every 24 miles traveled. (The car’s fuel economy is 24 miles per gallon.)

Demonstrate the classes by creating instances of each. Simulate filling the car up with fuel, and then run a loop that increments the odometer until the car runs out of fuel. During each loop iteration, print the car’s current mileage and amount of fuel.

Breaking it down

FuelGauge class

class FuelGauge {

    final static int MAXIMUM_GALLONS = 15;

    private int gallons;

    public FuelGauge() {
        gallons = 0;
    }

    /**
     * Constructor should initialize the number of gallons of gas. If the
     * gallons is greater than the max it will default to the max.
     *
     * TODO: If gallons is greater than max throw GasOverflowException
     *
     * @param gallons
     */
    public FuelGauge(int gallons) {
        if (gallons <= MAXIMUM_GALLONS) {
            this.gallons = gallons;
        } else {
            gallons = MAXIMUM_GALLONS;
        }
    }

    public int getGallons() {
        return gallons;
    }

    /**
     * Method will add one gallon, if gallon is greater than the max for the
     * tank then a message will be output.
     */
    public void addGallons() {
        if (gallons < MAXIMUM_GALLONS) {
            gallons++;
        } else {
            // TODO: see constructor, throw GasOverflowException
            System.out.println("FUEL OVERFLOWING!!!");
        }
    }

    /**
     * Burn fuel will reduce the gallons in the tank by 1, if the fuel tank
     * is empty then a message will be outputed.
     *
     * TODO: Instead of outputting out of fuel, throw a
     * GasTankEmptyException
     */
    public void burnFuel() {
        if (gallons > 0) {
            gallons--;
        } else {
            System.out.println("OUT OF FUEL!!!");
        }
    }

Odometer class

class Odometer {

    // Constant for the maximum mileage
    public final int MAXIMUM_MILEAGE = 999999;

    // Constant for the miles-per-gallon
    public final int MPG = 24;

    private int initialMileage;
    private int mileage;

    // Field to reference a FuelGauge object
    private FuelGauge fuelGauge;

    /**
     * Constructor
     *
     * @param m
     *            Initial mileage.
     * @param fg
     *            A reference to a FuelGauge object.
     */
    public Odometer(int mileage, FuelGauge fuelGauge) {
        this.initialMileage = mileage;
        this.mileage = mileage;
        this.fuelGauge = fuelGauge;
    }

    /**
     * getMileage method
     *
     * @returns The mileage.
     */
    public int getMileage() {
        return mileage;
    }

    /**
     * The addMileage method increments the mileage field. If mileage
     * exceeds the maximum amount, it rolls over to 0.
     */
    public void addMileage() {

        if (mileage < MAXIMUM_MILEAGE) {
            mileage++;
        } else {
            mileage = 0;
        }

        int driven = initialMileage - mileage;
        if (driven % MPG == 0) {
            fuelGauge.burnFuel();
        }
    }
}

Main program

public static void main(String[] args) {

    CarInstrumentSimulator carInstrumentSimulator = new CarInstrumentSimulator();

    FuelGauge fuel = carInstrumentSimulator.new FuelGauge();
    Odometer odometer = carInstrumentSimulator.new Odometer(0, fuel);

    for (int x = 0; x < FuelGauge.MAXIMUM_GALLONS; x++) {
        fuel.addGallons();
    }

    // dive until you can't drive no longer.
    while (fuel.getGallons() > 0) {

        // add mile driven
        odometer.addMileage();

        // Display the mileage.
        System.out.println("Mileage: " + odometer.getMileage());

        // Display the amount of fuel.
        System.out.println("Fuel level: " + fuel.getGallons() + " gallons");
        System.out.println("------------------------------");
    }
}

Output

Mileage: 1
Fuel level: 15 gallons
------------------------------
Mileage: 2
Fuel level: 15 gallons
------------------------------
Mileage: 3
Fuel level: 15 gallons
------------------------------
....
------------------------------
Mileage: 359
Fuel level: 1 gallons
------------------------------
Mileage: 360
Fuel level: 0 gallons
------------------------------

Level Up

  • Modify FuelGauge class to accept a variable amount of gallons to be used with any vehicle.
  • Modify FuelGauge class constructor where if it accepts over the max number of gallons it should throw an GasOverflowException.
  • Modify FuelGauge class to throw a throw a GasTankEmptyException when there isn't any gas remaining in the tank.
  • Modify Odometer class to accept a variable for max number of miles. While the max is 999,999 it could be possible that a car has a rebuild engine.