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

Totally stuck on Incrementing challenge for GoKart Code

Hi there,

I'm working on the GoKart challenge and I'm on the increment challenge with 1 task. I am suppose to make it so the battery of the GoKart will only charge until it reports being fully charged. I'm supposed to use the ! symbol and a while loop, then inside the loop I increment mBarsCount. Here is my code if that helps. Thank you!

Gabriel

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

  if (!mBarsCount.isEmpty()) {
      }

  while (GoKart.charge()) {
      }

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

}

3 Answers

never mind bro, i got it!

Well it would be nice if you would share how you completed it. It could help others to. :)

Well it would be nice if you would share how you completed it. It could help others to. :)

Chase Marchione
Chase Marchione
155,055 Points

Hi Gabriel,

1) Your current code asks the compiler to attempt to execute if and while statements as if they are self-contained methods. Java syntax does not consider them to be, so we know we'll want to include those kinds of statements inside of a method, rather than write them as if they are methods themselves.

2) The challenge asks us to alter the charge method; thus, our new code will go within that method.

3) We do not need an if statement at all, but we do need a while statement. The reason for the while statement is that we want the increment statement to run until the GoKart is fully charged (a way to do this is by having code that increases the GoKart's charge until the boolean isFullyCharged is true.) Once that object is fully charged, we can stop running that statement. The while test will no longer pass when the GoKart is fully charged, so the new charge method code that we are adding will stop running then, right when we need it to.

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;

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

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

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

Hope this helps!

Thank you very much for the help CJ, but when I copied and pasted the code you sent to me, it still showed me a long list of syntaxes. I'm still a bit confused. Would you have any suggestions?

eric Crawford
eric Crawford
2,381 Points

Thanx CJ for explaining. The reference to the "KISS" sticky note makes me think we were intended to start figuring things out that we havent exactly done yet after completing this challenge. From here on out i will not assume we have covered or seen examples of the challenges we need to complete.

Not gonna lie this challenge made me mad, and FORCED me to accept that i failed yaaay! I felt like the the question it'self was written in code. 1.5 hours on 1 task.......could have been done with this whole section by now. Once again thanx.

Thank you CJ. It wasn't working for me, but when I saw your solution, it became apparent that I had put the while (!isFullyCharged()) { mBarsCount++; too far down in the list. Woohoo. Onward to infinity!!

Thank you very much for the help CJ, but when I copied and pasted the code you sent to me, it still showed me a long list of syntaxes. I'm still a bit confused. Would you have any suggestions?