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 trialRick Garland
2,738 Pointsmaking the exception
to make this exception do you throw two exceptions
class GoKart {
public static final int MAX_BARS = 8;
private String color;
private int barCount;
private int lapsDriven;
public GoKart(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void charge() {
barCount = MAX_BARS;
}
public boolean isBatteryEmpty() {
return barCount == 0;
}
public boolean isFullyCharged() {
return MAX_BARS == barCount;
}
public void drive() {
drive(1);
}
public void drive(int laps) {
int lapsDriven = barCount += laps;
if (lapsDriven > MAX_BARS){
if (lapsDriven )
throw new IllegalArgumentException("Battery count less than zero");
}
barCount = lapsDriven;
}
}
1 Answer
Livia Galeazzi
Java Web Development Techdegree Graduate 21,083 PointsNo, you only need to throw one exception. Comparing lapsDriven with MAX_COUNT doesn't solve your problem (what if the battery was recharged at some point?). You should compare laps with barCount (the remaining battery). If you try to drive more laps than there are barCount, then you won't have enough battery and an exception should be thrown.
Put the check before the operations on lapsDriven and barCount.
These two lines are wrong:
int lapsDriven = barCount += laps;
barCount = lapsDriven;
Just leave them exactly as they were in the provided code, there is no need to change them:
lapsDriven += laps;
barCount -= laps;