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

how do I shorthand adding a lap to laps driven? I currently have lapsDriven++ as the shorthand but its not working.

I cant get it to add laps driven using the correct shorthand. Help!

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 () {
    barCount = MAX_BARS;
  }

  public boolean drive () {
    boolean lapsDriven = false;
      if (!isDriven()) {
        barCount--;
        lapsDriven++;
      }  
    return lapsDriven;
  } 

}

1 Answer

Simon Coates
Simon Coates
8,177 Points

Are you shadowing the main field? You seem to have a field and a local variable called lapsDriven and you aren't using the this keyword to access the field. And I think you have two drive methods with the same signature (name and same params). And you're referencing a method called isDriven that doesn't seem to exist. The challenge isn't offering helpful error messages. It accepts:

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

If would probably be a good idea to wrap the increment/decrement in if (!isBatteryEmpty()) , but it doesn't seem to care.

Hey thanks for your response. It helped me realize I added that an unnecessary variable to the code. I got it worked out.