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

what is wrong with

Line 15 to 17

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);
    throw new IllegalArgumentException ("Not enough bttery remains");
  }

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

}

1 Answer

In your overloaded 'drive' method (The drive method that takes an int as an argument). You need to check if mBarsCount is less than the number of laps passed into the drive method. If the number of laps is greater than mBarsCount, then you need to throw an IllegalArgumentException that says "Not enough battery remains."

Since you're trying to throw an IllegalArgumentException from your drive method if mBarsCount is less than the number of laps, you need to specify on your method that it throws an IllegalArgumentException.

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

//modify the overloaded drive method
  public void drive(int laps) throws IllegalArgumentException { //specifies that this method could throw an IllegalArgumentException

    // Other driving code omitted for clarity purposes
    if(mBarsCount < laps) { //checks if mBarsCount is less than the number of laps

      throw new IllegalArgumentException("Not enough battery remains.");  //throws an IllegalArgumentException, with the message you specified.
    }
    mBarsCount -= laps;  
  }

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

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

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

}

Let me know if you have any other questions.

Thanks alot

No problem. Also, Ahmed Alfatih , if my answer has helped you out it would be much appreciated if you gave it an upvote. Thanks :)