Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Harnessing the Power of Objects Method Signatures

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

Implementation 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
PLUS
kabir k
Courses Plus Student 18,036 Points

Never 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
Grigorij Schleifer
10,364 Points

Hi 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

Grigorij Schleifer
Grigorij Schleifer
10,364 Points

Hey 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