# -Infinity result

• 03-18-2012, 01:21 AM
FOX427
-Infinity result
Hi all! I am practicing my Java and I am new guy....In short doing exercise which asks to count amount of gas spent to drive the car and how much gas left in the tank. I code this:

public class Car3 {
public Car3()
{
fuelLevel=0;
}
public Car3(double efficiency, double initialFuel)
{
fuelLevel=initialFuel;
}
{
double currentGas = fuelLevel + added;
fuelLevel = currentGas;
}
public void drive(double distanceTraveled)
{
double gasSpent=distanceTraveled/efficiency;
double gasInTank = fuelLevel - gasSpent;
fuelLevel = gasInTank;
}
public double getGasInTank()
{
return fuelLevel;
}

private double efficiency;
private double fuelLevel;
}

and main:

public class Car3Tester {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Car3 myHybrid = new Car3(50, 0); //50 miles per gallon
myHybrid.drive(100); //Drive 100 miles
double gasLeft = myHybrid.getGasInTank(); //Get gas remaining in tank
System.out.println(gasLeft);
System.out.println("Expected: 18");

}

}

the result in the console as you can see:
-Infinity
Expected: 18

----------------------------------------------------------
It is not homework assignment, I am doing it for myself so if you know how to fix it then help me please, because I don't wanna continue if I cant even solve this... Thanks guys! regards.
• 03-18-2012, 01:35 AM
Junky
Re: -Infinity result
The parameter in your constructor called efficiency != to the instance variable called efficiency.
• 03-18-2012, 02:15 AM
FOX427
Re: -Infinity result
Hi Junky! Thanks for help, I already found that mistake! change only this part and now it works perfectly:

public Car3(double milesPerGallon, double initialFuel)
{
fuelLevel=initialFuel;
efficiency=milesPerGallon;
}

-----------------------------------
Thanks! ;)