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

Jacob Luallen
Jacob Luallen
1,947 Points

Completely lost!

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....

This is the prompt for this challenge. I am really lost. I think the I need to add something to the effect of "while (mBarsCount.!fullyCharged()) { return charge;}" I am not sure though. I have tried different combinations of the calls but every time I look at the preview there are so many errors it doesn't even help. I am not even sure where to place the while loop. I think I don't understand the wording of the prompt and I also am starting to have a really hard time following the logic. Thanks for any help.

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;
  }

}

3 Answers

I'd put it inside the charge method. So, instead of mBarsCount = MAX_ENERGY_BARS; which is a bit blunt!, try:

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

You're along the right lines, yes - don't worry - this question has caused many to scratch their heads!

Essentially, you want to charge the Kart whilst is is not fully charged. You've part of that already.

How about:

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

The method isFullyCharged returns a Boolean (true/false). The line mBarsCount++ increments the numbers of bars. The while loop essentially continues to increment mBarsCount until isFullyCharged returns true (that is negated by the ! or NOT operator).

I hope that helps!

Steve.

Jacob Luallen
Jacob Luallen
1,947 Points

Yes thank you. I finally read the question correctly and arrived at that exact line of code. However, I still don't know where to place the line of code so it will compile correctly. Any thoughts?

Edit: Okay finally solved it. I needed that line of code you provided and I eventually thought of inside the "charge" function(?). I find it just takes me a second to decipher these prompts. Thank you for the help.

Use the following snippet of code as your example to follow in changing the implementation details of the charge method:

public boolean dispense() {
    boolean wasDispensed = false;
    if (!isEmpty()) {
      mPezCount--;
      wasDispensed = true;
    }
    return wasDispensed;
  }
  • Instead of if, use while
  • increment, rather than decrement
  • The charge method doesn't need to return a boolean, so ignore all of the code involving the boolean wasDispensed

Keep isFullyCharged as it was from the previous challenge.

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