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

I thought I was doingeverything right

its asking me are you sure that it would equal to more than zero

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;
    int driveAmount = mBarsCount = laps;
    if (mBarsCount < laps) {
     throw new IllegalArgumentException("Not enough battery remains"); 
    }
    mBarsCount = driveAmount;
  }

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

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

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

}

2 Answers

Christopher Augg
Christopher Augg
21,223 Points

Emily,

We only need to test if the amount of laps are more than the amount of battery charge we have left. We need to do this test before we allow mBarsCount -= laps because we do not want this statement to execute.

      public void drive(int laps) {
           // Other driving code omitted for clarity purposes
           mBarsCount -= laps;   //need to move this so that it only executes if the exception is not thrown
           int driveAmount = mBarsCount = laps;  //not needed....remove
           if (mBarsCount < laps) {
               throw new IllegalArgumentException("Not enough battery remains"); 
           }
        mBarsCount = driveAmount; //This is where the other statement that should be moved should be. Replace this with it.
       }


 Regards,

Chris

can you help me with the "storing guesses" part of this lesson? I've spent over an hour watching and rewatching the video and my screen looks exactly like the instructors but I keep on getting the error message: java.util.NoSuchElementException there are a lot of people frustrated with this one because they forgot to inclued the "return isHit" but I have that and the error message is still happening!

Christopher Augg
Christopher Augg
21,223 Points

No Problem. Can you please take a snapshot of your workspace for storing guesses lesson and post the link?