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

Vinayak Gadag
Vinayak Gadag
749 Points

Couldn't able to do this task

I'm not able to complete this task

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(8);
  }

  public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    mBarsCount -= laps;
    if(MAX_BARS < 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;
  }

}

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! There are a number of issues so we'll tackle them one at a time and see if you can get it solved with some hints.

  • MAX_BARS is a constant. Its value will never be anything but what we set it to when we declare it. In this case, 8.
  • The first drive method should remain unchanged. You've changed the 1 to 8
  • In the second drive method we should check if the number of laps ( laps ) they request is greater than our current number of energy bars (not our MAX_BARS)
  • If laps is greater than our current energy bars ( mBarsCount ) then throw a new exception with a message
  • Otherwise deduct the number of laps from our current energy bars

Here's some pseudo code that might help:

if laps is greater than current bars
     throw new exception with message
end if
current bars is now equal to current bars minus laps requested

Hope this helps! :sparkles: