Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Alexander Teesdale
PLUS
Alexander Teesdale
Courses Plus Student 2,392 Points

Method Signatures - 1 Objective

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.

That is the task but I do not understand how to complete this task...

any help would be much appreciated!

5 Answers

anil rahman
anil rahman
7,786 Points

you can try this not sure if it's exact because you didn't show any code:

public void Drive() { laps++; }

He said make a new drive method and each time its called it should mean hes done 1 lap. This method does this by taking the laps variable and using the shortcut ++ to mean just add 1 to the variables value.

Alexander Teesdale
PLUS
Alexander Teesdale
Courses Plus Student 2,392 Points

Hello, thank you for the quick reply. I just realized I did not post any code!

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 charge() { while (!isFullyCharged()) { mBarsCount++; } }

public boolean isBatteryEmpty() { return mBarsCount == 0; }

public boolean isFullyCharged() { return mBarsCount == MAX_BARS; }

}

anil rahman
anil rahman
7,786 Points

In that case you should try:

public void drive() { mBarsCount--; }

mBarsCount is your energy counter, so by taking 1 away that is basically saying i've just done a lap.

Test it out :)

Alexander Teesdale
PLUS
Alexander Teesdale
Courses Plus Student 2,392 Points

It wants me to call the original drive() method, and pass in a default parameter of 1. I actually had this code originally too but could not figure out how to call the method...

anil rahman
anil rahman
7,786 Points

Ah ok i didn't spot that. You just call it in the main method of your other class or in the method it wants you to enter it in like this:

drive(1);

Alexander Teesdale
PLUS
Alexander Teesdale
Courses Plus Student 2,392 Points

Wow....simpler is always better. So easy to over think these tasks. Thank you very much my friend! :) Have a great day!