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 trialSandy Woods, Jr
Courses Plus Student 2,834 PointsMethod Overloading in Java
Some users of our GoKart class wrote and asked that our drive method accept a parameter to specify how many laps to go, instead of just one. It happens, well hey at least you can practice your addition and subtraction shortcuts.
Modify the drive method to define a parameter of how many laps should be driven. Update the method body to handle the new parameter.
The drive method works and is correct but the logic doesn't make sense: barCount = barCount -laps(laps should be driven?!?). Why not barCount-= lapsDriven? Wouldn't that decrement one bar per 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(int laps) {
lapsDriven += laps;
barCount-= laps;
}
}
2 Answers
Ezekiel dela Peña
6,231 PointsSimply because lapsDriven is the total of all the laps that you've done, while the laps corresponds to the current number laps you will be doing.
Visual example:
assuming I have editted the GoKart class, so that I can have a getters for barCount and lapDriven
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;
this.lapsDriven = 0;
}
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 int getBarCount() {
return this.barCount;
}
public void drive(int laps) {
lapsDriven += laps;
barCount -= laps;
}
public int getLapDriven() {
return this.lapsDriven;
}
}
GoKart goKart = new GoKart("red");
goKart.getLapDriven(); // will return 0
goKart.charge(); // barCount now will be 8
goKart.drive(3); // barCount will be 5
System.out.println(goKart.getBarCount());
goKart.getLapDriven(); // will return 3
goKart.drive(2); // barCount will be 3
System.out.println(goKart.getBarCount());
but if we implement the code to the one you're saying
public void drive(int laps) {
lapsDriven += laps;
barCount-= lapsDriven;
}
Then
GoKart goKart = new GoKart("red");
goKart.getLapDriven(); // will return 0
goKart.charge(); // barCount now will be 8
goKart.drive(3);
System.out.println(goKart.getBarCount()); // barCount will be 5
goKart.getLapDriven(); // will return 3
goKart.drive(2);
System.out.println(goKart.getBarCount()); // barCount = 0, which is logically incorrect
Sandy Woods, Jr
Courses Plus Student 2,834 PointsThis code was helpful. Please explain this statement: System.out.println(goKart.getBarCount()); // barCount = 0, which is logically incorrect
I got 3 for barCount and 5 for lapsDriven