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

Modifying an existing method to accept an int value

So I've found myself stuck for the first time in java. The question is asking me to modify and existing method (drive) to accept an int value for how many laps should be driven.

My confusion stems from already incrementing a lapsDriven in the original code, i'm just not sure what the question is asking of me.

The original code was:

public void drive(); {
 lapsdriven++;
 barCount--;
}

I tried adding an if statement that looked like:

public void drive(); {
lapsdriven++;
barcount--;
if (lapsDriven == 8) {
return System.out.println("Time to return to the pits!  ");
}

In my head that accomplishes the same goal but it's not what the question is looking for. I am confused. Any help is appreciated, thanks in advance.

GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  public static final int MAX_LAPS = 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() {
    drive = lapsDriven;
    lapsDriven++;
    barCount--;
  }
}

1 Answer

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 38,004 Points

Hi Ryan Johnson. I agree this question is confusing. So right now the drive method is adding one (++) to lapsdriven and subtracting one (--) from barcount. So what they want you to do is pass an int into the drive method and use that integer to subtract and add to lapsdriven and barcount. So if you pass 2 in it will add 2 to lapsdriven and subtract 2 from barcount. If you pass 1 in it will work like it does now. They want you to practice using += and -=. Hope this helps Ryan and good luck. Post back to the Community if you are still stuck