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

Daniel Maia
Daniel Maia
6,228 Points

I think i got the right code but i am not where to put it :s

I am using the following code:

boolean charged = false while(!isFullyCharged) { mBarsCount++; charged = true; } return charged;

and tried it in the charge() method changing void to boolean but doesnt work. Tried again to use the code in each isBatteryFull(), the isFullyCharged and gone as far as creating my own method with the code above but still not joy.... Please 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 charging() {
  boolean charging = false
    while(!isFullyCharged) {
      mBarsCount++; 
      charging = true;
    }
    return charged;
  }

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

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

}

1 Answer

Michael Hess
Michael Hess
24,512 Points

Hi Daniel,

It looks like you're really close, but there are a few things that you may want to change.

The charge() method is where you want to write your code. We want to use the isFullyCharged() method to help us test whether or not the battery is charged. If the battery is not fully charged we add a bar.

Please see the following code sample:

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

If you have any other questions feel free to ask! Hope this helps!

Daniel Maia
Daniel Maia
6,228 Points

Perfect!! Thank you.

Looking back, I also made a silly mistake and forgot to add () to isFullyCharged() method in the while loop. I also noticed that I didn't need to create a boolean variable because the return on the isFullyCharged() method is 8 bars or not, in which case it increments by one in the while loop created till its meets the 8 bars to then be true to jump out the while loop. :)

Luca Baffico
Luca Baffico
5,408 Points

I can't find the right solution for this challenge. I tried to write exactly what you posted, but it dowsn't work. Also, I don't understand why you put "mBarsCount = MAX_ENERGY_BARS;" in your while loop.

Luca Baffico
Luca Baffico
5,408 Points

No, Ok. I found the solution and it was actually what I believed it should be, but I probably made some changes in the code somewhere else, so I had to refresh the page and re-write it. Now it works.

I was right: "mBarsCount = MAX_ENERGY_BARS;" doesn't have to be in the while loop.

By the way, here you are how it should be:

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