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 trialZandre Alberts
2,613 PointsHelp me!
The question: 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".
The error tell me: ./GoKart.java:43: error: reached end of file while parsing } ^ ./GoKart.java:39: error: cannot find symbol if (laps > MAX_BARS) { ^ symbol: variable laps location: class GoKart 2 errors
And This is my code:
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;
}
public void charge() {
while (!isFullyCharged()) {
mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_BARS;
}
public void load (int drive) {
if (laps > MAX_BARS) {
throw new IllegalArgumentException("Not enough battery remains");
}
}
3 Answers
Josh Davis
24,227 PointsThis is all you have to do. It seems go through even though I didn't attach a message. But just add the conditional to the drive(int) methodโchecking that bars - laps isn't less than 0.
public void drive(int laps) {
// Other driving code omitted for clarity purposes
if (mBarsCount - laps < 0) {
throw new IllegalArgumentException();
}
mBarsCount -= laps;
}
Full answers:
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 (mBarsCount - laps < 0) {
throw new IllegalArgumentException();
}
mBarsCount -= laps;
}
public void charge() {
while (!isFullyCharged()) {
mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_BARS;
}
}
Josh Davis
24,227 PointsIt appears that you are missing an extra closing bracket to your if-statement after ' throw new IllegalArgumentException("Not enough battery remains"); '
public void load (int drive) {
if (laps > MAX_BARS) {
throw new IllegalArgumentException("Not enough battery remains");
}
}
Zandre Alberts
2,613 PointsI found out and did it rigth but they say this now:
./GoKart.java:38: error: method drive(int) is already defined in class GoKart public void drive(int laps) { ^ 1 error
Josh Davis
24,227 PointsDid that work for you? Be sure to select that as "correct response" if so!
Zandre Alberts
2,613 PointsZandre Alberts
2,613 PointsThanks