Has To be in Java For this assignment, you will design a set of classes that wor
ID: 3767625 • Letter: H
Question
Has To be in Java
For this assignment, you will design a set of classes that work together to simulate a
cars 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 I 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 I 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 travled. ( The cars fuel economy is 24 miles per gallon)
--Demonstrate the classes by creating instances of each. Simulate filing the car up with the fuel, and then run a loop that increments the odometer until the car runsout of fuel. During each loop iteration, print the car's current milage and amount of fuel.
--The Ouput-- should look in this format
Milage: xxxx
Fuel level: # of gallons
(This format should be repeated)
Explanation / Answer
/**
* The java program CarSimulation that simulates the
* car fuel . Prints fuel and mileage until
* the car run out of fuel.
* */
//CarSimulation.java
public class CarSimulation
{
public static void main(String[] args)
{
//set fuel 10
int FUEL=10;
//Create an instance of FuelGauge with FUEL=10
FuelGauge fuel=new FuelGauge(FUEL);
//Create an instance of Odometer
Odometer odometer=new Odometer();
//Set a boolean variable to false
boolean runOutFuel=false;
//Run the while loop until boolean variable, runOuFuel to true
while(!runOutFuel)
{
//Call increment method
odometer.increment();
//Call getMileage that decrements the fuel
//for every 24 miles
if(odometer.getMileage()%24==0)
fuel.decrement();
//Check if number of gallons are zero
if(fuel.getGallons()==0)
{
//Set boolean variable runOutFuel to true
runOutFuel=true;
//print mileage
System.out.println("Milage:"+odometer.getMileage());
//print gallons
System.out.println("Fuel level:#"+fuel.getGallons()+" of gallons");
}
else
{
System.out.println("Milage:"+odometer.getMileage());
System.out.println("Fuel level:#"+fuel.getGallons()+" of gallons");
}
}
}//end of main method
}//end of the class
-----------------------------------------------
/**
* The class FuelGauge that resembles the FuelGauge object
* that contains fuel tank
* */
//FuelGauge.java
public class FuelGauge
{
//Set max number of gallons as 15
private final int MAX_GALLONS=15;
private int fuel;
//Constructor to set fuel
public FuelGauge(int fuel)
{
this.fuel=fuel;
}
//Returns gallons
public int getGallons()
{
return fuel;
}
//Increment the fuel by one of fuel is less than number of gallons
public void increment()
{
if(fuel<=MAX_GALLONS)
fuel++;
}
//Decrement the fuel by one.
public void decrement()
{
if(fuel>0)
fuel--;
}
}
-------------------------------------------------------------
/**
* The Odometer class that ressembles Odometer
* object that contains mileage
* */
//Odometer.java
public class Odometer
{
//Set maximum mileage is 999999
private int MAX_MILEAGE=999999;
private int mileage;
public Odometer()
{
mileage=0;
}
//Returns mileage
public int getMileage()
{
return mileage;
}
//increment the mileage by one if mileage
//is less than 999999
public void increment()
{
if(mileage<MAX_MILEAGE)
mileage++;
}
//Decrement the mileage by one if mileage is greater than zero
public void decrement()
{
if(mileage>0)
mileage--;
}
}
---------------------------------------------------------------------
Sample Outptut:
Milage:1
Fuel level:#10 of gallons
Milage:2
Fuel level:#10 of gallons
Milage:3
Fuel level:#10 of gallons
Milage:4
Fuel level:#10 of gallons
Milage:5
Fuel level:#10 of gallons
Milage:6
Fuel level:#10 of gallons
Milage:7
Fuel level:#10 of gallons
Milage:8
Fuel level:#10 of gallons
Milage:9
Fuel level:#10 of gallons
Milage:10
Fuel level:#10 of gallons
Milage:11
Fuel level:#10 of gallons
Milage:12
Fuel level:#10 of gallons
Milage:13
Fuel level:#10 of gallons
Milage:14
Fuel level:#10 of gallons
Milage:15
Fuel level:#10 of gallons
Milage:16
Fuel level:#10 of gallons
Milage:17
Fuel level:#10 of gallons
Milage:18
Fuel level:#10 of gallons
Milage:19
Fuel level:#10 of gallons
Milage:20
Fuel level:#10 of gallons
Milage:21
Fuel level:#10 of gallons
Milage:22
Fuel level:#10 of gallons
Milage:23
Fuel level:#10 of gallons
Milage:24
Fuel level:#9 of gallons
Milage:25
----------------(continuation)
Fuel level:#1 of gallons
Milage:238
Fuel level:#1 of gallons
Milage:239
Fuel level:#1 of gallons
Milage:240
Fuel level:#0 of gallons
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.