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

Java objects quiz

Hi so I have no idea how I would do :

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.

Anyone 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;
  }
  while (!isBatteryEmpty()) {

  }
}

5 Answers

Stone Preston
Stone Preston
42,016 Points

the task states: 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.

so we want to do something while a certain condition is true. and that condition that needs to be true is that the kart is NOT fully charged (we only want to charge until it reaches max charge). this is the perfect time to use a while loop.

how can we check if the cart is fully charged? you can use the isFullyCharged() method. however we want to make sure the cart is NOT fully charged so we put a ! (the not boolean operator) in front

while (!isFullyCharged()) {

// do something while the cart is NOT fully charged
}

Now you need to add some code in the loop body. we need to increment mBarsCount by 1 using the postfix ++ autoincrement operator

public void charge() {
    while (!isFullyCharged()) {

      mBarsCount++;
    }
  }

Hi mate i still cant seem to get it

Stone Preston
Stone Preston
42,016 Points

did you put the while loop inside the charge method? see my last code block above

Ben Morse
Ben Morse
6,068 Points

thanks for the help. I'm glad that treehouse has a forum. I would have never got this. For some reason the task it was asking just did not make sense to me.

thank you guys am saving by the bell

Edane Barton
Edane Barton
11,457 Points

I may be over complicated the matter, but why did we needed to put the WHILE loop inside of 'charge'? 1) Shouldn't we have created another function that states 'charging'? 2)In other languages, could you not have had the while loop outside of a function?

Robert Abreu
Robert Abreu
1,797 Points

Thank you! I overlooked the part about writing the code in the charge method.

Ismail Alssaka
Ismail Alssaka
1,475 Points

And make sure the

mBarsCount = MAX_ENERGY_BARS;

line is still inside the charge() method as well :)

That should fix the issue.

Stone Preston
Stone Preston
42,016 Points

I think that line actually needs to be removed completely right? since we are looping until the kart is fullyCharged its going to be equal to the max when the loop is done. otherwise whats the point of the loop

Ismail Alssaka
Ismail Alssaka
1,475 Points

Oh right good point lol. I didn't remove the line and it still said it was correct but I can see why you need to remove it, otherwise it would keep setting the mBarsCount equal to the MaxEnergy which would mean the while loop is useless.

thank you!

James N
James N
17,864 Points

Thanks! this helped me out as well!

Jeremy Hutson
Jeremy Hutson
4,371 Points

My error? Simple syntax mBarsCount ++;

See my mistake? ;-)

I definitely didn't catch on about deleting the previous statement in the charge method though. Thanks! Makes perfect sense going back and reading it after it's right. Almost like plain English.

Drew Warren
Drew Warren
4,565 Points

Thank you to the original poster and all the answers. I had been stuck on this one and was feeling very discouraged and not meeting my daily goals. With your help I am now back on track!

Thanks again,

Drew