Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Harnessing the Power of Objects Incrementing

Daniel Makafui Ahiatrogah
Daniel Makafui Ahiatrogah
3,044 Points

a function to charge a battery until it is fully charged and the function return it is fully charged

Okay, so lets use our new isFullyCharged helper method to change our implementation details of the charge method. Let's make it so it will only charge until the battery reports fully charged. Let's make the ! symbol and a while loop inside the loop increment mBarsCount.

1 Answer

Hi Daniel,

The isFullyCharged method is a helper method designed to give you quick access to the GoKart's charge state. It returns true or false, as you would expect. It does not charge the GoKart instance. That's what the charge() method does for you.

So, you use the isFullyCharged() method to see if your GoKart instance needs to use the charge method.

Let's look at the question as this gives us lots of hints how to tackle this one:

Okay, so let's use our new isFullyCharged helper method to change our implementation details of the charge method. Let's make it so it will only charge until the battery reports being fully charged. Let's use the ! symbol and a while loop. Inside the loop increment mBarsCount.

That's quite a lot ... what it is saying is: In the charge method, while the GoKart not isFullyCharged() do mBarsCount++. That's not very readable! Let's do that in code:

  public void charge() {
    while(!isFullyCharged()){
      mBarsCount++;
    }
  }

And that's it. We modify the charge() method. First, we enter a while loop. This loops constantly while the condition !isFullyCharged() is true. So it loops while the GoKart isn't fully charged. Inside the loop, it increments the mBarsCount variable. On the next loop, it checks isFullyCharged() again, if it isn't fully, charged, the loop runs, if it is fully charged, the method exits.

Make sense?

Steve.

No problem! :-)

Peng Liu
Peng Liu
Courses Plus Student 14,273 Points

Can't imagine it is so simple, sometimes, overthinking is not a good thing at all.

Appreciated,

Peng - another guy got stuck

Hey, Peng. As long as you got through it, you learned. And that's cool. We all learn everyday from various people and places. That's cool.

Steve.

Peng Liu
Peng Liu
Courses Plus Student 14,273 Points

Hi, Steve. Thanks for that and encouragement, keep it in mind already.

Peng.