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

HELP. Well past the 20 minute rule been at this for almost 90 minutes, can someone please tell me what im doing wrong :(

Any advice much appreciated

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) {
    lapsDriven += laps;
    barCount -= laps;
    int fuelRemaining = barCount - lapsDriven;
    if (fuelRemaining < laps) {
      throw new IllegalArgumentException("Not enough fuel");
    }
  }
}

2 Answers

Hi James,

You're nearly there!

You wan't to check if the number of laps being requested is greater than the value held in barCount. if it is, throw the exception, as you have done else do the maths piece.

Your method is "doing the maths" irrespective, so it'll allow barCount to become negative if laps is greater than it. That's what we're trying to protect against.

So, test whether laps is greater than barCount and if it is, throw the IllegalArgumentException - else deduct laps from barCount and increase lapsDriven by laps.

That all looks something like:

  public void drive(int laps) {
    if(laps > barCount){
      throw new IllegalArgumentException("Not enough bars!");
    } else {
      lapsDriven += laps;
      barCount -= laps;
    }
  }

Make sense?

Steve.

You can omit the else part as the method will exit if the exception is thrown so it'll not run if that's the case. But, personally, I prefer it being there for improved readability. The code below is fine but it doesn't highlight the binary nature of the code as either the exception will be thrown or the maths bit will happen; never both. I think the addition of the else clause makes that far more obvious.

  public void drive(int laps) {
    if(laps > barCount){
      throw new IllegalArgumentException("Not enough bars!");
    }
    lapsDriven += laps;
    barCount -= laps;
  }

Yes Steve thanks a lot man!

Suleyman Orazgulyyev
PLUS
Suleyman Orazgulyyev
Courses Plus Student 5,798 Points

Hey! So basically you have to check if the number of laps inputted is greater than the number of batteries. If it is, it means that there is not enough battery so you throw an exception. Here it is:

public void drive(int laps) {
    if(barCount >= laps){
      lapsDriven += laps;
      barCount -= laps;
    }else{
      throw new IllegalArgumentException("Error!");
    }
  }