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 trial

Java Java Objects (Retired) Harnessing the Power of Objects Method Signatures

Ivan Gadza
Ivan Gadza
1,543 Points

I got no idea what to do,can anyone explain it a bit?

Help please. I have added a method drive that defines a parameter laps. These high-powered GoKarts use 1 energy bar off their battery per lap. Using method signatures, add a new default method called drive that performs just 1 lap and takes no arguments.

GoKart.java
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;
  }

}

3 Answers

Lee Reynolds Jr.
Lee Reynolds Jr.
5,160 Points

It's asking you to use method signatures to create a method. Currently you have one drive method in place and it takes a number as it's signature. So you can use the same name of drive as follows:

public void drive() { //world changing code goes here. }

Now Java knows the difference in the different methods because it passes no parameters. Had you been passing the other method of drive then you would call the method and input a number as your signature point, but if you call drive method and pass nothing inside of it then it would use the one that we just created above.

Now what it wants us to do is use one of the drive methods to make the other drive method run once. So it goes like this:

public void drive() { drive(1); }

Inside of that one there you are passing the int for the number of laps. When you pass the int as a parameter it now knows that we are calling the first method called drive. I know I'm using a lot of words but that's signatures in a lot of words.

Coulter Reyes
Coulter Reyes
703 Points

Thanks for this explanation Lee!

Allie O.
Allie O.
11,601 Points

Hi Ivan,

The challenge is asking you to create a new method called drive() - it won't take any arguments so there will be no values inside the parenthesis, unlike the drive method that is already there. And it won't return any values, so make sure to include the keyword void. Inside this new drive method, you will use the drive(int laps) method to perform 1 lap.

See if you can figure out the code from here :)