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

the problem i m having is from the barsCount line 22 it said cannot find symbole

i have to throw an illegalArgumentException ,if the requested number of laps cannot be completed on the current battery level from the drive method

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 ( laps > barsCount )
      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

Yahya Saadi
Yahya Saadi
4,277 Points

Hi Ramy, first in your if statement you've typed barsCount instead of mBarsCount. However the correct answer should be:

public void drive(int laps) {

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

mBarsCount -= laps; }

Hope this helps.

thx a lot Yahya but one question in the mBarsCount statement <1 why did u put one they didnt say in one lap so how did you know ? and thx a lot it was the good answer :D

and one last thing plz

-The code in Main.java is going to throw an IllegalArgumentException when the drive method is called. Catch it and warn the user.

  • Bummer! Looks like you didn't catch the exception.

1 public class Main { 2 public static void main(String[] args) { 3 GoKart kart = new GoKart("yellow"); 4 if (kart.isBatteryEmpty()) { 5 System.out.println("The battery is empty"); 6 } 7 kart.drive(2); 8 try { 9 kart. drive (400); 10 System.out.println (" this will never happen !"); 11 } 12 catch (IllegalArgumentException iae ) { 13 System.out.println (" be careful "); 14 System.out.printf("the error was : %s\n" , iae.getMessage ()); 15 } 16 } 17 }

Yahya Saadi
Yahya Saadi
4,277 Points

I didn't get the last part. What is your question?