Java Java Objects Harnessing the Power of Objects Handling Exceptions

What am I doing wrong?

I have been trying to get the right answer, but everything I do seems to be wrong. Can someone please help?

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("not enough battery remains");
    } catch(IllegalArgumentException iea) {
      system.out.println("not enough battery remains");
      system.out.printf("the error was %s", iea.getMessage());
    }
}
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

Pedro Cabral
Pedro Cabral
22,688 Points

You have two problems and both in your Example class:

  1. Java is case-sensitive, system should have a capital S. You use it three times in lowercase.
  2. Your Example class is missing a closing curly-bracket }

Note: if in the code challenge you click on Preview you will be shown some guidance on where the errors are.