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 this while loop please help me with this task

// i think at the place of gk there will be something else
  public boolean isFullyCharged() {
    return mBarsCount == MAX_ENERGY_BARS;
        boolean cleared = true;
    if(!isBatteryEmpty()){
      mBarsCount--;
      return cleared = false;
    }
    return cleared;
while(gk.isFullyCharged()){
  mBarsCount++;
  System.out.println("your battery is fully charged");

}
  }

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Neelesh;

Welcome to Treehouse!

I can see where you are going with your code, but let's take a look at what we are asked to do in this specific challenge:

Task 1

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.

Wow, that seems like a lot, but it really isn't that bad. We have our isFullyCharged() method already and it is working as we need it to as it simply returns a Boolean value (True/False) as to whether our battery is fully charged or not. Our task at hand, therefore, is to leave that method alone and work on the charge() method.

We are asked to use the ! symbol, which is shorthand for not and a while loop, right? The syntax for that in general would look like:

while ( !isFullyCharged() ) {
    // do something magical here
}

We are instructed to increase mBarsCount inside the while loop, and that can be accomplished by mBarsCount++.

To add all of that together, our charge() method should look like:

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

Post back if you are still stuck or have further questions.

Happy coding,
Ken

faraz
faraz
Courses Plus Student 21,474 Points

Loved your answer Ken, you explained it great!