Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
4,912 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.