So I am having trouble with the last bit of the car class, I have to update the number of gallons in the car based on the number of miles driven...
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
*/
public void drive(double numMiles)
{
double gasNeeded = numMiles/mpg ;
if(numMiles != 0 )
{ mileage = mileage + numMiles ;
}
}
}