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 Throwing Exceptions

Christopher Borchardt
Christopher Borchardt
2,908 Points

Not sure what I have wrong

I keep getting an error to make sure to throw the exception before changing mBarsCount, but I'm pretty sure that I am, the mBarsCoutn variable isn't changed till after the if statement

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 String getColor() {
    return mColor;
  }

  public void drive() {
    drive(1);
  }

  public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    int newAmount = mBarsCount -= laps;
    if (newAmount <0){
      throw new IllegalArgumentException("Not enough battery remains!");
    }
    mBarsCount=newAmount;
  }

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

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

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

}

3 Answers

Jordan George
Jordan George
9,926 Points

I don't think -= is a real thing in this context.

Use this line for the if statement:

if(mBarsCount == 0)

or

int newAmount = mBarsCount - laps;
if(newAmount < 0)

Happy coding :)

Christopher Borchardt
Christopher Borchardt
2,908 Points

thank you, i missed the = from when i had that as its own line, take that out and it worked fine.

Steve Goldman
Steve Goldman
2,345 Points

1) I don't think you have to make a new int=newAmount. You can just say mBarsCount -=laps.

2) I think what he wants is that before you minus the laps from the bar count you should check if the battery is zero. Meaning let's check if the battery has charge in it before we start driving and subtracting bars

Hope that helps