Reputation: 31
I'm working on a project to simulate a car. The requirements are to demonstrate the operation of a car by filling it with fuel and then run the car until it has no more fuel. Simulate the process of filling and running the car at different speeds. As the car is running, periodically print out the car’s current mileage, amount of fuel and speed.
I wrote some other classes to hold some methods that I will use to calculate the fuel, speed, and mileage. I'm just having a little trouble on how I should go about making it work like an actual car would, any help would be appreciated.
public class FuelGauge {
protected double fuel;
public FuelGauge()
{
fuel = 0.0;
}
public double getFuel()
{
return fuel;
}
public void setFuel(double fuel)
{
this.fuel = fuel;
}
public void fuelUp()
{
if(fuel<18)
fuel++;
}
public void fuelDown()
{
if(fuel>0)
fuel--;
}
}
public class Odometer extends FuelGauge {
private int mileage, mpg;
private int economy;
public int getMileage()
{
return mileage;
}
public void setMileage(int mileage)
{
this.mileage = mileage;
}
public int getMpg()
{
return mpg;
}
public void setMpg(int mpg)
{
this.mpg = mpg;
}
public void mileUp()
{
if(mileage<999999)
mileage++;
}
public void mileReset()
{
if(mileage>999999)
mileage = 0;
}
public void decreaseFuel(int fuel)
{
if(mileage == mpg)
fuelDown();
}
public int getEconomy()
{
return (int) (mileage/fuel);
}
public void setEconomy(int economy)
{
this.economy = economy;
}
}
public class Car extends Odometer{
private String name;
private int speed;
public Car()
{
name = "Car";
getMileage();
getMpg();
getEconomy();
getFuel();
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getSpeed()
{
return speed;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
public void increaseSpeed()
{
if(speed<=120)
speed++;
}
public void decreaseSpeed()
{
if(speed>0)
speed--;
}
}
Upvotes: 1
Views: 3852
Reputation: 72
Here is the design of your car simulator application :
Hope this is helpful.
-KishoreMadina
Upvotes: 0
Reputation: 4221
Well, here are some suggestions:
Hope that helps the inspiration running.
Upvotes: 1
Reputation: 535
I would more recommend the contains vs isa relationship for the components of your car.
class FuelGauge { ... }
class Odometer { ...}
class Vehicle { ... }
class Car extends Vehicle
{
private FuelGauge fuelGauge = new FuelGauge();
private Odometer odometer = new Odometer();
...
}
Upvotes: 5