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

Simon Shour
Simon Shour
545 Points

Need help with incrementing mBarsCount in while loop.

I can't seem to set up my while loop properly. I think I must be missing a step. If anyone can take a look and tell me what I am doing wrong that would be great.

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 boolean charging(){
    boolean isFullyCharged = false;
    if (!isBatteryEmpty()){
      mBarsCount++;
      isFullyCharged = true;
    }
    while (mBarsCount.charge()){
      System.out.println("Charged!");
    }
  return isFullyCharged;
  }



  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

Dan Johnson
Dan Johnson
40,533 Points

In this challenge you're actually redefining the charge method, so all the new code will go there.

Here's an outline showing how to leverage the new helper method:

public void charge() {
  // While the GoKart is not fully charged:
    // increment mBarsCount
}

The conditional used with the while loop will handle the termination so you won't need a separate if statement.

As Dan Johnson said, you will be redefining the charge method. You will need to replace

mBarsCount = MAX_ENERGY_BARS;

with a while loop.

There is a tutorial on while loops and do-while loops here if you need more help understanding them.

A while loop looks like the following:

while ( an expression that determines whether the code below gets executed goes here) {
     //the code to execute while the expression is true goes here
}

Since the ! operator means "not", and you already have a isFullyCharged() method that checks to see if mBarsCount is full, you can use !isFullyCharged between the parenthesis like so:

while ( !isFullyCharged()) {
     //the code to execute while the expression is true goes here
}

This runs the code in the curly braces while mBarsCount is not full.

Finally, since you need to increase mBarsCount by one, you can use

mBarsCount ++;

between the curly braces like so:

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