Thanks for the responses. I took out where i has the variable initialized to 0, the class still compiled but I still got NaN. Here is the code I have so far.
Car Class
public class Car
{
private double gas;
private double efficiency;
public double drive;
/**
Constructs a car with a given fuel efficiency.
@param anEfficiency the fuel efficiency of the car
*/
public Car(double anEfficiency)
{
gas = 0;
efficiency = 0;
drive = 0;
}
/** Adds gas to the tank.
@param amount the amount of fuel to add
*/
public void addGas(double amount)
{
double total = gas + amount;
gas = total;
}
/**
Drives a certain amount, consuming gas.
@param distance the distance driven
*/
public void drive(double distance)
{
double drive = distance;
}
/**
Gets the amount of gas left in the tank.
@return the amount of gas
*/
public double getGasInTank()
{
double gasInTank = gas - (drive / efficiency);
return gasInTank;
}
}
and the Tester Class
public class CarTester
{
public static void main(
String [] args)
{
Car myHybrid = new Car(50); // 50 miles per gallon
myHybrid.addGas(20);
myHybrid.drive(100); // consumes 2 gallons
double gasLeft = myHybrid.getGasInTank();
// TODO: Print actual and expected gas level
System.out.print("Gas Level: ");
System.out.println(myHybrid.getGasInTank());
System.out.println("Expected: 18");
}
}