Alright so I have a tester for this class and it keeps saying this message:
"Now testing your drive method:
*** A car with mileage of 700 miles driving 400 miles before running out of gas should not now have mileage of 1700.0 miles"

However when I remove the mileage or gasNeeded or gasInTank it will say the one that is there it is working, but not both!

Any help is appreciated!!

public class Car
{    
    private double mpg;
    private double mileage;
    private double tankCapacity;
    private double gasInTank;
    /**
    Constructs a car.
    @param miles miles per gallon that a car gets
    @param milesOnCar total mileage on car
    @param gasTankHolds amount of gas the tank holds
    @param gasTankHas gas in tank now
     */
    public Car(double miles, double milesOnCar, 
    double gasTankHolds, double gasTankHas)
    {
        mpg = miles;
        mileage = milesOnCar;
        tankCapacity = gasTankHolds;
        gasInTank = gasTankHas;
    }
    /**
    Returns the current mileage on the car.
    @return mileage on car
     */
    public double getMileage()
    {
        return mileage;
    }
    /**
    Returns the amount of gas in the gas tank.
    @return amount of gas in tank
     */
    public double getGasInTank()
    {
        return gasInTank;
    }
    /**
    Returns the gas needed to drive miles (based on mpg)
    @param numMiles the number of miles to be driven
    @return gas needed to drive numMiles miles
     */
    public double gasNeeded(double numMiles)
    {
        double gasNeeded = numMiles/mpg ; 
        if(numMiles == 0)
        {
            return 0 ; 
        }
        else if(mpg == 0)
        {
            return 0 ;
        }
        else
            return gasNeeded ; 
    }
    /** 
    Returns true if the car has enough gas in the tank to
    drive numMiles, otherwise returns false.
    @param numMiles miles to be driven
    @return true if gas in tank is enough to drive numMiles
    miles; false otherwise.
     */
    public boolean enoughGas(double numMiles)
    {    
        double gasNeeded = numMiles/mpg ; 
        if(gasInTank == gasNeeded)
        {
            return true ; 
        }
        else
{
            return false;  
}
}
    /**
    If tank is less than half full, fills tank and updates
    gasInTank.  Otherwise does nothing.
     */
    public void getGas()
{ double gas2 = 1.0/2.0 * tankCapacity ; 
        if(gasInTank <= gas2) 
{
            gasInTank = tankCapacity ; 
}
}
    /**
    Updates mileage and gasInTank to reflect numMiles
    being driven.
    @param numMiles number of miles driven
     */ /**
    Updates mileage and gasInTank to reflect numMiles
    being driven.
    @param numMiles number of miles driven
     */
    public void drive(double numMiles)
{
double gasNeeded = numMiles/mpg ; 
if(numMiles != 0 )
{ 
    mileage = mileage + numMiles ; 
}
 if(gasInTank> gasNeeded)
{
gasInTank = gasInTank - gasNeeded ; 
}
if(gasInTank < gasNeeded){
    gasInTank = 0 ; 
}

}
}

I've just posted in your other topic on that class, pointing out several issues with your code. (The thread you should have posted this in, btw, since having what is basically one question in several threads is confusing).

I'm not going to copy paste it here, but just add a link.

public void drive(double numMiles)
{
double gasNeeded = numMiles/mpg ; 
if(numMiles != 0 )
{ 
    mileage = mileage + numMiles ; 
}
 if(gasInTank> gasNeeded)
{
gasInTank = gasInTank - gasNeeded ; 
}
if(gasInTank < gasNeeded){
    gasInTank = 0 ; 
}
}

as pointed out in my other post, this method is an Exception in the making.
You've added code that updates gasInTank, which is good, but there is a flaw in your logic, so for now I'll focus on that.

 if(gasInTank> gasNeeded)
{
gasInTank = gasInTank - gasNeeded ; 
}
if(gasInTank < gasNeeded){
    gasInTank = 0 ; 
}

First thing to remember, and this is quite important, this check has to be done before you update the mileage. After all, the way your code is now, if you try to drive 500 miles, but only have gas for 50, still, your mileage will say you drove 500 miles.
Either your car should update the mileage with the exact amount capable of driving, or the car shouldn't drive when it doesn't have enough gas. Just because it's simpler, I'll go with the second solution.

As I explained in the other thread, none of this code should run if the numMiles is 0 (or a negative number, for that matter).

public void drive(double numMiles){
// numMiles must be > 0. no miles? the car doesn't drive
if ( numMiles > 0 ){
    double gasNeeded = numMiles/mpg ; 
    // here, verify whether or not we have enough gas. if we don't, don't drive
    if ( gasNeeded >= this.gasInTank) {
        mileage = mileage + numMiles;
        // this will set the right value for both gasInTank == gasNeeded and for gasInTank > gasNeeded
        gasInTank = gasInTank - gasNeeded ; 
    }
  }
}

In your current code, it seems that with just 1unit of gas in your tank, (or even less) you can drive any possible distance, without keeping track of this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.