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 trialSimbarashe Dumbura
765 PointsHi, I cannot seem to get past task 2 of method overloading: Create a new method named drive Task 1 no longer passes
I get thru the first task easily but the next one, of calling the newer drive method and setting a default value of 1 seems to have gotten the better of me. I am quite a novice at coding.
Please help with solutions
Simba
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() {
lapsDriven++;
barCount--;
}
public void drive(int totalLaps) {
lapsDriven += totalLaps;
barCount -= totalLaps;
}
public void drive() {
this.totalLaps = 1;
drive(int totalLaps);
}
}
1 Answer
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 PointsHi Simbarashe,
the challenge wants you to call the old drive with an argument of 1 within the new drive method that doesn't take any arguments.
Your code with comments:
public void drive() {
this.totalLaps = 1; // You don't need this line. You can pass the "1" directly into the drive function.
drive(int totalLaps); // Remove the "int". It is only needed to have the type in front of a variable when declaring a variable or a method.
}
I could paste you the solution, but I believe you can solve this with the comments I added above.
Kind Regards, Florian
Simbarashe Dumbura
765 PointsSimbarashe Dumbura
765 PointsThanks Florian. Much appreciated!!