1. Member
Join Date
Jun 2011
Posts
54
Rep Power
0

## -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.

2. ## Re: -Infinity result

The parameter in your constructor called efficiency != to the instance variable called efficiency.

3. Member
Join Date
Jun 2011
Posts
54
Rep Power
0

## 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! ;)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•