Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Harnessing the Power of Objects Throwing Exceptions

Need help

I am not sure why my code isn't working. Can someone please help.

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
    if (laps > MAX_BARS) {
     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;
  }

}

2 Answers

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;  //this specific line of code will only happen if mBarsCount is greater than the number of laps specified.
  }

  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 for your help. I did not copy that code, I just experimented with it and found a better way... I think.

If you don't mind, could I see the way that you completed it? Because often with the code challenges here, they accept and pass code that otherwise if used in an actual program wouldn't compile.

Ok