Java Java Objects Harnessing the Power of Objects Handling Exceptions

Maria Petersen
Maria Petersen
366 Points

How to get past the Protect the call to kart.drive(42);?

I feel I have tried everything by now, and I even looked up other people's codes but nothing works to get me through this assignment. I get a compile error where it says something about illegal static declaration as my only error.

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);
    } catch(IllegalArgumentException iae) {
      System.out.println(iae.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

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there, Maria Petersen ! It seems there is a problem with Java-based challenges right now. This has been reported to Treehouse staff by myself and at least one other moderator. I feel confident that the Support staff is working on this.

That being said, you can skip the challenge by using the white "dots" at the top of the page where the challenge/video is. Clicking any one of these will take you to that section of the course. Don't forget to come back and finish the challenge once everything is working properly again.

Hope this helps! :sparkles: