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

Chris Polito
Chris Polito
1,028 Points

I really need help with this one

I'm confused with the "While" I need to say continue to add to mBarsCount until isFullyCharged.

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() {
    if (!mBarsCount.isFullyCharged()){
     mBarsCount++; 
    } While (mBarsCount.
  }

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

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

}

3 Answers

The while loop should be enough

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

because mBarsCount gets incremented in the while loop, until it has the right/correct value. Therefore mBarsCount does not need to be set again at the end.

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

That's right, I forgot to omit that, I think it's there by default in the code challenge.

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

The while loop iterates until the condition is no longer true. You can do it like this:

  public void charge() {

    while(!isFullyCharged()){
      mBarsCount++;
    }
    mBarsCount = MAX_ENERGY_BARS;
  }

I hope that helps a little bit