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 Throwing Exceptions

Isaac Serrano
Isaac Serrano
565 Points

Trying to include a throw new IllegalArgumentExpresion in relation to the battery being less than 0. Not sure how?

I added the throw new IllegalArgumentExpresion and the if loop, but not sure how to finalize the code to make it work?

GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private String color;
  private int barCount;
  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(barCount<0){
      throw new IllegalArgumentException();
    }
    lapsDriven += laps;
     barCount -= laps;


  }
}

1 Answer

Antonio De Rose
Antonio De Rose
20,885 Points

very close not so close, here, from the previous questions, we have to come with an assumption, one barcount is equivalent to run 1 lap, barcount is nothing than energy, the energy to be greater than the lap you make

you have to have greater energy than the lap

yes, in your case, you are checking the barcount, good, but why should it be checked against 0, it should be against, I know, you will find it.