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

I don't understand the Code Challenge "Incrementing and Decrementing". GoKart

Challenge Task 1 of 1


Okay, so let's use our new isFullyCharged helper method to change our implementation details of the charge method. Let's

make it so it will only charge until the battery reports being fully charged.

Let's use the ! symbol and a while loop. Inside the loop increment mBarsCount.


thanks,

ISAIAH

3 Answers

OK, so you've got a method called charge in which the mBarsCount variable is altered. At the moment, it just sets it to MAX. The challenge is to change this.

There's a method isFullyCharged() that returns a Boolean to say whether the charge is complete, or not, i.e. is mBarsCount at its maximum possible amount.

So, we want to put a loop together to charge the kart only when it isn't already fully charged. It suggests using a while loop for this, which makes sense.

Along the lines of, while the kart isn't fully charged add one to the charge until it is fully changed.

That looks like,

public void charge() {
    while ( !isFullyCharged() ) {
        mBarsCount++;
    }
}

So, while the isFullyCharged function returns false (the ! is the not operator), add one to mBarsCount. As soon as it is fully charged, don't!

I hope that helps.

Steve.

Thanks for this post, my brain started hurting from trying to figure out what I missed haha :D

Zandre Alberts
Zandre Alberts
2,613 Points

That shoud work but mine cant indentefy the charge funtion

Hi Zandre,

Can you post your code and I'll take a look.

Steve.

Zandre,

I received your code in my email but not on here.

You have add an alternative charge() method after your isFullyCharged method. If you put that code inside the existing charge method then you're absolutely fine. :smile:

Steve.

Ruslans Melniks
Ruslans Melniks
2,867 Points

Great explanation, thank you so much!

No problem! :+1:

that worked.

thanks,

ISAIAH

I am having the same problem. What's wrong?

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;

    while (!isFullyCharged) {
      mBarsCount ++;
  }

}

Hi David,

The challenge here needs you to re-write the charge() method. You have amended the isFullyCharged()method. You need to use isFullyCharged as this tells you whether the battery is fully charged or not, but you don't need to change it. Set it back to:

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

Now, in the charge() method, you want to put the code you had in the other method. So, charge() looks like:

  public void charge() {
    while (!isFullyCharged()) {
      mBarsCount++;
    }
  }

Don't forget to include the empty brackets after the method call to isFullyCharged().

I hope that helps,

Steve.