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

Juan Rodríguez
Juan Rodríguez
782 Points

How do I call the original drive method and how do I "pass" 1?

I can't seem to call the original method "drive" the instructor gave. I could only make the car do one lap by writing:

public void drive() { mBarsCount = mBarsCount - 1; }

Which would meand that when we use that method, the GoKart would do 1 lap (which is the same as consuming 1 energy bar). So I don't know how could I do this in another way... I'm stucked! D:

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 drive(){
   (mBarsCount = mBarsCount - 1);
  }

  public void charge() {
    while (!isFullyCharged()) {
      mBarsCount++;
    }
  }

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

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

}

2 Answers

Juan Rodríguez
Juan Rodríguez
782 Points

I could finally do it by setting the drive method to 1 like this...

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

That was all! Anyway, I don't understand why that works... My guess is that it makes the variable laps equal to 1, but I'm not sure, could I get a hand from you guys?

This is creating method signatures, the original one takes a parameter that tells the GoKart to go a certain number of laps. The challenge asks you to create a second signature that automatically runs one lap. Instead of basically retyping the same code we just add the original method in there and give it a '1' for the argument; so when someone uses the drive() method with an argument it will go that number of laps otherwise, if they do not put in an argument it will go the default of one lap.

The rules of method signatures is that you can create the same method name over and over as long as you have varying arguments- you cannot have two completely identical methods with the same type and number of arguments; this will confuse the compiler.