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

Trying to use a helper method to change implementation details for a method. I need to use ! and a while loop.

I have tried several different lines of code that points to how I have currently written my code. What am I missing?

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

}

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi, Alandus James , isFullyCharged as helper function has been provided to you by the code challenge, and you should use it as it is and don't make any modification to it.

Change the implementation of charge() function instead to make use of the helper function.

  public void charge() {
    while (!isFullyCharged()) {
      mBarsCount++;
    }
  }
Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

You use isFullyCharged to check and see if the battery is fully charged, it shouldn't do anything, because you didn't tell it to. You never want to assume how someone is going to use your code, so you don't want to create side effects.

Thank you for your responses. I think I'm starting to get the understanding of this now. I have learned so much from your videos and with you answering my questions as I need them is like your right there with me. Thanks again.

Thanks, that's what I end up doing. But I'm not understanding why this is the correct answer verse changing isFullyCharged method.