Thursday, July 17, 2014

Java Car Simulator

Class 1: Car
-----------------------------------

public class Car
{
public static void main(String []args)
{
FuelGauge amountOfFuel = new FuelGauge(15);
Odometer currentMileage = new Odometer(0);
while (amountOfFuel.getAmountOfFuel() > 0)
{
currentMileage.incrementcurrentMileage();
if( currentMileage.getcurrentMileage() % 24 == 0 )
amountOfFuel.decrementFeul();
{
System.out.printf("Amount Of Fuel = %s\tCurrent Mileage = %s\n",
amountOfFuel.getAmountOfFuel(), currentMileage.getcurrentMileage());
}
}
}
}


Class 2 : Fuel Gauge
-------------------------------
public class FuelGauge
{   
private int amountOfFuel;

public FuelGauge(int gallons){
 amountOfFuel = gallons;
 }
public int getAmountOfFuel(){
return amountOfFuel;
}
public void incrementFeul(){
if (amountOfFuel < 15 )
amountOfFuel++;
}
public void decrementFeul(){
if (amountOfFuel > 0 )
amountOfFuel--;
}
}


Class 3: Odometer 
----------------------------
public class Odometer 
{
 private int currentMileage;
public Odometer(int gallons)
{
  currentMileage = gallons;
}
public int getcurrentMileage()
{
return currentMileage;
}
 public void incrementcurrentMileage()
 {
if (currentMileage < 1000 )
currentMileage++;
if (currentMileage == 1000 )
currentMileage=0;
  }
public void decrementcurrentMileage()
{
if (currentMileage > 45 )
   currentMileage--;
 }
public void incrementMileage()
{
}

}

No comments:

Post a Comment