Can someone help me with implementing this class?

November 8, 2009 in Programming & Design

Did you know that you can save fuel and run your car on water

fuel efficiency

hollyk321 asked:

I have to write something in java that does the following- implement class Car- the car has a certain fuel efficiency and a certain amount of fuel in the tank. The efficiency is specified in the constructor, and the initial fuel level is 0- supply a method drive that simulates driving the car for a certain distance, which reduces amount of fuel in tank. Supply another method getGasInTank, which return amount of fuel in tank, and also addGas, which should add gas to the tank-What should my code look like? Thanks!!!! Any help at all would be appreciated.

Did you know that you can save fuel and run your car on water

Comments

One Response to “Can someone help me with implementing this class?”
  1. Anh Nguyen says:

    should look something like this

    public class Car
    {
    int fuel;
    double mpg;

    public Car() //constructor
    {
    fuel = 0;
    mpg = whatever it should be;
    }

    public void drive(int miles)
    {
    fuel = fuel – (miles / mpg)
    }

    public int getGasInTank()
    {
    return fuel;
    }

    public void addGas(int gas)
    {
    fuel += gas
    }

    }

Speak Your Mind

Tell us what you're thinking...