Question: [JAVA] [FULL CODE GIVEN] I'm looking for an answer that doesn't use private FuelGauge fuelGauge; Car Instrument Simulator Design tow classes that work together to
[JAVA] [FULL CODE GIVEN]
I'm looking for an answer that doesn't use
private FuelGauge fuelGauge;
Car Instrument Simulator
Design tow classes that work together to simulate a car's fuel gauge and odometer. The classes you will design are the following:
1. The FuelGauge Class: This class will simulate a fuel gauge. Its responsibilities are:
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 one gallon. This simulates putting fuel in the car. (The car can hold a maximum of 15 gallons).
To be able decrement the amount of fuel by one gallon, if the amount of fuel is greater than zero gallons. This simulates burning fuel when the car's engine is running.
2. The Odometer Class: This class will simulate the car's odometer. Its responsibilities are:
To know the car's current mileage.
To report the car's current mileage.
To be able to increment the current mileage by one mile. the maximum mileage on the odometer is 99,999 miles. When this amount is exceeded, the odometer resets the current mileage to 0.
To be able to work with the a FuelGauge object. It should decrease the FuelGauage object's current amount of fuel by one gallon for every 24 miles the car has traveled. (This addresses 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 out the car's mileage and amount of fuel.
Sample Output:
.
.
.
Mileage: 310 Fuel level: 3 gallons ------------------------------ Mileage: 311 Fuel level: 3 gallons ------------------------------ Mileage: 312 Fuel level: 2 gallons ------------------------------ Mileage: 313 Fuel level: 2 gallons ------------------------------ Mileage: 314 Fuel level: 2 gallons ------------------------------ Mileage: 315 Fuel level: 2 gallons ------------------------------ Mileage: 316 Fuel level: 2 gallons ------------------------------ Mileage: 317 Fuel level: 2 gallons ------------------------------ Mileage: 318 Fuel level: 2 gallons ------------------------------ Mileage: 319 Fuel level: 2 gallons ------------------------------ Mileage: 320 Fuel level: 2 gallons ------------------------------ Mileage: 321 Fuel level: 2 gallons ------------------------------ Mileage: 322 Fuel level: 2 gallons ------------------------------ Mileage: 323 Fuel level: 2 gallons ------------------------------ Mileage: 324 Fuel level: 2 gallons ------------------------------ Mileage: 325 Fuel level: 2 gallons ------------------------------ Mileage: 326 Fuel level: 2 gallons ------------------------------ Mileage: 327 Fuel level: 2 gallons ------------------------------ Mileage: 328 Fuel level: 2 gallons ------------------------------ Mileage: 329 Fuel level: 2 gallons ------------------------------ Mileage: 330 Fuel level: 2 gallons ------------------------------ Mileage: 331 Fuel level: 2 gallons ------------------------------ Mileage: 332 Fuel level: 2 gallons ------------------------------ Mileage: 333 Fuel level: 2 gallons ------------------------------ Mileage: 334 Fuel level: 2 gallons ------------------------------ Mileage: 335 Fuel level: 2 gallons ------------------------------ Mileage: 336 Fuel level: 1 gallons ------------------------------ Mileage: 337 Fuel level: 1 gallons ------------------------------ Mileage: 338 Fuel level: 1 gallons ------------------------------ Mileage: 339 Fuel level: 1 gallons ------------------------------ Mileage: 340 Fuel level: 1 gallons ------------------------------ Mileage: 341 Fuel level: 1 gallons ------------------------------ Mileage: 342 Fuel level: 1 gallons ------------------------------ Mileage: 343 Fuel level: 1 gallons ------------------------------ Mileage: 344 Fuel level: 1 gallons ------------------------------ Mileage: 345 Fuel level: 1 gallons ------------------------------ Mileage: 346 Fuel level: 1 gallons ------------------------------ Mileage: 347 Fuel level: 1 gallons ------------------------------ Mileage: 348 Fuel level: 1 gallons ------------------------------ Mileage: 349 Fuel level: 1 gallons ------------------------------ Mileage: 350 Fuel level: 1 gallons ------------------------------ Mileage: 351 Fuel level: 1 gallons ------------------------------ Mileage: 352 Fuel level: 1 gallons ------------------------------ Mileage: 353 Fuel level: 1 gallons ------------------------------ Mileage: 354 Fuel level: 1 gallons ------------------------------ Mileage: 355 Fuel level: 1 gallons ------------------------------ Mileage: 356 Fuel level: 1 gallons ------------------------------ Mileage: 357 Fuel level: 1 gallons ------------------------------ Mileage: 358 Fuel level: 1 gallons ------------------------------ Mileage: 359 Fuel level: 1 gallons ------------------------------ Mileage: 360 Fuel level: 0 gallons ------------------------------ Press any key to continue . . .
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; } }
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("------------------------------"); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
