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

Zandre Alberts
Zandre Alberts
2,613 Points

Help me!

The question: Okay, so let's throw an IllegalArgumentException from the drive method if the requested number of laps cannot be completed on the current battery level. Make the exception message "Not enough battery remains".

The error tell me: ./GoKart.java:43: error: reached end of file while parsing } ^ ./GoKart.java:39: error: cannot find symbol if (laps > MAX_BARS) { ^ symbol: variable laps location: class GoKart 2 errors

And This is my code:

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
    mBarsCount -= laps;
  }

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

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

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

  public void load (int drive) {
        if (laps > MAX_BARS) {
     throw new IllegalArgumentException("Not enough battery remains");
   }

}

3 Answers

This is all you have to do. It seems go through even though I didn't attach a message. But just add the conditional to the drive(int) method–checking that bars - laps isn't less than 0.

  public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    if (mBarsCount - laps < 0) {
      throw new IllegalArgumentException();
    }
    mBarsCount -= laps;
  }

Full answers:

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
    if (mBarsCount - laps < 0) {
      throw new IllegalArgumentException();
    }
    mBarsCount -= laps;
  }

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

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

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

}

It appears that you are missing an extra closing bracket to your if-statement after ' throw new IllegalArgumentException("Not enough battery remains"); '

public void load (int drive) {
   if (laps > MAX_BARS) {
     throw new IllegalArgumentException("Not enough battery remains");
   }
}
Zandre Alberts
Zandre Alberts
2,613 Points

I found out and did it rigth but they say this now:

./GoKart.java:38: error: method drive(int) is already defined in class GoKart public void drive(int laps) { ^ 1 error

Did that work for you? Be sure to select that as "correct response" if so!