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 trialISAIAH S
1,409 PointsI don't understand the Code Challenge "Incrementing and Decrementing". GoKart
Challenge Task 1 of 1
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.
thanks,
ISAIAH
3 Answers
Steve Hunter
57,712 PointsOK, so you've got a method called charge
in which the mBarsCount
variable is altered. At the moment, it just sets it to MAX
. The challenge is to change this.
There's a method isFullyCharged()
that returns a Boolean to say whether the charge is complete, or not, i.e. is mBarsCount
at its maximum possible amount.
So, we want to put a loop together to charge the kart only when it isn't already fully charged. It suggests using a while
loop for this, which makes sense.
Along the lines of, while the kart isn't fully charged add one to the charge until it is fully changed.
That looks like,
public void charge() {
while ( !isFullyCharged() ) {
mBarsCount++;
}
}
So, while the isFullyCharged
function returns false (the !
is the not
operator), add one to mBarsCount
. As soon as it is fully charged, don't!
I hope that helps.
Steve.
ISAIAH S
1,409 Pointsthat worked.
thanks,
ISAIAH
David Spell
899 PointsI am having the same problem. What's wrong?
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 (!isFullyCharged) {
mBarsCount ++;
}
}
Steve Hunter
57,712 PointsHi David,
The challenge here needs you to re-write the charge()
method. You have amended the isFullyCharged()
method. You need to use isFullyCharged
as this tells you whether the battery is fully charged or not, but you don't need to change it. Set it back to:
public boolean isFullyCharged() {
return mBarsCount == MAX_ENERGY_BARS;
}
Now, in the charge()
method, you want to put the code you had in the other method. So, charge()
looks like:
public void charge() {
while (!isFullyCharged()) {
mBarsCount++;
}
}
Don't forget to include the empty brackets after the method call to isFullyCharged()
.
I hope that helps,
Steve.
anthonybeckles
2,065 Pointsanthonybeckles
2,065 PointsThanks for this post, my brain started hurting from trying to figure out what I missed haha :D
Zandre Alberts
2,613 PointsZandre Alberts
2,613 PointsThat shoud work but mine cant indentefy the charge funtion
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi Zandre,
Can you post your code and I'll take a look.
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsZandre,
I received your code in my email but not on here.
You have add an alternative
charge()
method after yourisFullyCharged
method. If you put that code inside the existingcharge
method then you're absolutely fine.Steve.
Ruslans Melniks
2,867 PointsRuslans Melniks
2,867 PointsGreat explanation, thank you so much!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsNo problem!