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,856 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.