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

Matt George
Matt George
2,507 Points

Okay, so let's throw an IllegalArgumentException from the drive method if the requested number of laps cannot be complet

Okay, so let's throw an IllegalArgumentException from the drive method if the requested number of laps cannot be completed on the current battery level. Make the exception message "Not enough battery remains".

Please help me https://teamtreehouse.com/library/java-objects/harnessing-the-power-of-objects/throwing-exceptions

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

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

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

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

}

5 Answers

Michael Hess
Michael Hess
24,512 Points

Hi Matt,

You're really close! But, get rid of:

int newAmount = mBarsCount + laps

and set the number of bars minus the laps :

mBarsCount -= laps;

You should end up with:

if (laps > mBarsCount){
    throw new IllegalArgumentException ("Not enough battery remains");
}
mBarsCount -= laps;

If you have any other questions feel free to ask! Hope this helps!

Matt George
Matt George
2,507 Points

Thanks Michael, but this is what i got and it still doesnt work:

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

 if (laps > mBarsCount){
throw new IllegalArgumentException ("Not enough battery remains");

} mBarsCount -= laps;

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

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

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

}

Michael Hess
Michael Hess
24,512 Points

Try this:

public void drive(int laps) {
    // Other driving code omitted for clarity purposes
   if (laps > mBarsCount){
    throw new IllegalArgumentException ("Not enough battery remains");
     }
     mBarsCount -= laps;
  }
Jordan Bester
Jordan Bester
4,752 Points

Hey micheal why would we get rid of :

int newAmount = mBarsCount + laps ?

I got the code but Just need some clarity on why we are not using this code?

I used this too and found the explanation quite helpful :)

Matt George
Matt George
2,507 Points
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
  if (laps > mBarsCount){
    throw new IllegalArgumentException ("Not enough battery remains");
}
mBarsCount -= laps;



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

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

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

}
Michael Hess
Michael Hess
24,512 Points

Add a curly brace to close drive() and you've got it!

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

}  //add a curly brace here and you've got it!



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

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

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

}
Matt George
Matt George
2,507 Points

when i clicked preview i show me this

./GoKart.java:25: error: class, interface, or enum expected
  public void charge() {
         ^
./GoKart.java:28: error: class, interface, or enum expected
    }
    ^
./GoKart.java:31: error: class, interface, or enum expected
  public boolean isBatteryEmpty() {
         ^
./GoKart.java:33: error: class, interface, or enum expected
  }
  ^
./GoKart.java:35: error: class, interface, or enum expected
  public boolean isFullyCharged() {
         ^
./GoKart.java:37: error: class, interface, or enum expected
  }
  ^
JavaTester.java:69: error: cannot find symbol
if (!kart.isBatteryEmpty()) {
         ^
  symbol:   method isBatteryEmpty()
  location: variable kart of type GoKart
7 errors
Matt George
Matt George
2,507 Points

wait... i just copied your code and it works now

Thanks Alot!!!