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

Shaun Wetzel
Shaun Wetzel
9,085 Points

Explain why this code worked.

Some users of our GoKart class wrote and asked that our drive method accept a parameter to specify how many laps to go, instead of just one. It happens, well hey at least you can practice your addition and subtraction shortcuts.

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

^^ this was the coding challenge for java methods the following code completed the challenge but im still struggling to fully understand why it worked.

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

The section im questioning is the last two drive methods.

2 Answers

Steven Parker
Steven Parker
231,007 Points

When you say this "worked", you mean the challenge accepted it? It doesn't appear that it would.

In particular, the version that takes the parameter doesn't make any changes to "lapsDriven" or to "barCount", which I would think is the whole purpose of the function.

Also, the instructions say "Modify the drive method..." which would suggest that (at least at this task) there should only be one function named "drive".

You may have discovered a bug in the challenge verification mechanism. tt might help if you post a link to the challenge page so we could try this out.

Shaun Wetzel
Shaun Wetzel
9,085 Points

yes this code passed the challenge

Steven Parker
Steven Parker
231,007 Points

You can report bugs directly to the staff as described on the Support page.