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 Helper Methods

Livia Lulushi
Livia Lulushi
1,777 Points

"Bummer! Your method should return true when mBarsCount is full."

I am confused as to what in my code is preventing the program from recognizing that mBarsCount has been set to the max level of 8. When the line "public void charge() { } runs, I believed that it had loaded/charged the battery up to 8 bars. So the subsequent helper method should see that mBarsCount is now full, and return "true" consequentially. But apparently it is not recognizing that mBarsCount has been filled. Where did I go wrong?

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

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

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

  public String getColor() {
    return mColor;
  }

  public void charge() {
    mBarsCount = MAX_BARS;
  }

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

}

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Can you restate what your isFullyCharged method says in English, please? (I think that'll help)

Livia Lulushi
Livia Lulushi
1,777 Points

I might have missed something in the video, but my understanding is that the helper method isFullyCharged assigns a boolean value to mBarsCount. If I type, "return mBarsCount == 1", I think the program creates an identity statement "mBarsCount is fully charged."

Although, I am confused by the isBatteryEmpty() line as well. The line of code that charges the battery follows it, so if we say that "mBarsCount == 0" which I think is saying "the battery is not empty" (which it should be at this point because it has been initialized to 0 in the preceding code), why will public void charge() still be sequenced if it's reading the statement "I need to be charged" as a false statement? I think I must be misinterpreting the code.

Craig Dennis
Craig Dennis
Treehouse Teacher

Aha! Double equals == is used to check equality and it returns a boolean. In English this is saying "to check if the battery is fully charged, see if the number of bars is 1". You want that to state is equal to the maximum number of bars.

Make sense?

Livia Lulushi
Livia Lulushi
1,777 Points

Thank you, this clarifies things for me a lot. With this in mind, I will try the task again and update you on if I have success.