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

Amit Dhamankar
Amit Dhamankar
4,163 Points

I am still not able to solve this exercise. Our colleague gave me a Hint, but still I am not able to figure it out

Make the use of the charge method using isEmptyCharged id very confusing.

GoKart.java
public class GoKart {
  public static final int MAX_ENERGY_BARS = 8;
  private String mColor;
  private int mBarsCount;

  public GoKart(String color) {
    mColor = color;
    mBarsCount = 0;
  }

  public String getColor() {
    return mColor;
  }

  public void charge() {
    mBarsCount = MAX_ENERGY_BARS;
  }

  public boolean isBatteryEmpty() {
    return mBarsCount == 0;
  }

  public boolean isFullyCharged() {
    return mBarsCount == MAX_ENERGY_BARS;
  }

}

3 Answers

Hi Amit,

For this, you need to modify the existing charge() method. The requirement is to increment mBarsCount only while the Kart is NOT fully charged. The condition for the while loop uses isFullyCharged but you need to negate that with the not operator. That way, once the battery is fully charged, the while loop terminates. Inside the loop, just increment mBarsCount.

Your code could look something like this:

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

I hope that helps,

Steve.

Amit Dhamankar
Amit Dhamankar
4,163 Points

Hello Thanks to you Steve. I was struggling with it for 2 days. Thanks a lot

Thomas Nilsen
Thomas Nilsen
14,957 Points

Okay -

change

public void charge() {
    mBarsCount = MAX_ENERGY_BARS;
  }

to

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