Mike
Mike

Reputation: 31

Simulating a Car (Java)

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

Answers (3)

kma
kma

Reputation: 72

Here is the design of your car simulator application :

  • Identify Car class which will have odometer reading, current fuel inside tank etc as the instance variables.
  • Write a thread which continuously run with some sleep time of 100 millis or so for every iteration and inside the thread's run method you deal with the logic of incresing the odometer reading and decreasing the fuel in some proportion. make sure that your thread will run till the fuel in tank is greater than 0. if in case you can raise an event or alarm just in cse the fuel is below a certain constant.
  • Write the main class to initiate the class with full tank fuel (may be 40 ltrs a constant) and odometer reading to 0 and then start the thread.

Hope this is helpful.

-KishoreMadina

Upvotes: 0

Kneel-Before-ZOD
Kneel-Before-ZOD

Reputation: 4221

Well, here are some suggestions:

  • Start the car.
  • Pull out from your driveway; if that's not needed, start driving
  • If you plan to drive at a fixed speed, you can calculate how long the ride would take in advantage, and just use a loop to update the distance and fuel; otherwise, you can store a set of speeds in an array, use a loop, and pass the variable speeds on each iteration (this might be a little hard to calculate how much fuel is left)



Hope that helps the inspiration running.

Upvotes: 1

jco.owens
jco.owens

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

Related Questions