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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Brandyn lee murtagh](https://secure.gravatar.com/avatar/1189343298572c71c3bff60609cb5a98?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Brandyn lee murtagh
432 PointsJava 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?
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](https://secure.gravatar.com/avatar/c2b2872f48c222732218df9be85e1ed2?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Stone Preston
42,016 Pointsthe 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++;
}
}
Ismail Alssaka
1,475 PointsAnd make sure the
mBarsCount = MAX_ENERGY_BARS;
line is still inside the charge() method as well :)
That should fix the issue.
![Stone Preston](https://secure.gravatar.com/avatar/c2b2872f48c222732218df9be85e1ed2?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Stone Preston
42,016 PointsI 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
1,475 PointsOh 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](https://uploads.teamtreehouse.com/production/profile-photos/1585642/micro_portal-aperture-laboratories_00435228.jpg)
James N
17,864 PointsThanks! this helped me out as well!
![Jeremy Hutson](https://secure.gravatar.com/avatar/ef52c0cb314ade8b57a2403f35f66a1b?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Jeremy Hutson
4,371 PointsMy 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](https://secure.gravatar.com/avatar/1d445b4429791f0c5e11d3410c4d5ccb?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Drew Warren
4,565 PointsThank 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
Brandyn lee murtagh
432 PointsBrandyn lee murtagh
432 PointsHi mate i still cant seem to get it
Stone Preston
42,016 PointsStone Preston
42,016 Pointsdid you put the while loop inside the charge method? see my last code block above
Ben Morse
6,068 PointsBen Morse
6,068 Pointsthanks 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.
MUZ140584 Heredity Mbundire
4,979 PointsMUZ140584 Heredity Mbundire
4,979 Pointsthank you guys am saving by the bell
Edane Barton
11,457 PointsEdane Barton
11,457 PointsI 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
1,797 PointsRobert Abreu
1,797 PointsThank you! I overlooked the part about writing the code in the charge method.