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

It says I completed the assignment, but I still feel as if I did it wrong.

For this exercise I switched if(laps < 0) to if(laps > 0), Can someone explain to me how this is correct? I am a little confused.

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 = laps + mBarsCount;
    if (laps > 0) {
      throw new IllegalArgumentException("Not enough battery remains");
    }
    mBarsCount -= laps;
  }

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

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

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

}

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Ha! Yeah, you made the test pass. You should check newAmount < 0 it just so happens that your code triggers my test correctly. I will fix it thanks!

Your formula is looking at things a little backwards. The newAmount should be the mBarsCount - laps. If you pass through the exception, set mBarsCount equal to the newAmount.

That make sense?

For one I dont think your if should be dealing with bars but the mBarsCount. not sure about why it is passing

Yes, I got it now! Thank you very much.