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 trialgramjoirps
2,048 PointsCan someone help me with this question?
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.
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() {
boolean isFullyCharged = false;
while (!this.isFullyCharged()){
mBarsCount++;
return mBarsCount == MAX_ENERGY_BARS;
}
}
}
8 Answers
MUZ140117 Walter Moyo
7,289 PointsHie. Looking at the challenge what you need to do is change the code for the charge() method. so first go to the charge method and delete the 'mBarsCount = MAX_ENERGY_BARS;' because we wont be needing it anymore. Then inside the curly brackets of the charge() method type: while(!isFullyCharged()){mBarsCount++;}. So the code reads "while its not fully charged, add 1 to mBarsCount until it reaches 8". The charge method's return type is void so the method does not return anything. That worked for me
Gloria Dwomoh
13,116 PointsThe challenge asks this from you "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."
Take notice of what I have made bold. Now it asks you to use "!" because isFullyCharge returns a boolean. True or false according to if it is fully charged or not. For that reason it wants you to keep charging the battery in the function "charge" as long as "isFullCharge" is false, meaning the battery is not full. I hope this helps clear some things up for you :)
bradley daley
504 Pointspublic void charge() { while (!isFullyCharged()) { mBarsCount++; } mBarsCount = MAX_ENERGY_BARS; }
vicky sara
746 Pointspublic 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() { while(!isFullyCharged()) { mBarsCount++; } }
public boolean isBatteryEmpty() { return mBarsCount == 0; }
public boolean isFullyCharged() { return mBarsCount == MAX_ENERGY_BARS; }
}
vicky sara
746 PointsThis code worked for me
ashwinjangam2
2,795 PointsThank you Moyo, I had the same problem and its working fine for me too now.
Stephanie Walker
4,158 PointsAhh Gloria, you have just saved me form myself haha! Thank you so much I've been scratching me head on this for ages haha
Gloria Dwomoh
13,116 PointsYou are welcome. I'm glad I was able to help.
Jeremy Hutson
4,371 PointsI had the same error mBarsCount ++; Simple syntax, really. Once it's right, it's almost plain English.
Stephanie Walker
4,158 Pointswell I haven't got a clue where I am going wrong this is my code...
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() {
while (!isFullyCharged()) {
mBarsCount ++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_ENERGY_BARS;
}
}
Any help would be greatly appreciated, thank you x
Stephanie Walker
4,158 Pointsthe error i am getting says Bummer! Use ++ to increment the mBarsCount member field.
Gloria Dwomoh
13,116 PointsRemove the space from .... mBarsCount ++;