Java Java Objects Harnessing the Power of Objects Increment and Decrement

decrementing in a code

... may i be assisted in the third part of the question. "... in the drive method, decrement the battery's status that we maintain in the private field barCount."

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 void drive() {
    boolean wasDriven = false;
  {
  lapsDriven ++;
  wasDriven = true;
  }
  return wasDriven;

    {
      barCount = --;
    }
      }

  public boolean isBatteryEmpty() {
    return barCount == 0;
  }

  public boolean isFullyCharged() {
    return MAX_BARS == barCount;
  }
}

2 Answers

Emmanuel C
Emmanuel C
7,630 Points

In the drive method, all they ask is to increment the lapsDriven property and decrement the barCount property.

public void drive(){
    lapsDriven++;
    barCount--;
}
Trent Christofferson
Trent Christofferson
15,445 Points

Emmanual C you are correct, I am just adding this if someone else doesn't know what -- or ++ does. barCount--; is the same as barCount -= 1; which is the same as barCount = barCount - 1;

... thanks very much