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 (Retired) Harnessing the Power of Objects Throwing Exceptions

Neelesh Tewani
Neelesh Tewani
1,239 Points

make sure you throw the exception before changing mBarsCount field

public void drive(int laps) { // Other driving code omitted for clarity purposes

mBarsCount -= laps;
if(mBarsCount<=-1){
throw new IllegalArgumentException("Not enough battery remains");
}
}
GoKart.java
public class GoKart {
  public static final int MAX_BARS = 8;
  private String mColor;
  private int mBarsCount;

  public GoKart(String color) {
    mColor = color;
    mBarsCount = 0;
  }

  public String getColor() {
    return mColor;
  }

  public void drive() {
    drive(1);
  }

  public void drive(int laps) {
    // Other driving code omitted for clarity purposes

    mBarsCount -= laps;
    if(mBarsCount<=-1){
    throw new IllegalArgumentException("Not enough battery remains");
    }
    }

  public void charge() {
    while (!isFullyCharged()) {
      mBarsCount++;
    }
  }

  public boolean isBatteryEmpty() {
    return mBarsCount == 0;
  }

  public boolean isFullyCharged() {
    return mBarsCount == MAX_BARS;
  }

}

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Hey Neelesh,

Two things:

Our if statement should just check if our mBarsCount has a value greater than the "laps" value (the value we pass in as a constructor). if our mBarsCount is less than the value we pass in, then we can't subtract from it because it would bring a negative which we dont want. When that happens, we want to throw the exception. Also the subtraction should happen after we check the if statement. So in visual format:

Answer.java
  public void drive(int laps) {
    // Other driving code omitted for clarity purposes


    if(mBarsCount< laps){ //check if mBarsCount is less than "laps"
    throw new IllegalArgumentException("Not enough battery remains"); //if true then error
    }
        mBarsCount -= laps; //otherwise peacefully subtract desired value
    }

I have also found out that the method below works as well however the above solution is more logical because what we had a value of 1 in our mBarsCount but we passed in a parameter of 5. It would pass the if statement but then bring back a error because we can't subtract 5 from 1.

I added a few comments below

Alternative.java
  public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    if (mBarsCount==0) { //check if we have any value in our mBarsCount
     throw new IllegalArgumentException ("Not enough battery remains"); //if no value inside, then throw exception
    }
    mBarsCount -= laps; 
//if we do have a value, then subtract the value that we passed in. but as i stated above, this method will bring back errors if our "lap" value is greater than our "mBarsCount" value
  }

I hope that helped,

Kevin