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 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

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: