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

im stuck on ths one , anyone to help?

i don(t get the point, how should i decrement in the drive function?

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

  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;
  }
  private int lapsDriven;

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

    }
  }
}
Douglas Carvalho
Douglas Carvalho
5,056 Points

I don't know if I got exactly what you want to know, but your drive method seems correct. It means that for each lap you complete, one less to go. But one thing that is missing is the lapsDriven class attribute.

1 Answer

Cameron Hastings
Cameron Hastings
3,439 Points

In the first task, you should add a new private int field called lapsDriven. I see you have it, but in a weird place. So, like this:

Solution to first task:

GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private String color;
  private int barCount;
  private int lapsDriven; // <---- this is what you need.

For the second and third task, create a new method called 'drive' (that doesn't return anything), increment the new lapsDriven variable by 1. Decrement the battery by 1 every lap. Use the incrementing shorthand to increase lapsDriven and decrease batteryCount.

So first create the method. It is void, because it doesn't return anything. Looks like this:

GoKart.java
 public void drive() {

}

Then within the drive method, set lapsDriven = 0; and barCount = MAX_BARS. After that, add an if statement. "If the battery is not empty, add 1 to lapsDriven and subtract barCount by 1" Looks like this:

Solution to task 2 and 3:

GoKart.java
 public void drive() {
    lapsDriven = 0;
    barCount = MAX_BARS;
    if (!isBatteryEmpty()) {
      lapsDriven++;
      barCount--;
    }
  }

Hope this helped.