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 trialkabir k
Courses Plus Student 18,036 PointsImplementation of the charge() method in the `Incrementing` Challenge Task
In the last challenge where we were asked to change the implementation details of the charge() method, is the original code line
mBarsCount = MAX_ENERGY_BARS; // is this code needed?
in the new implementation of the charge() method
public void charge() {
while(!isFullyCharged()) {
mBarsCount++;
}
mBarsCount = MAX_ENERGY_BARS; // is this code line needed in this method?
}
needed?
I wrote the charge() method as above and it passed but I saw someone write the same method WITHOUT that code line in the forum and I wanted to be sure if the method is correct without that code line.
And if the method is correct WITH that code line, can someone do a walk-through of how it works. I understand the while loop part. It's the last line I'm not sure how it fits in the method when we already have the while loop that seems to be doing the same thing.
2 Answers
kabir k
Courses Plus Student 18,036 PointsNever mind, I tested both scenarios and I found out that the challenge passes for both cases. So it seems the test is just looking for the presence of the while loop in the charge method. So, I believe it should be written without the code line which makes more sense since the charging incrementally leads up to the maximum charge. Hence, that line is not required.
Grigorij Schleifer
10,365 PointsHi Kabir,
you can delete this line
mBarsCount = MAX_ENERGY_BARS;
Your new charge method is incrementing the mBarsCount in dependance of the isFullyCharged() method state and not setting mBarsCount to the maximum without looking how much bars are inside the battery.
public void charge() {
while(!isFullyCharged()) {
mBarsCount++;
}
}
Grigorij
kabir k
Courses Plus Student 18,036 PointsThanks Grigori for your response
Grigorij Schleifer
10,365 PointsHey there,
if you let the line there it happens nothing. The compiler will enter the charge method and proof the while condition. Until the result of the isFullyCharged method is not true (see the return statement) the mBarsCount will be increased. When the count is 8 the !isFullyCharged() is false and the compiler leaves the loop to go straight to the next line of your prevoius code.
There the mBarsCount ( is = 8 ) will set up to 8 again so the code runs without errors
See you in the forum :)
Greg
kabir k
Courses Plus Student 18,036 PointsThanks, you too.