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

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,723 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,723 Points

Instead of passing MAX_BARS to drive , you pass 1.

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