The Odometer class that resembles Odometer object that co
/*********************************
* The Odometer class that resembles Odometer
* object that contains mileage. Set maximum
* mileage is 999999999
***********************************/
//Odometer.java
public class Odometer
{
//Set maximum mileage is 9999999
private int MAX_MILEAGE-999999;
private int mileage;
//constructor to set mileage to zero
public Odometer()
{
mileage=0;
}
//Returns milegae
public int getMileage()
{
return mileage;
}
/*increment the mileage by one if mileage is less thzn 99999*/
public void increment()
{
if(mileage)
{
mileage++;
}
}
/* Decrement the mileage by one if mileage is greater than zero */
public void decrement()
{
if (mileage>0)
mileage--;
}
}
-----------------------------------------------------------------
/********************************
* The class FuelGauge that resembles the FuelGauge
* object that contains fuel tank.The tank capacity
* is 15 gallons. The increment the fuel*
*******************************/
package fuelgauge;
//FuelGauage.java
public class FuelGauge
{
//Set max number of gallons as 15
private final int MAX_GALLONS=15;
private final int fuel;
//Contuctor 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--;
}
}
------------------------------------------------------------------------
//CarSimulation.java
public class CarSimulation
{
public static void main(String[]args)
{
// Assume car fuel tank has of 10 gallons
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;
System.out.println(\"Fuel:\"+FUEL);
/*Run the while loop until boolean varible
run Out fuel 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.getGallon()==0)
{
//Set boolean variable runOutFuel to true
runOutFuel= true;
//print milleage
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\");
}
}
System.out.println(\"Car run out of fuel\");
System.out.println(\"Car travelled\"+odometer.getMileage()+\"mile\");
}
}
I have a problem with this code
Solution
few changes made to work your code
1)private int MAX_MILEAGE-999999; replace - with =
2)if(mileage) to if(this.mileage)
3)if (mileage>0) to if (this.mileage>0)
4)private final int fuel; remove final as final variables cannot be modified
5)if (fuel.getGallon()==0) to if (fuel.getGallons()==0)
and comment unwanted code
/*********************************
* The Odometer class that resembles Odometer
* object that contains mileage. Set maximum
* mileage is 999999999
***********************************/
//Odometer.java
class Odometer
{
//Set maximum mileage is 9999999
private int MAX_MILEAGE=999999;
private int mileage;
//constructor to set mileage to zero
public Odometer()
{
mileage=0;
}
//Returns milegae
public int getMileage()
{
return mileage;
}
/*increment the mileage by one if mileage is less thzn 99999*/
public void increment()
{
if(this.mileage<MAX_MILEAGE)
{
mileage++;
}
}
/* Decrement the mileage by one if mileage is greater than zero */
public void decrement()
{
if (this.mileage>0)
mileage--;
}
}
// -----------------------------------------------------------------
/********************************
* The class FuelGauge that resembles the FuelGauge
* object that contains fuel tank.The tank capacity
* is 15 gallons. The increment the fuel*
*******************************/
//package fuelgauge;
//FuelGauage.java
class FuelGauge
{
//Set max number of gallons as 15
private final int MAX_GALLONS=15;
private int fuel;
//Contuctor 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--;
}
}
//------------------------------------------------------------------------
//CarSimulation.java
public class CarSimulation
{
public static void main(String[]args)
{
// Assume car fuel tank has of 10 gallons
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;
System.out.println(\"Fuel:\"+FUEL);
/*Run the while loop until boolean varible
run Out fuel 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 milleage
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\");
}
}
System.out.println(\"Car run out of fuel\");
System.out.println(\"Car travelled\"+odometer.getMileage()+\"mile\");
}
}






