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

Alexander Nowak
Alexander Nowak
498 Points

Method signatures

HI, I didn't really understand this video despite watching it a few times. I am also stuck on the challenge. Would really appreciate any help/guidance .

Thanks!

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 -= laps;  

  }


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

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

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

}

6 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Alexander,

a method signature helps you to distinguish methods from each other. The first drive method takes a int argument. You tell here how many laps do you want to drive. The second drive method takes no argument. This is the difference between this drive methods. They have different signatures.

And the challenge wants you to create a second drive method that takes no parameter (no arguments like your first drive method) so you can distinguish them. Inside the new methode you can use drive(int laps) method that take arguments. Try this:

public void drive(int laps) {
    // Other driving code omitted for clarity purposes
    mBarsCount -= laps;
  }

  public void drive() { //a new drive method without an argument inside the parenthesis ()
    drive(1);//here you use your first drive method. And because this method needs an argument (int laps) how many laps to drive and the challenge wants only one lap, you put a 1 as an argument inside
  }

Hope it helps

Grigorij

Alexander Nowak
Alexander Nowak
498 Points

Thanks that clears things up! What is a method? and what are they used for?

Thanks,

Alex

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Alex,

you have a class. A class is like a blueprint that describes what objects of this class can do and what they know. So the methods do something an variables know something.

public class Dog{// a Dog class

String dogName = "Washington"; //in the class-blueprint Dog you have a String variable dogName. It knows the dogยดs name - its Washington

void bark(){ // the class Dog have methods and this methods can do something. Therefore you declare a bark() method   that does "Wuff Wuff". 
//Remember that methods have a parenthesis at the end of methods name - bark()
System.out.println("Wuff Wuff");
}
}

I hope it helps

If not, let us know

Grigorij

Alexander Nowak
Alexander Nowak
498 Points

Thank you! That really cleared things up!

Alexander Nowak
Alexander Nowak
498 Points

Thank you! That really cleared things up!

Christiaan Quyn
Christiaan Quyn
14,706 Points

Beautifully Explained ! Thank you :)

Alexander Nowak
Alexander Nowak
498 Points

Oh! aha

I have changed it now :)

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

No problem,

we both learn from our mistakes, like Craig said :)

See you in the forum, coding-bro !

Kyle Banyard
Kyle Banyard
2,260 Points

Thank you Grigorij I was stuck on that for forever!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

You are very welcome !

But you gave you the best answer

:):):)