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

Berkan Sonmez
Berkan Sonmez
3,403 Points

Cant understand why and where should i add boolean to this code also it not working! Please help me to fix it!

On the video Craig was using booleans but i dont know where to use those on this code.

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


  }

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

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

}

4 Answers

Christopher Augg
Christopher Augg
21,223 Points

Correct. The only thing required is to change the implementation of the charge method. You did that correctly by using a while loop that calls the new isFullyCharged helper method. That method is already going to return a boolean. Your while loop will test on every iteration to see if it is NOT charged and increment mBarsCount if that is the case since !isFullyCharged() is what the instructions said to do until mBarsCount is equal to 8 because then it is fully charged and breaks the while loop.

Your error was only because you have an extra } after the charge method:

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


              }

That is closing your class and will give you 8 or so compile errors because the methods below this point are no longer part of it.

I tested this with treehouse and in Eclipse to make sure. So I know it works.

Regards,

Chris

Christopher Augg
Christopher Augg
21,223 Points

Heya Berkan. It looks like the only issue you have is an extra } after the charge method. You don't have to keep a boolean variable to hold any values since you just return a boolean of the isFullyCharged method and test that it is NOT ! ... in which you have done correctly.

Please feel free to let me know if this did not help.

Regards,

Chris

Berkan Sonmez
Berkan Sonmez
3,403 Points

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.

Berkan Sonmez
Berkan Sonmez
3,403 Points

this is the task

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.

I believe you are missing a line of code in the public void charge() method. Add in the line mBarsCount = MAX_ENERGY_BARS; This should solve your problem.

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

Christopher Augg
Christopher Augg
21,223 Points

Linnea,

I'm sorry, but that is not the correct implementation. The instructions state:

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

Your code would assign mBarsCount to the max before ever going through the while loop. Therefore, the while loop would break out instantly because the !isFullyCharged method would return false and mBarsCount++ would never happen. However, this will compile and run without complaining though. It just does not work as the instructions intended.

Regards,

Chris