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

Chaz Hall
Chaz Hall
1,970 Points

Don't understand what charge variable is doing.

I understand what most of the code is doing. However, the charge variable states that:

mBarsCount = MAX_ENERGY_BARS //which is 8 in this exercise. However, then we are to include a while loop that referenced variable isFullyCharged and asks if that is false and if it is, increase mBarsCount by 1.

I'm confused, how does this work? It seems that mBarsCount would immediately be equal to MAX_ENERGY_BARS. Sorry if I'm not asking this question in a more understandable way.

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

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

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

}

1 Answer

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey

Think of it like this. If I call isFullyCharged. It uses == to compare the number of bars I have to the max number. It will either give me a true or it will give me a false. I know this for the method returns a boolean based on the double == which compares two values. So 1 == 2 is false. 1 == 1 is true. We want to get to 8 == 8. That being said if mBarsCount is not 8, it will return a false, that means I don't have 8 bars. Which means I want to call the charge method.

Call the charge method and in the charge method I have to use ! and a while loop. So ! basically turns false into a true or a true into a false. The while loop runs while something is true. Although, if I am in the charge method, it's because I don't have 8 bars. So I would expect isFullyCharged() to return a false. At least until it's fully charged.

My while loop wont run on a false value. It needs a true value. So I can do something like this

while (!isFullyCharged())

This says ohh ok, as long as I keep getting false. The ! will turn it into a true. So while true, increment mBarsCount. I can do this several ways, but the easiest is to use ++

while (!isFullyCharged()) {
    mBarsCount++
}

That runs through one time. Than the while calls is fully charged and if it's not, it will add to mBarsCount again. It will do this until isFullyCharged() returns a True, which because of the ! really means it will return true but be changed to false. A false value will exit the loop while fully charged. Concluding the exercise.

Here is the full code below.

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

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

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

}


Let me know if this makes sense. If not I can definitely try to explain it another way. No worries!