开发者

Lesson 16 - Gas Mileage - Multiple Classes Project

~~~Update: Solved! Thanks everyone!~~~

I'm working on a project from the Blue Pelican Java book, lesson 16 project Gas Mileage. It asks to create two classes, one is Automobile which holds the methods I will work with. The other class, Tester, is the main class. Every time I run the Tester class, it returns a value of -Infinity. I can't figure out why, other than that I've singled out the problem is in the Automobile class at line 14 in the takeTrip method. When I leave that method out of the Tester class, it returns the correct values. This is the Automobile class:

public class Automobile
{
    public Automobile(double m) // Accepts value m 开发者_JAVA技巧to the double mpg. Also declares
    {
        double mpg = m;
        double gallons;
    }
    public void fillUp(double f) // Adds fuel to the tank
    {
        gallons += f;
    }
    public void takeTrip(double t) // Takes away fuel from the tank depending upon how many miles are driven
    {
        gallons -= t / mpg; // Not sure how to do this line. For some reason, when I reference mpg, the output of Tester is "-infinity". Shouldn't it do gallons = gallons - (miles driven / mpg)?
    }
    public double reportFuel() // Returns value of how much fuel is left in tank
    {
        double r = gallons;
        return r;
    }
    public double mpg;
    public double gallons;
}

And this is the Tester class:

public class Tester
{
    public static void main(String args[])
    {
        Automobile myBmw = new Automobile(24); // Passes constructor argument of 24 mpg
        myBmw.fillUp(20); // Adds 20 gallons to fillUp method
        myBmw.takeTrip(100); // Takes away the fuel used for 100 miles using the takeTrip method
        double fuel_left = myBmw.reportFuel(); // Initializes fuel_left to the method reportFuel
        System.out.println(fuel_left);
    }
}

Any help is appreciated, thanks! -AJ


You constructor doesn't need the 'double' identifier. Here you are creating a new variable also called mpg, which is forgotten after the constructor completes. Instead use this:

public Automobile(double m) // Accepts value m to the double mpg. Also declares
{
    mpg = m;
}


This is the problem:

public Automobile(double m) // Accepts value m to the double mpg. Also declares
{
    double mpg = m;
    double gallons;
}

This is declaring a local variable called mpg, rather than changing the value of the instance variable for the object. (The instance variables are declared at the bottom of the class.) It's then declaring another local variable called gallons which isn't assigned any value. At the end of the constructor, the instance variables gallons and mpg will both be zero - which means you'll be dividing by zero in the takeTrip method - so you're subtracting "infinity gallons" from the fuel tank, leading to your final result. Your constructor should look like this instead:

public Automobile(double m)
{
    this.mpg = m;
}

or just:

public Automobile(double m)
{
    mpg = m;
}

If you're still unsure about local variables and instance variables after this, see if there's anything earlier in the book which might help you. (By lesson 16 I'd expect it to have been covered...)


In your Automobile c'tor, you're currently creating a local variable called mpg, instead of changing the class member. All there should be in that function is mpg = m; (the second line does nothing).

Currently, mpg (the member) is automatically initialized to 0, and then t/mpg is infinity, and when you take that away from some finite number, you get -infinity.

By the way, in reportFuel(), you could just as well just write return gallons;, without declaring r.


public class Automobile {    

    private double mpg = 0; //need to handle the division by 0 if you
                            //initialize it to 0
    private double gallons = 0;

    public Automobile(double m, double g)      
            {        
                 mpg = m;         
                 gallons = g;
             } 


Why are again declaring your attributes inside your constructor where as you have already declared them in your class. Actually the attributes you are declaring inside the constructor will not persist after the execution of the method ends (in this case the constructor). So though you are trying to initialize the attributes of your class you are actually not doing this.

So in your constructor try this

mpg=m
gallons=0

I think the other methods are fine. Another thing try to keep those attributes (mpg and gallons) private. Though the program will run without any error still you are violating the main thing of oop - encapsulation. cheers :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜