title: Java car class for beginner
body: <p>I must Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gas 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, reducing the amount of gasoline in the fuel tank. Also supply methods getGasInTank, returning the current amount of gasoline in the fuel tank, and addGas, to add gasoline to the fuel tank.</p>
  
  <p>I have created a class for the car and a test program to plug some values in and when i run the program all i get returned is the addGas value that i put in. the computation for the miles per gallon will not run and i do not understand why. as you can probably tell i am VERY new at java and any help on the issue is much appreciated.</p>
  
  <pre><code>    public class CarTest
  {
  public static void main(String[] args)
    {
      Car richCar = new Car(49);
  
      richCar.addGas(15);
      richCar.drive(150);
      System.out.println(richCar.getGas());
    }
  }
  
  
  
  
  
      /**
  A car can drive and consume fuel
  */
  public class Car
  {
  /**
    Constructs a car with a given fuel efficiency
    @param anEfficiency the fuel efficiency of the car
  */
  public Car(double mpg)
  {
    milesPerGallon = mpg;
    gas = 0;
    drive = 0;
  }
  
   /** Adds gas to the tank.
     @param amount the amount of fuel added
   */
   public void addGas(double amount)
   {
    gas = gas + amount;
   }
  
   /**
     Drives a certain amount, consuming gas
     @param distance the distance driven
   */
   public void drive(double distance)
   {
    drive = drive + distance;
   }
  
   /**
     Gets the amount of gas left in the tank.
     @return the amount of gas
    */
  
   public double getGas()
   {
    double mpg;
    double distance;
    distance = drive;
    mpg = gas * milesPerGallon / distance;
    return gas;
   }
  
    private double drive;
    private double gas;
    private double milesPerGallon;
  }
  </code></pre>