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

Roger Dailey
Roger Dailey
14,887 Points

Keep getting complier errors

I am completely lost on this challenge. I have tried everything and still cannot get it correct. I try doing a while loop and it says that there is a complier error and I cannot start with a while loop and also tried starting with a if statement and it says the same thing. I NEED HELP.

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;
  }

}

1 Answer

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

Hi Roger,

First we want to set a while Loop that continues to charge the battery until it's fully charged, we can do this by

while(!isFullyCharged()) {
}

The ! operator means 'not' in java, so we are actually passing in while not fully charged, since our method returns true if it's fully charged, we're telling the compiler to continue to run this block of code until isFullyCharged returns true.

Next, in our while loop we want to incrment.

So it would look something like this

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

Putting this code in our charge method will allow you to pass the challenge.

Roger Dailey
Roger Dailey
14,887 Points

Thank you I had the code right but I was not putting within the charge method, so that is why I was getting an error message. I was trying to put it in the isFullyCharged method. Thank you again for the help.