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

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

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

what means with handling the new parameter??

GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private String color;
  private int barCount;
  private int lapsDriven;
  private int laps;

  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(int laps) {

    // todo something to handle the new parameter laps..
    lapsDriven++;
    barCount--;
  }
}
Terry McKee
Terry McKee
27,299 Points

If you got the challenge wrong - notice the comment (make use of += and -=). You want to increase the lapsDriven variable by laps. In other words you want to use the increment operator += to increase that variable. The same goes for barCount except you want to decrement (-=).

9 Answers

Hello

Please examine this:

public void drive(int laps) {

// todo something to handle the new parameter laps..
lapsDriven++;
barCount--;

}

if you pass in laps = 7

then

what you should be doing (hard coded)

lapsDrives += 7 barCounts -= 7

So, for your case, it would be (pseudo code)

lapsDriven += laps;
barCount -= laps

If this answers your question, please mark the question as answered.

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

chase singhofen
chase singhofen
3,811 Points

works perfect

the lapsDriven+= 2 ; --->this is adding 2 laps barCount-= 2 ; ----> this takes 2 bars away from battery

remember each lap takes away 1 energy bar and we drove 2 laps around track the only part that confused me was deciding on the parameter private int laps. i didnt know i had to do this, until i remembered that we couldnt use lapsDriven

this is what I wrote I kept messing with it and finally got it

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

thank you! I was at a complete loss!

I am having problems with this question as well. If someone could give another response to help I am ripping my hair out i dont know what they want from me.

Anupam Kumar
Anupam Kumar
2,961 Points

hey Bryan, How come your method come true over here, Why do you want to decrease barCount by 2 and increase every lapsDriven by 2, Would you please explain

chase singhofen
chase singhofen
3,811 Points

the lapsDriven+= 2 ; --->this is adding 2 laps barCount-= 2 ; ----> this takes 2 bars away from battery

remember each lap takes away 1 energy bar and we drove 2 laps around track the only part that confused me was deciding on the parameter private int laps. i didnt know i had to do this, until i remembered that we couldnt use lapsDriven

Jasmine Martin
Jasmine Martin
14,307 Points

I get the answers.. thankful for the forum. But the question isn't clear.

Christopher Sullivan
Christopher Sullivan
4,230 Points

Can someone tell me why its 7 and not 8?

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

}

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