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 trialFredy Lopez
2,044 PointsModify the drive method to define a parameter of how many laps should be driven. Update the method body to handl...
im stuck in this
class GoKart {
public static final int MAX_BARS = 8;
private String color;
private int barCount;
private int lapsDriven;
public void drive(laps) {
}
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() {
lapsDriven++;
barCount--;
}
}
1 Answer
Matthias Margot
7,236 PointsThis challenge is asking you to change the method to take a parameter that defines the number of laps you want to drive so that if you wanna drive 5 laps, instead of calling drive(); 5 times you just call it once with the parameter 5 like so: drive(5);.
Now obviously you have to change up the method for that to work correctly. Here's one way to do it:
public void drive() {
lapsDriven++;
barCount--;
}
public void drive(int numberOfLaps) {
while (numberOfLaps > 0) {
drive();
numberOfLaps--;
}
}
This will call the normal drive method and decrement your numberOfLaps until that reaches 0. This introduces a bug by the way because you can give a parameter for numberOfLaps higher than 8 and that will result in your lapsDriven variable to be more than 8 and your barCount to be less than 0. If i remember correctly one of the following challenges asks you to fix this.
Hope i could help
Matthias :)