Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment is about aggregation and class collaboration. You will create se

ID: 3743257 • Letter: T

Question

This assignment is about aggregation and class collaboration.

You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange.

The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current value.

You should also have a display that shows how many miles traveled since you last added gasoline, MilesSinceLastGas, and a display for the estimated number of miles until you run out of gasoline, the CruisingRange.

The Odometer should keep track of total mileage from 0 to 999,999. After that it turns over back to 0. Make sure your code allows for this rollover.

The overall class is to be named InstrumentDisplay. You may create the various outputs using System.out or JOptionPane. Make sure that account for both filling and emptying the tank. While your odometer will display in 1 mile increments, you should keep track of mileage internally in one tenth of a mile increments for purposes of computing gasoline remaining in the FuelGage and miles in the CruisingRange

Please make sure you comment your code thoroughly.

The code should be nicely formatted and should use proper variables.

Explanation / Answer

// First Create a superclass, named "Instrument Display"

package com.levelup.java.exercises.beginner;
public class InstrumentDisplay {

/*

Within this class, Create a FuelGauge class having one local member named 'gallon to keep count.

*/


class FuelGauge {
final static int MAXIMUM_GALLONS = 15;
private int gallons;

public FuelGauge() {
gallons = 0;
}

// Constructor Function to initlialize gallon value with the help of passed parameter.

public FuelGauge(int gallons) {

   if (gallons <= MAXIMUM_GALLONS) {

    this.gallons = gallons;

   } else {

    gallons = MAXIMUM_GALLONS;

   }

  }

//This function is used to retrieve total gallons consumed.

public int getGallons() {

   return gallons;

  }

// To Add More Gallon

public void addGallons() {
if (gallons < MAXIMUM_GALLONS) {
gallons++;
} else {

System.out.println("Overflow of Fuel");
}
}
/* ConsumeFuel function will reduce the gallons in the tank by 1, if the fuel tank is empty then a message will be outputed. */

public void ConsumeFuel() {
if (gallons > 0) {
gallons--;
} else {
System.out.println("OUT OF FUEL!!!");
}
}
}

// now Our second class is Odometer

class Odometer {

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

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

private int initialMileage;
private int mileage;

// Field to reference a FuelGauge object

private FuelGauge fuelGauge;

// Now Constructor function of Odometer is declared

public Odometer(int mileage, FuelGauge fuelGauge) {
this.initialMileage = mileage;
this.mileage = mileage;
this.fuelGauge = fuelGauge;
}

// To get 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();
}
}
}

public static void main(String[] args) {

InstrumentDisplay carInstrumentSimulator = new InstrumentDisplay();

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

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

// drive 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("------------------------------");
}
}

}