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

kabirdas
kabirdas
1,976 Points

Overloading Methods

I watched the course video and I do not understand the applicability of the material at all. I do not understand why courses are being shown in the shell, but not shown how we are to actually use that code outside the shell. Regardless, I still do not understand what the questions is even asking. Here is the question:

Modify the drive method to define a parameter of how many laps should be driven. Update the method body to handle the new parameter.

Now, I know I need to change the drive(), but what I need to change that to, I have no idea.

Please help.

Thanks

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

3 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,258 Points

the existing drive method takes no parameter and modifies the two variables lapsDriven and barCount by 1. here they want you to make a new drive method that has a parameter, then uses that parameter to modify lapsDriven and barCount accordingly. in the method definition the parameter goes in the () after the name, like public int myFunc(int myInt).... by passing an argument to the new drive method that will be held in the parameter you add, you will be able to modify lapsDriven and barCount by whatever value is passed in, not just 1.

kabirdas
kabirdas
1,976 Points

OMG!! Never mind! I got it! That was so easy! I think I was overthinking it.

public void drive(int lapsRemaining) {
  lapsDriven += lapsRemaining;
  barCount -= lapsRemaining;
}
kabirdas
kabirdas
1,976 Points

Ok thank you! Here's what I did, and I'm still getting an error:

public void drive(int lapsDriven += 2); {
}