Java Java Objects Harnessing the Power of Objects Handling Exceptions

I am not understanding this.

I don't know what to do with this problem.

Example.java
class Example {

  public static void main(String[] args) {
    GoKart kart = new GoKart("purple");
    if (kart.isBatteryEmpty()) {
      System.out.println("The battery is empty");
    }

    try{
    kart.drive(42);
      System.out.println("error");
    }catch (IllegalArgumentException iae) {
  System.out.println("baatery is not enough");
}

  }

}
GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private int barCount;
  private String color;
  private int lapsDriven;

  public GoKart(String color) {
    this.color = color;
  }

  public String getColor() {
    return color;
  }

  public void charge() {
    barCount = MAX_BARS;
  }

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

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

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

  public void drive(int laps) {
    if (laps > barCount) {
      throw new IllegalArgumentException("Not enough battery remains");
    }
    lapsDriven += laps;
    barCount -= laps;
  }

}

1 Answer

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,551 Points

Hi there,

You want to use the getMessage() method on the instance you've called iae, use dot notation. Then, output that using println with the above expression inside the brackets.

Let me know how you get on.

Steve.