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 trialHelen King
9,717 PointsCode Challege help! Method Signatures
I can not figure this one out....
https://teamtreehouse.com/library/java-objects/harnessing-the-power-of-objects/method-signatures-2
I have added a method drive that defines a parameter laps. These high-powered GoKarts use 1 energy bar off their battery each lap. Using method signatures, add a new method called drive that performs just 1 lap and takes no arguments.
I've attached my code. What am i doing wrong with the drive(1)?
public class GoKart {
public static final int MAX_BARS = 8;
private String mColor;
private int mBarsCount;
public GoKart(String color) {
mColor = color;
mBarsCount = 0;
}
public String getColor() {
return mColor;
}
public void drive(int laps) {
// Other driving code omitted for clarity purposes
mBarsCount -= laps;
}
public String drive(1){
mBarsCount--;
}
public void charge() {
while (!isFullyCharged()) {
mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_BARS;
}
}
5 Answers
Kourosh Raeen
23,733 PointsHi Helen - The drive() method shouldn't take any arguments. Inside the method, call the other drive method that takes an int an pass 1 to it.
Kourosh Raeen
23,733 PointsThat's ok. So we need to create another drive method. It's return type is going to be void like the other one, but it doesn't take any parameters. So it will look like this:
public void drive() {
// now call the other drive method with an argument of 1 passed in
}
See if you can figure out the line of code that goes after the comment I left in the code. Let me know if you need more help.
Helen King
9,717 Pointsi've tried adding
drive = 1; drive (1); return drive (1);
while drive(int laps){ drive = 1; }
I'm probably majorly overthinking this :( I guess i'm not understanding how to call the other method
Kourosh Raeen
23,733 Pointsdrive(1); is the one you want. It calls the other drive method with an argument of 1, which results in performing just 1 lap. You don't want the return since the method is void and not returning anything. You also don't want drive = 1; because this is assigning 1 to a variable called drive and not a function call. So, the complete code looks like this:
public class GoKart {
public static final int MAX_BARS = 8;
private String mColor;
private int mBarsCount;
public GoKart(String color) {
mColor = color;
mBarsCount = 0;
}
public String getColor() {
return mColor;
}
public void drive(int laps) {
// Other driving code omitted for clarity purposes
mBarsCount -= laps;
}
public void drive() {
drive(1);
}
public void charge() {
while (!isFullyCharged()) {
mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_BARS;
}
}
Helen King
9,717 PointsYeah I can't get my head around this one, I can't visualize it at all. I don't see where to add the other drive method
Clayton Roberts
2,109 PointsOh man this was driving me up the wall. Thank you for the help
Joseph Richardson
5,909 PointsThis entire lessons wording on its questions are just confusing. It needs to be rewritten .
Helen King
9,717 PointsHelen King
9,717 PointsThank you! I swear I tried that and it didn't work, maybe it was glitching out on me. I get a lot of communication errors! I appreciate the help :)