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

Help, is my format wrong for this question? How do I set this up?

I tried to do this code challenge about a week ago and I couldn't do it, and I still can't seem to figure out what I am doing wrong. When I look at the "Preview" I get a huge amount of errors, which makes me think that my format or general set up is totally wrong. Is there a certain order these go in? How different is it from the video we just watched? I tried reversing the charging/fill since the pez example is emptying while the go kart is filling. Seriously. I need help with this, my coding may look horrible but I have no idea what I am doing. Please, any help is greatly appreciated!

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

  while (charger.charge()) {
    System.out.println ("Charging...");
  }

  if (charge.isFullyCharged()) {
  System.out.println ("Gokart is fully charged!");
  }

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

}

1 Answer

All you need to do in your charge() method is first delete or comment out the line of code they have in there and then do a while loop that will increment the mBarsCount: while(!isFullyCharged()) returns true. Notice that I had a '!' in there, basically saying that while the battery is NOT fully charged increment the mBarsCount or mBarsCount++.

Just the two lines of code is all you need.

I hope this helps.