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 trialAmir Tamim
8,597 PointsTruly at a loss on this one:
Could someone guide me and point in the right direction?
Add logic to the drive method so that it throws an IllegalArgumentException if there aren't enough bars to support the laps request. Remember it takes 1 bar of energy to go around a lap.
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) {
if (laps>8){
throw new IllegalArgumentException();
}
}
lapsDriven += laps;
barCount -= laps;
}
}
2 Answers
Timothy Hilley
2,292 Pointslets look at it in a //psuedo code
public void drive(int laps) { //the method is taking in the amount of desired laps to drive
if(laps > barCount) { //since it takes one bar of fuel to do one lap, if theres not enough fuel
throw new IllegalArgumentException(); //say WOAH thats too much to ask for. I'm only at half a tank.
}
lapsDriven += laps; //if you have enough fuel, drive the laps
barCount -= laps; //the laps used up fuel
}
you also have one too many } in there between your ifthrow and the rest of the content of the method This is my first try at helping so let me know if that works lol.
Pablo Villegas
2,922 PointsThe problem lies in your conditional laps > 8
. Think about what would happen if barCount
is 2 after driving some laps. Would you condition laps > 8
work?