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

//Create a New Java Project // called YourLastNameCarInstrument . //For this ass

ID: 3798615 • Letter: #

Question

//Create a New Java Project // called YourLastNameCarInstrument.

//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.

NOTE: Each class should be defined in its on class file.

Explanation / Answer

//The FuelGauge Class: //
package chegg.Carfuelcalculation;
public class FuelGauge {
   private double fuel;
   private Odometer odoMeter;
   public FuelGauge(double fuel, Odometer odoMeter) {
       this.fuel = fuel;
       this.odoMeter = odoMeter;
       if (this.odoMeter != null) {
           this.odoMeter.setMileage(this.fuel * 24);
       }
   }
   public void setFuel(double fuel) {
       this.fuel = fuel;
   }
   public void setOdeMeter(Odometer odoMeter) {
       this.odoMeter = odoMeter;
   }
   public Odometer getOdoMeter() {
       return odoMeter;
   }
   public double getFuel() {
       return fuel;
   }
   public void incrementAmountOffuel(double gallon) {
       this.fuel = this.fuel + gallon;
       if (this.fuel > 15) {
           this.fuel = 0;
       }
   }
   public void decrementAmountOffuel(double gallon) {
       this.fuel = this.fuel - gallon;
       if (this.fuel < 0) {
           this.fuel = 0;
       }
   }
   public void incrementCurrentMileage(double mileage) {
       this.odoMeter.mileage = this.odoMeter.mileage - mileage;
       if (this.odoMeter.mileage >= 999999) {
           this.odoMeter.mileage = 0;
       }
       double decreametedFuel = this.odoMeter.getMileage() / 24;
       setFuel(decreametedFuel);
   }
   @Override
   public String toString() {
       return "FuelGauge [fuel=" + fuel + ", odoMeter=" + odoMeter + "]";
   }
}

### Class Odometer.java ####
------------------------------------------
package chegg.Carfuelcalculation;
public class Odometer {
   public double mileage;
   public Odometer() {
   }
   public Odometer(double milegae) {
       this.mileage = milegae;
   }
   public double getMileage() {
       return mileage;
   }
   public void setMileage(double mileage) {
       if (this.mileage >= 999999) {
           this.mileage = 0;
       } else {
           this.mileage = mileage;
       }
   }
   @Override
   public String toString() {
       return "Odometer [mileage=" + mileage + "]";
   }
}

#### DemonstrateCarFuel.java #####
--------------------------------------------------
package chegg.Carfuelcalculation;
public class DemonstrateCarFuel {
   public static void main(String[] args) {
       // Initially set fuel and mileage according to 24 miles per 1 gallon.
       // here initially set 10 gallon of fuels in car so that car can run upto 240 miles.
       FuelGauge fuelGauge = new FuelGauge(10, new Odometer());
      
       // run a loop that increments the odometer until the car runs out of fuel.
       while (fuelGauge.getFuel() > 0) {
           fuelGauge.incrementCurrentMileage(24);
           System.out.println(fuelGauge.toString());
       }
   }
}

Description here we need to run main method of class DemonstrateCarFuel to demostrate the car fuel and odometer relatioship.Follwoing output will produced if we take fuel quantity = 10 gallon. you can set the intial fuel quantity while creating the instance of FuelGauge.java

Output

-----------

FuelGauge [fuel=9.0, odoMeter=Odometer [mileage=216.0]]
FuelGauge [fuel=8.0, odoMeter=Odometer [mileage=192.0]]
FuelGauge [fuel=7.0, odoMeter=Odometer [mileage=168.0]]
FuelGauge [fuel=6.0, odoMeter=Odometer [mileage=144.0]]
FuelGauge [fuel=5.0, odoMeter=Odometer [mileage=120.0]]
FuelGauge [fuel=4.0, odoMeter=Odometer [mileage=96.0]]
FuelGauge [fuel=3.0, odoMeter=Odometer [mileage=72.0]]
FuelGauge [fuel=2.0, odoMeter=Odometer [mileage=48.0]]
FuelGauge [fuel=1.0, odoMeter=Odometer [mileage=24.0]]
FuelGauge [fuel=0.0, odoMeter=Odometer [mileage=0.0]]