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

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.

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)