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

Zachary Martin
Zachary Martin
3,545 Points

I don't know what I'm doing wrong (isFullyCharged)

I thought I was getting close when it told me I forgot the ! but after I added that it just told me to try again in the error message.

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() {
    while (!isFullyCharged()) {
    mBarsCount++;}
    return mBarsCount == MAX_ENERGY_BARS;
  }

}

1 Answer

andren
andren
28,558 Points

The code you have written is actually correct but you have placed it in the wrong method, the task is asking you to change how the charge method is implemented, not the isFullyCharged method.

In fact by placing that while loop inside the isFullyCharged method you have created an infinite recursive loop, since the loop will call isFullyCharged which will run the loop calling isFullyCharged which will run the loop again and so on and so on.

Take the while loop and put it into the charge method instead (removing the code that is already there) and you will solve the challenge.