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

Adam Arafat
Adam Arafat
1,066 Points

In your newly created drive method, increment the new lapsDriven variable by 1.

So in this question its on one of the videos for Java basic in the objects sub-genre. I would add to the code the following without quotes on the outside;

"public String drive(); "

Yes it tells me that it doesn't need a return thus telling me to make it void, but when i add a code like the following;

" public methodDrive(); "

It tells me that I need to place a new method. I've also tried the following;

" public boolean drive() { boolean wasDriven = true; if (!isDriven()); mBarCount--; Also tried mBarCount++; wasDriven = false; } return wasDriven; } "

This also gives me the response that the return is void, I added " return void wasDriven" then tells me it doesn't work at all. I have no clue what I'm doing wrong.

GoKart.java
class GoKart {
  public static final int MAX_BARS = 8;
  private String color;
  private int mBarCount;
  public GoKart(String color) {
    this.color = color;
  }
  public String getColor() {
    return color;
  }
  public void charge() {
    mBarCount = MAX_BARS;
  }
  public boolean isBatteryEmpty() {
    return mBarCount == 0;
  }
  public boolean isFullyCharged() {
    return MAX_BARS == mBarCount;
  }
  private int lapsDriven;
}

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

The drive() method's body is actually very simple, you only need to increment lapsDriven by 1.

class GoKart {
  public static final int MAX_BARS = 8;
  private String color;
  private int barCount;
  private int lapsDriven;  // create the lapsDriven field

  public void drive() {   // drive() method with no return type
    lapsDriven++;         // increment lapsDriven by 1
  }

  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;
  }
}