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

Spent 4 hours on this question. Can't figure it out. I want the answer by now.

Tried all sorts of codes. Apparently I'm missing something. While I tried my hardest to give the right answer, it doesn't work out.

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

}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I really wish I knew what you'd tried that's not working so I could better say where you're going wrong. My instinct is to say that you're probably overcomplicating this. So I'll show you my solution and walk you through it.

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

Here, we are redoing how the charge() method works. We start a while loop and use it to call the isFullyCharged method. That method is going to return a boolean value of true or false. If the method returns true, it means the battery is fully charged and the loop exits. However, if it returns false, we increment our mBarsCount by one. This will continue until the battery is fully charged.

Hope this helps! :sparkles:

Hi Jennifer, thanks for the answer. The while(!isFullyCharged()){mBarsCount ++;} wasn't the problem. Figured that out pretty quick. Somehow I missed placing it in the curly brackets of the charge method. After I missed it, I were overcomplicating it. At one point I completely rebuilded the code lol.

The way this question is put together caused the problems. Read it like 40 times. I'll have to point out English isn't my first language so getting confused is easier.