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

Stas Novsky
Stas Novsky
3,119 Points

i am confused by if/while

i am confused and very sad about this...Don't know what to do

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() {
    if (!isFullyCharged()){
      mBarsCount++;
    }while()
    mBarsCount = MAX_ENERGY_BARS;
    return isFullyCharged;
  }

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

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

}

2 Answers

Chase Marchione
Chase Marchione
155,055 Points

Hi Stas,

  • Since the charge() method is void, you don't want to return anything from it.
  • Per the instructions, you need the negation operator (!) and a while loop.
  • Since you're checking if the current bar count is full: you need to check if the value of mBarsCount is equal to the value of MAX_ENERGY_BARS. Consider what those two variables represent: the bar count, and the maximum energy bars.
  • Since the bar count isn't full, you need to make it full. This doesn't involve returning any value; rather, it involves changing the value of a variable (the mBarCount variable.)

Example code if you're still stuck:

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() {
    if (!isFullyCharged()){
      mBarsCount++;
    } while (!(mBarsCount == MAX_ENERGY_BARS))
    mBarsCount = MAX_ENERGY_BARS;
  }

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

  public boolean isFullyCharged() {
    return mBarsCount == MAX_ENERGY_BARS;
  }
}
Stas Novsky
Stas Novsky
3,119 Points

thanks a lot, man...) it works for me)

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hmmm...

There is not an if/while block, maybe it's being confused with the do while code block?

Your code is so close Stas, don't feel to discouraged! Stick with it!

Try replacing your if with a while. Remember that isFullyCharged() is already doing that == check for you.

You got this!

Stas Novsky
Stas Novsky
3,119 Points

Thank you, Craig! I've done like CJ said and it works for me) your tutorials are increddible)!