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 (Retired) Harnessing the Power of Objects Incrementing

Robert Roberson
PLUS
Robert Roberson
Courses Plus Student 8,401 Points

I have watched the video several times and tried several times to complete the task with no results.

I would like to add the screen shot of what I coded but can't

GoKart.java
public class GoKart {
  public static final int MAX_ENERGY_BARS = 8;
  private String mColor;
  private int mBarsCount;

  public GoKart(String color) {
    mColor = color;
    mBarsCount = 0;
  }

  public String getColor() {
    return mColor;
  }

  public void charge() {
    mBarsCount = MAX_ENERGY_BARS;
  }

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

  public boolean isFullyCharged() {
    return mBarsCount == MAX_ENERGY_BARS;
  }

}

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Okay, so the task wants you change one method so that it calls another based on a certain condition. So it only wants to charge a GoKart while there are bars available to charge it.

You could do this with an if condition but the challenge wants a while loop

public void charge() {

    while( !isFullyCharged() ) {
      mBarsCount++;
    }
    mBarsCount = MAX_ENERGY_BARS;

  }

With the while loop you're checking that the action results in a condition that returns false. We know we're checking for a false result because we're adding the negation operator to the while loop expression. We know that if the function does return false then it needs to be fully charged. To do that we simply increment the value of the mBarsCount variable and we will continue to do this until the method returns true.

Hope this makes sense :)

Robert Roberson
PLUS
Robert Roberson
Courses Plus Student 8,401 Points

Thank you, That is definitely not what I understood from the video. I was trying to use everything he did in the video.