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

Aaron Williams
Aaron Williams
1,702 Points

Im so confused

.

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

  public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    mBarsCount -= laps;
     int newDriveAmount = mBarsCount + laps;
    if (newDriveAmount > MAX_BARS) {
      throw new IllegalArgumentException("Not enough battery remains"); 
    }
    mBarsCount = newDriveAmount;
  }

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

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

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

}
Ricky Catron
Ricky Catron
13,023 Points

Hey Aaron what exactly are you confused about? The only thing I see a problem with at a glance is the indentation in your drive function.

mBarsCount -= laps; int newDriveAmount = mBarsCount + laps;

^ Why is this indented more here?

--Ricky

5 Answers

Let me clarify. I was saying to throw an iae if (mBarsCount<laps). Please see comments:

  public void drive(int laps) {
    //If number of battery bars is less than the number of laps, throw an iae
    if(mBarsCount < laps){
      throw new IllegalArgumentException("Not enough battery remains.");  
    }
    // run the GoKart and deplete the battery for the number of laps.
    mBarsCount -= laps;
  }

Background information:

  • mBarsCount is the battery remaining in the GoKart. (Maximum 8 bars)
  • laps is the number of laps the GoKart is needed to run.
  • assuming 1 lap will need 1 bar of battery. It is needed to prevent the GoKart from running more laps than it can handle. (mBarsCount should be more or equal to laps)

So before driving (mBarsCount -= laps), you need to throw the IllegalArgumentException if (mBarsCount < laps).

Aaron Williams
Aaron Williams
1,702 Points

I'm now getting "Bummer! Are you sure you threw the IllegalArgumentException if the requested amount of laps would make the battery less than zero?" 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(MAX_BARS); }

public void drive(int laps) { // Other driving code omitted for clarity purposes mBarsCount -= laps; int newDriveAmount = mBarsCount + laps; if (mBarsCount >= laps) { throw new IllegalArgumentException("Not enough battery remains"); } mBarsCount = newDriveAmount; }

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

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

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

}

  1. You don't need int newDriveAmount = mBarsCount + laps;, and
  2. if (mBarsCount >= laps) { should be if (laps > mBarsCount) {.