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

For this assignment, you will design a set of classes that work together to simu

ID: 3626259 • Letter: F

Question

For this assignment, 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.

Explanation / Answer

FuelGauge Class public class FuelGauge { private int amountOfFuel; public FuelGauge(int gallons){ amountOfFuel = gallons; } public int getAmountOfFuel(){ return amountOfFuel; } public void incrementFuelTank(){ if (amountOfFuel < 15 ) amountOfFuel++; } public void decrementFuelTank(){ if (amountOfFuel > 0 ) amountOfFuel--; } } Odometer Class public class Odometer { private int currentMileage; public Odometer(int gallons){ currentMileage = gallons; } public int getcurrentMileage(){ return currentMileage; } public void incrementcurrentMileage(){ if (currentMileage < 999999 ) currentMileage++; } public void decrementcurrentMileage(){ if (currentMileage > 24 ) currentMileage--; } public void incrementMileage(){ } } CarSimulator Demo public class CarSimulator { public static void main(String []args){ FuelGauge amountOfFuel = new FuelGauge(15); Odometer currentMileage = new Odometer(0); while (amountOfFuel.getAmountOfFuel() > 0) { // add a mile to the odometer currentMileage.incrementcurrentMileage(); if( currentMileage.getcurrentMileage() % 24 == 0 ) amountOfFuel.decrementFuelTank(); { System.out.printf("Amount Of Fuel = %s Current Mileage = %s ", amountOfFuel.getAmountOfFuel(), currentMileage.getcurrentMileage()); } } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote