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

Kamran Ismayilov
Kamran Ismayilov
5,753 Points

IllegalArgumentException

I do not understand what to do. Please help.

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;
  }
  if(isBatteryEmpty) {
   throw new IllegalArgumentExeption("Not enough battery remains");
  }
  public void drive() {
    try {
      drive(1);
    } catch (IllegalArgumentException iae) {
       System.out.printf(iae.getMessage());
    }
  }

  public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    mBarsCount -= laps;
  }

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

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

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

}

2 Answers

Hi Kamran,

Just observed

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

In this line you are declaring isBatterEmpty as boolean but returning an int mBarsCount instead

Not sure what the logic of the program is but it seems like a logic error

Christopher Augg
Christopher Augg
21,223 Points

Kamran,

Sorry for the confusion. Lets get you on track.

The instructions:

Okay, so let's throw an IllegalArgumentException from the drive method if the requested number of laps cannot be completed on the current battery level. Make the exception message "Not enough battery remains".

  1. We only need to alter the drive method for this challenge as the instructions say, "...from the drive method..."
  2. Within the drive method, we need to test the variable being passed in named laps against the amount we have in mBarsCount. If laps is greater than mBarsCount, then we need to throw an IllegalArgumentException with the given exception message.
  3. We should not remove the other code within the drive method because if laps is not greater than mBarsCount, we need that code to work as it did.

Please start the challenge over and only change the drive method. Here is some pseudo-code you can alter to get it to work:

public void drive(int laps) {

    //if laps greater than mBarsCount 
      // throw exception with message. Please review video if you need a refresher
      // on how to throw an exception.

    // we keeps this from before
    mBarsCount -= laps;
 }

Please let me know if this helps.

Regards,

Chris