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

NAJEEBULLAH KHAN
NAJEEBULLAH KHAN
2,967 Points

whys is not working i did what was asked

why

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 maxLaps) {
  maxLaps += lapsDriven;
  maxLaps -= barCount;}
  public void drive(int value) {
    value = 1;}
}

2 Answers

Binyamin Friedman
Binyamin Friedman
14,615 Points

Remember, it told you to call the older drive method that takes a number inside your new drive method and use 1 as a default.

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

There are a few things that are wrong Ill explain the best I can

public void drive(int maxLaps) {
    maxLaps += lapsDriven;
    maxLaps -= barCount;}
    public void drive(int value) {
      value = 1;}

you have public void drive twice. delete everything in your drive method and read the directions again and add it one by one. Instead of maxLaps change it to lapsDriven or something like that. Currently you are only modifying the value entered when calling this method you need to change the barCount and lapsDriven not maxLaps. Changing maxLaps does nothing. Use this but make sure you understand what is happening

public void drive(int lapsToDrive) {
  lapsDriven += lapsToDrive;
  barCount -= lapsToDrive;
}

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

in the code above what is happening is I am adding lapsToDrive to lapsDriven, then barCount goes down 1 every lap so I subtract lapsDriven from barCount. Then the instructions tell you to create another drive method that does not take any parameters. That is the second drive method in the code above. All it does is use the first drive method and just druves one lap, which is why you use drive(1) the one is stored in the variable lapsToDrive in the first drive method.