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

David O'Mahoney
David O'Mahoney
869 Points

Overloading Methods Challenge. Help needed.

Hi there, I cannot seem to answer this task correctly, I've spent an hour on it and I'm getting nowhere. Any help would be much appreciated. The preview console doesn't seem to be much help at all.

Thanks, Dave.

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 int drive( int lapsDriven) {
    lapsDriven++;
    barCount--;
  }
}

4 Answers

Steven Parker
Steven Parker
229,708 Points

You've added the argument, but you still need to do the part of the instructions that say "Update the method body to handle the new parameter." That means instead of increasing/decreasing by one, you'll need to modify the internal variables by the amount in the parameter. Also:

  • updating the body will be easier to do if the parameter name is different from the internal variables
  • this method still does not return anything (is still "void")
David O'Mahoney
David O'Mahoney
869 Points

Hi Steven, thank you for your help, unfortunately after a couple more hours I still can't get the correct answer, I just really don't understand it, think I'm going to have to give up on this track, which is frustrating as I was enjoying it!

I'm sorry for what was probably a very simple question but I'm completely new to any sort of coding and I'm finding very difficult.

Thanks again for you help.

Steven Parker
Steven Parker
229,708 Points

In case it helps, here's an example of what it might look like with my hints applied:

  public void drive(int laps) {  // return type remains "void", use a unique parameter name
    lapsDriven += laps;          // increase the amount by the parameter
    barCount -= laps;            // decrease the amount by the parameter
  }
David O'Mahoney
David O'Mahoney
869 Points

Thanks Steven, I have tried to answer the objective with the code you have suggested and it still doesn't seem to work. I think maybe the page is not checking the work correctly.

Steven Parker
Steven Parker
229,708 Points

That definitely should work for task 1. Are you on task 2?

David O'Mahoney
David O'Mahoney
869 Points

Yeah I am on task 1, it must be something wrong with the page, which is frustrating. Thanks again for your help.

Steven Parker
Steven Parker
229,708 Points

Try restarting the browser and then perhaps your machine itself.

Can confirm the above worked for me, thank you Steven!