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 …