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

Willie Suggs
PLUS
Willie Suggs
Courses Plus Student 5,879 Points

how do i call existing method into new method & set default parameters?

just help me enough to the point were i can figure it out.

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--;
  }

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

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

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

}

5 Answers

anil rahman
anil rahman
7,786 Points

Ok so this is what you've got so far:

public void drive() 
{ 
   public_void_drive(int laps); 
    mBarsCount--;
}

what you are saying is ok make the new method with no parameters: Correct!

public void drive() 
{ 

}

but then you kind of had the right plan but just didn't come out right.

public_void_drive(int laps); 
    mBarsCount--;

You put those two in the method. What you are doing there is like the start of an actual method declaration. What instead you wanted to do was call the method drive you already have so like this:

drive(); 

But remember that has a parameter called laps which takes a integer. And the method is used the minus whatever number you put in from the energy count.

drive(1);

That is saying call my drive method and pass integer of 1 so it will go through and call that previous method. This one:

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

That method does the work for you so it takes the one and says energy - 1.

so overall you are left with:

public void drive() 
{ 
   drive(1);
}
anil rahman
anil rahman
7,786 Points

Ok so it says create another method called drive with no parameters i think you know how to do that, so after that it wants you to call the other drive method. to call a method this is the syntax.

method_name(parameter_values_if_it_takes_any);

Willie Suggs
Willie Suggs
Courses Plus Student 5,879 Points

public void drive() { public_void_drive(int laps); mBarsCount--;}

what am i doing wrong

Dennis Addo
PLUS
Dennis Addo
Courses Plus Student 2,943 Points

all you need to do it to create another method with the same name as drive but with no argument, and then call the drive method with Integer argument. since your drive method should perform only one lap, just put 1 in the drive argument method and it is done. it looks like this.

anil rahman
anil rahman
7,786 Points

He said he didn't want the answer he only wanted the slightest help so he could figure out how to do it alone.

Willie Suggs
Willie Suggs
Courses Plus Student 5,879 Points

public void drive() { public_void_drive(int laps); mBarsCount--; }

what am i doing wrong

anil rahman
anil rahman
7,786 Points

No worries that's why i only gave syntax of calling method ;)

Dennis Addo
PLUS
Dennis Addo
Courses Plus Student 2,943 Points

yea i just realize, thanks for the hint, i hope it helps him