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 trialAlex Popian
977 PointsCan't figure out my mistake..
I can't seem to find my mistake. I keep getting this error message: Bummer! Are you sure you threw the IllegalArgumetnException if the requested amount of laps would make the battery less than zero? Please help! Thanks!
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 batteryCount = mBarsCount - laps;
if (mBarsCount < 0){
throw new IllegalArgumentException("Not enough battery remains");
}
mBarsCount = batteryCount;
}
public void charge() {
while (!isFullyCharged()) {
mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_BARS;
}
}
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! Christopher is partially correct. However, this does not take into account the number of laps. So imagine for a moment that we have 2 bars left, but we want to run 5 laps. Two is going to be greater than or equal to 0 so it won't throw an exception, but it should. What we want to check for is if the number of bars left is less than the number of laps to be run. Try again, with this hint in mind!
Christopher Harris
1,166 Pointsif (mBarsCount < 0) try putting this as if (mBarsCount <= 0) the way your code is currently means when the battery = 0 it can still take one putting it on -1, but by using the less than or equal to it cant run when it gets to zero
Christopher Harris
1,166 PointsChristopher Harris
1,166 Pointsahh right you are Jen lucky you looked properly :)
Alex Popian
977 PointsAlex Popian
977 PointsThanks for not giving me the full answer, it actually helped me understand the challenge. I ended up replacing batteryCount with currentCharge which made sense since the mBarsCount is either 0 or max (which is 8) thanks to the charge method. If we do, say, 6 laps, it will be currentCharge = 8(mBarsCount) - 6(laps) which will be 2 and then I set it to <=0 which fixed everything. Thanks to both of you for the help! :D