Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6606935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:26:55+00:00 2026-05-25T19:26:55+00:00

~~~Update: Solved! Thanks everyone!~~~ I’m working on a project from the Blue Pelican Java

  • 0

~~~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 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:26:55+00:00Added an answer on May 25, 2026 at 7:26 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: Solved, with code I got it working, see my answer below for the
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add
UPDATE: Solved, i'm an idiot, thank you everyone! Okay little bit weird.. I just
Update: Thanks for the suggestions guys. After further research, I’ve reformulated the question here:
UPDATE (5/21/2010) SUCCESS! So after MUCH $head->desk()'ing, I've solved it. Remember kids, be wary
Thanks to everyone replying with ideas and alternate solutions. More efficient ways of solving
UPDATE: SOLVED!!! Please see the answer I added below Does anyone know if there
update SOLVED - the problem is not with my code. there is a design
UPDATE & SOLUTION : Everyone, for whoever has this problem in the future, I
[update2] One of those problems I solved & can't remember how... :) [update1] could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.