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 trialNeelesh Tewani
1,239 Pointsi am not able to run this while loop please help me with this task
// i think at the place of gk there will be something else
public boolean isFullyCharged() {
return mBarsCount == MAX_ENERGY_BARS;
boolean cleared = true;
if(!isBatteryEmpty()){
mBarsCount--;
return cleared = false;
}
return cleared;
while(gk.isFullyCharged()){
mBarsCount++;
System.out.println("your battery is fully charged");
}
}
1 Answer
Ken Alger
Treehouse TeacherNeelesh;
Welcome to Treehouse!
I can see where you are going with your code, but let's take a look at what we are asked to do in this specific challenge:
Task 1
Okay, so let's use our new
isFullyCharged
helper method to change our implementation details of thecharge
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 incrementmBarsCount
.
Wow, that seems like a lot, but it really isn't that bad. We have our isFullyCharged()
method already and it is working as we need it to as it simply returns a Boolean value (True/False) as to whether our battery is fully charged or not. Our task at hand, therefore, is to leave that method alone and work on the charge()
method.
We are asked to use the ! symbol, which is shorthand for not and a while loop, right? The syntax for that in general would look like:
while ( !isFullyCharged() ) {
// do something magical here
}
We are instructed to increase mBarsCount
inside the while loop, and that can be accomplished by mBarsCount++
.
To add all of that together, our charge()
method should look like:
public void charge() {
while ( !isFullyCharged() ) {
mBarsCount++;
}
}
Post back if you are still stuck or have further questions.
Happy coding,
Ken
faraz
Courses Plus Student 21,474 PointsLoved your answer Ken, you explained it great!
Ken Alger
Treehouse TeacherThanks. I do my best.
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherEdited for markup