Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Harnessing the Power of Objects Overload Methods

Overloading methods/confused about what the second task is asking for

The second task on this code challenge is really confusing to me. It would seem to imply that I deleted the first drive method, but the instructions pretty much tell you not to delete code from previous completed tasks.

I don't get what it means when it wants the newest drive method to call the one from before with a default int of 1. Can somebody show me how this is supposed to be done and explain how it works? I feel like the Java Objects course is moving much faster than the Java Basics course did and I'm not sure how well I'm understanding each things before he moves on, even in the same video,

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

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


}

3 Answers

Tomasz Obidowski
Tomasz Obidowski
984 Points

I had this same problem. Thanks.

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,577 Points

The first question is asking that you modify the existing drive method (i.e. you're not adding overloading the method). The second task is asking that you use the modified method, which now takes an int, in a new drive method that takes no arguments.

Your code:

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

  public void drive(int lapAmount){ // keep this
    lapsDriven += lapAmount;
    lapAmount -= barCount;
  }

   public void drive(int 1){ // this should call drive with argument 1; it shouldn't take any arguments
    drive(MAX_BARS);
  }

I understand the first part, but I still have no idea what the solution to the second part would be. I don't know what that would look like in code. What does argument 1 mean?

Clayton Perszyk
Clayton Perszyk
Treehouse Moderator 48,577 Points

Instead of passing MAX_BARS to drive , you pass 1.

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