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 trialDaniel Hunter
4,622 Pointsmethod overloading,, help!!!
the task I am stuck on is - goKart.java method overloading - question - Create a new method named drive that accepts no arguments. It should call the newer drive method passing in a 1 for the default.
I know how to create the method but am unclear how to pass in a 1 for the default.
My answer -
public void drive() { drive = a1; }
it comes up make sure you set an integer a 1 as default, I have tried all different ways but can't remember how to set a default value. also it doesn't make sense that is is asking to declare an integer with a character and an integer valve. please help
regards dan
class GoKart {
public static final int MAX_BARS = 10;
private String color;
private int barCount;
private int lapsDriven;
private int maxLaps = 8;
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 maxLaps){
barCount-= lapsDriven += maxLaps;
}
public void drive(){
drive
}
}
4 Answers
Steven Parker
231,184 PointsIn task 1, you were expected to change the existing "drive" method to use the argument. Since you added a new one, you still have one that takes no argument. You can only have one like that.
Then, don't get confused by the word "default". In the method that takes no argument, you will just call the other version giving it an argument of "1", like this: "drive(1);
"
Daniel Hunter
4,622 Pointsthanks Steven, that first part makes perfect sense now, I did wonder why I would have two drive methods without any arguments I just didn't realise I should have modified the first one.. As for the second part of my question I don't quite the question. It says to pass in a 1 as the default. it does make sense what answer they are expecting but here does the "a" come from? or is that a typo in the question? Dan
Steven Parker
231,184 PointsI think it's just a matter of how you read it:
- ...passing in "a 1"
-vs-
- ...passing in a "1" I believe this is how they intended it
But I think you discovered a possible bug. The original method added 1 to lapsDriven and reduced barCount by one, so it makes sense that the new one should add and remove the laps instead. This is something the challenge should probably check for. But the code you passed with reduces barCount by the total lapsDriven instead of just the argument.
You might want to report this as a bug to Support.
Daniel Hunter
4,622 PointsI still can't get this to work. I deleted the first version of the drive method without any arguments and entered -
public void drive(){ drive(1); }
and I've tried -
public void drive(1){ drive; }
and have tried various other options, where do you put in the default value? thanks.
Jacob Gonzales
1,837 PointsThe first one is really close! try
public void drive(){
this.drive(1);
}
Daniel Hunter
4,622 Pointsah it was because I hadn't deleted the end } from the first drive method. thanks for your help
Jacob Gonzales
1,837 PointsJacob Gonzales
1,837 PointsHey Dan! Since you want to use 1 as the default value it would be like calling your drive method with 1 as the parameter. So inside of your drive method without the parameter you can just call your previous drive method inside of it using 1 as the parameter it should look something like this:
This way when you call drive with no parameter it will always just do the one lap. Also, it may be just me but I find it easier to read your first drive method if it was on separate lines? Mine looked like this:
Again that is just my personal preference so take it with a grain of salt haha. Let me know if you have any other questions good luck!