我正在做一个模拟汽车的项目。要求是通过给汽车加注燃料来证明汽车的运行,然后让汽车运行直到它没有更多的燃料。模拟以不同速度填充和运行汽车的过程。在汽车运行时,定期打印汽车的当前里程、燃油量和速度。
我编写了一些其他类来保存一些我将用来计算燃料、速度和里程的方法。我只是在如何让它像一辆真正的汽车一样工作时遇到了一点麻烦,任何帮助都将不胜感激。
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--;
}
}
公共类里程表扩展 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;
}
}
公共类汽车扩展里程表{
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--;
}
}