Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Harnessing the Power of Objects Throwing Exceptions

Niyamat Almass
Niyamat Almass
8,176 Points

I have a problem

there is a error in my code.and the error is ./GoKart.java:25: error: cannot find symbol if (newCharge < 0) { ^ symbol: variable newCharge location: class GoKart 1 error

So,please solve the problem

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;
  }
  public void load(int laps) {
   int newCharage = mBarsCount - laps;
    if (newCharge < 0) {
      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;
  }

}
Niyamat Almass
Niyamat Almass
8,176 Points

wow i solve the problem.thank you Gloria Dwomoh and Nikolay Mihaylov

3 Answers

Nikolay Mihaylov
Nikolay Mihaylov
2,810 Points

Well, the solution to the problem is the following:

if((mBarsCount - laps) < 0) throw new IllegalArgumentException();

You should put this in the drive method with the integeter argument.

  public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    if( (mBarsCount - laps) < 0 ) throw new IllegalArgumentException(); //Check the battery count and throw an exception if its below zero
    mBarsCount -= laps;
  }

You have to create a conditional statement in order to check the battery level. If its below zero you throw an exception using the IllegalArgumentException object. I would like to point out that it isn't required to use the full if else statement. Once you throw an exception the jvm exits the method and ignores all code after the throw statement.

You could also use the isBatteryEmpty method like that:

if( ( isBatteryEmpty() ) throw new IllegalArgumentException(); 

This is probably the better solution actually :).

Niyamat Almass
Niyamat Almass
8,176 Points

Hi Nikolay Mihaylov I tried your code exactly but there is error .the error is ./GoKart.java:23: error: method drive(int) is already defined in class GoKart public void drive(int laps) { ^ 1 error

Nikolay Mihaylov
Nikolay Mihaylov
2,810 Points

----> int newCharage = mBarsCount - laps;

You have declared a variable newCharage not newCharge. That is why it can't find it.

Niyamat Almass
Niyamat Almass
8,176 Points

hey Nikolay Mihaylov after that they said that ''' Bummer! Are you sure you threw the IllegalArgumentException if the requested amount of laps would make the battery less than zero?'''

The challenge asks you to throw the exception in the "drive" method not in the "load" method. So I suggest you start the challenge afresh and go to the drive method then throw and exception if the requested number of laps cannot be completed with the current battery level. In other words if the laps are more than the mBarsCount.