Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Junky

Thread: -Infinity result

  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default -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;
    }
    public void addGas(double added)
    {
    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.addGas(1000); // tank 20 gallons
    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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: -Infinity result

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

  3. #3
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

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

Similar Threads

  1. Infinity PFM 0.5.0 RC2
    By java software in forum Java Software
    Replies: 0
    Last Post: 03-01-2012, 05:50 PM
  2. Keep getting result of 0.
    By dookie1293 in forum New To Java
    Replies: 7
    Last Post: 06-09-2011, 05:01 AM
  3. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 10:15 AM
  4. How to format the result
    By galagali in forum New To Java
    Replies: 3
    Last Post: 07-11-2010, 12:15 AM
  5. uncorrect result
    By jamborta in forum New To Java
    Replies: 3
    Last Post: 11-11-2009, 01:17 PM

Posting Permissions

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