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

Guillermo Ferreyra
Guillermo Ferreyra
1,314 Points

gokart challenge tast 2 of 2 laps methods

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

I have create the new drive method but what does it means "passing in a 1 for default"?

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 laps) {
    lapsDriven += laps;
    barCount -= laps;
  }
}

1 Answer

Hi there,

Your drive method that takes no arguments should call the other drive method (that does take an argument) and that argument should make the kart drive 1 lap. So, inside drive() call drive(int laps) and pass in 1 as the argument (laps). Do nothing else inside your drive() method as the maths is all done within drive(int laps).

Make sense?

Steve.

Guillermo Ferreyra
Guillermo Ferreyra
1,314 Points

I like how you answer but it took me time to figure out what you meant with calling the drive method that contains the laps argument inside the drive method that doesn't have arguments I was not expecting that I just need to write "drive(1);" inside the drive method with no arguments. Thanks for your answer it really helps me to solve the problem.