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: 3626314 • 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.

 




Method main() in client HAS TO look like:

public class CarInstrumentSimulator
{
public static void main(String[] args)
{
// Create a FuelGuage object.
FuelGauge fuel = new FuelGauge();

// Create an Odometer object to work with the FuelGauge object.
Odometer odometer = new Odometer(999900, fuel);

// Fill the car up with gas.
for (int i = 0; i < fuel.MAX_GALLONS; i++)
fuel.incrementGallons();

// Drive the car until it runs out of gas.
while (fuel.getGallons() > 0)
{
// Drive a mile.
odometer.incrementMileage();

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

 

 

 

 

 

The output may look like:

 

 

 

Mileage: 999901

 

Fuel level: 15 gallons

 

------------------------------

 

Mileage: 999902

 

Fuel level: 15 gallons

 

------------------------------

 

Mileage: 999903

 

Fuel level: 15 gallons

 


.

 

.

 

.

 

(continues)

 

.

 

.

 

.

 

Mileage: 258

 

Fuel level: 1 gallons

 

------------------------------

 

Mileage: 259

 

Fuel level: 1 gallons

 

------------------------------

 

Mileage: 260

 

Fuel level: 0 gallons

 

------------------------------

Explanation / Answer

Odometer.java

public class Odometer {
    private int mileage;
    private int setPoint; // Miles since the fuel was last decremented
    private FuelGauge fuelGauge;
   
   
    private final int MAX_MILEAGE = 999999;
    private final int MPG = 24;
   
    public Odometer(int m, FuelGauge fg) {
        fuelGauge = fg;
        mileage = m;
   
    }
   
    public int getMileage() {
        return mileage;
    }
   
    public void incrementMileage() {
        mileage += 1;
        // If mileage is over the max, "wrap around"
        mileage = mileage % (MAX_MILEAGE+1);
       
        setPoint += 1; // Keep track of how many miles it's been since we decremented the fuel
        while (setPoint >= MPG) {
            fuelGauge.decrementGallons();
            setPoint = setPoint - MPG;
        }
    }
}

--------------------------------

FuelGauge.java

public class FuelGauge {
    private int gallons;
    public final int MAX_GALLONS = 15;
   
   
    public FuelGauge() {
        gallons = 0;
    }
    public FuelGauge(int g) {
        gallons = g;
    }
    public int getGallons() {
        return gallons;
    }
    public void incrementGallons() {
        if (gallons < MAX_GALLONS)
            gallons+= 1;
    }
    public void decrementGallons() {
        if (gallons >= 1)
            gallons -= 1;
    }
   
   
}

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