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 Harnessing the Power of Objects Overload Methods

Jean Malan
Jean Malan
10,781 Points

Not sure exactly what the instructions are asking me to do.

I understood the first part of the question but this second part it really making me confused. Can anyone please explain what must be added on to this code? it says:

Challenge Task 2 of 2

Of course, another user of the code just wrote and asked "Where'd that drive method go! I loved that method, can you put it back please?" Sigh...Well thanks to method overloading we can pretty easily bring the method back

Create a new method named drive that accepts no arguments. It should call the newer drive method passing in a 1 for the default.

Thanks a lot in advance :)

GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private String color;
  private int barCount;
  private int lapsDriven;

  public GoKart(String color) {
    this.color = color;
  }

  public String getColor() {
    return color;
  }
  public void charge() {
    barCount = MAX_BARS;
  }

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

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

  public void drive() {
    lapsDriven++;
    barCount--;
  }

  public void drive(int lapsToGo) {
    lapsDriven += lapsToGo;
    barCount -= lapsToGo;
  }


}

2 Answers

Tal Yaron
Tal Yaron
9,211 Points

Hey Jean, The challenge says "call the newer drive method passing in a 1" So instead of repeating yourself with similar code in the two drive methods you can do

public void drive() {
  drive(1); // [MOD: added semi-colon - srh]
}
Jean Malan
Jean Malan
10,781 Points

Thank you so much! Just another question in response to that. It says "It should call the newer drive method", which is (as I understand it) -

public void drive(int lapsToGo)

So would I not have to call on the new drive method by referring to lapsToGo?

Tal Yaron
Tal Yaron
9,211 Points

You can use both drive() and drive(lapsToGo), for example

drive() //drives one lap
drive(3) //drives three laps

Does that answer your question?

Jean Malan
Jean Malan
10,781 Points

Okay yea, I get it now! Thanks a lot for the help :)