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 Increment and Decrement

Kevin Jervis
Kevin Jervis
2,600 Points

I'm trying to decrement the the batteryCount by 1, after 1 lap is completed

Keep getting an error: Task 1 is not passing. I'm sure it's something simple, Help :o(

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

4 Answers

If lapsDriven == 1 in order to decrement the battery, then what happens if 2 laps need to be driven and lapsDriven becomes == 2? The simplest way to handle that is by incrementing lapsDriven by 1 and decrement batteryCount by 1 each time the method is is called.

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

Sometimes its hard to see the simplest approach, especially when your brain is filled with new information. Happens to me all the time.

Kevin Jervis
Kevin Jervis
2,600 Points

Thanks for you help guys. Much appreciated :o)

The problem is in this line at the end : if (lapsDriven = 1(){

change it to : if (lapsDriven == 1){

But actually you need to put that in a "for loop" to decrement one barCount after each lapsDriven

You over complicated it just a bit. public void drive(){ lapsDriven++; barCount--;} is really all you need here.

Kevin Jervis
Kevin Jervis
2,600 Points

Hi Mo

Thank you for your response. Of course, makes sense. I tried changing it to == but got the same error.