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 trialAmit Dhamankar
4,163 PointsI am still not able to solve this exercise. Our colleague gave me a Hint, but still I am not able to figure it out
Make the use of the charge method using isEmptyCharged id very confusing.
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;
}
}
3 Answers
Steve Hunter
57,712 PointsHi Amit,
For this, you need to modify the existing charge()
method. The requirement is to increment mBarsCount
only while
the Kart is NOT fully charged. The condition for the while
loop uses isFullyCharged
but you need to negate that with the not operator. That way, once the battery is fully charged, the while loop terminates. Inside the loop, just increment mBarsCount
.
Your code could look something like this:
public void charge() {
while(!isFullyCharged()){
mBarsCount++;
}
}
I hope that helps,
Steve.
Thomas Nilsen
14,957 PointsOkay -
change
public void charge() {
mBarsCount = MAX_ENERGY_BARS;
}
to
public void charge() {
while(!isFullyCharged()) {
mBarsCount++;
}
}
Amit Dhamankar
4,163 PointsNielson Thanks a lot
Amit Dhamankar
4,163 PointsAmit Dhamankar
4,163 PointsHello Thanks to you Steve. I was struggling with it for 2 days. Thanks a lot