car-instrument simulator design a set of classes that work together to simulate
ID: 3757600 • Letter: C
Question
car-instrument simulator
design a set of classes that work together to simulate a car's fuel gauge and odometer. that classes you will design are as follows:
the FuelGauge class: it will know the car's current amount of fuel in gallons
report the car's current amount of fuel in gallons
be able to increment the amount of fuel by 1 gallon. this simulates putting fuel in the car( max 15 gallons)
be able to decrement the amount of fuel by 1 gallon if the fuel is greater than 0 gallons
Odometer class: simulates the cars odometer
know the cars current mileage
report the cars current mileage
able to increment the current mileage by 1 mile with the max odometer reading of 999,999 then resets to 0
able to work with the FuelGuage object by decreasing the amount of fuel by 1 gallon every 24 miles
demostrate the classes by creating instances of each. simulate filling the car up with fuel, 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.
Odometer + MAX_MILEAGE : int - 999999 + MPG : int = 24 - mileage : int setPOint: int fuelGauge : FuelGauge + Odometer(m : int, + getMileage() : int fg : FuelGauge) + incrementMileage: void FuelGauge + MAXGALLONS = 15 : int - gallons : int - + FuelGauge() + FuelGauge(g : int) + getGallons): int + incrementGallons) : void + decrementGallons) voidExplanation / Answer
class FuelGauge {
final static int MAXIMUM_GALLONS = 15;
private int gallons;
public FuelGauge() {
gallons = 0;
}
public FuelGauge(int gallons) {
if (gallons <= MAXIMUM_GALLONS) {
this.gallons = gallons;
} else {
gallons = MAXIMUM_GALLONS;
}
}
public int getGallons() {
return gallons;
}
public void addGallons() {
if (gallons < MAXIMUM_GALLONS) {
gallons++;
} else {
// TODO: see constructor, throw GasOverflowException
System.out.println("FUEL OVERFLOWING!!!");
}
}
public void burnFuel() {
if (gallons > 0) {
gallons--;
} else {
System.out.println("OUT OF FUEL!!!");
}
}
class Odometer {
public final int MAXIMUM_MILEAGE = 999999;
public final int MPG = 24;
private int initialMileage;
private int mileage;
private FuelGauge fuelGauge;
public Odometer(int mileage, FuelGauge fuelGauge) {
this.initialMileage = mileage;
this.mileage = mileage;
this.fuelGauge = fuelGauge;
}
public int getMileage() {
return mileage;
}
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) {
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();
}
while (fuel.getGallons() > 0) {
odometer.addMileage();
System.out.println("Mileage: " + odometer.getMileage());
System.out.println("Fuel level: " + fuel.getGallons() + " gallons");
System.out.println("------------------------------");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.