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

Neelesh Tewani
Neelesh Tewani
1,239 Points

I am not able to run the loop please help me with this

How to run this loop please tell me

  public boolean isFullyCharged() {
    return mBarsCount == MAX_ENERGY_BARS;
        boolean cleared = true;
    if(!isBatteryEmpty()){
      mBarsCount--;
      return cleared = false;
    }
    return cleared;
while(.isFullyCharged()){
  mBarsCount++;
  System.out.println("your battery is fully charged");

}
  }

2 Answers

Hi Neelesh,

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.

There's a similar explanation in this thread from a while ago too.

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Neelesh,

You have the right idea I think you just have a typo. Remember the not symbol is java is "!" Also you are printing out that the battery is fully charged, normally this would be fine, but erring on the side of caution I would change the parameter to check for in your while loop to from the . to the ! symbol and take out the println() statement.