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 trialDov Brodkin
2,695 PointsWhile java loops
I do not understand what is wrong with this code, I get about 9 errors, does anyone know what is 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;
return isFullyCharged();
}
while (!isFullyCharged()){
mBarsCount++;
}
}
Jennifer Nordell
Treehouse TeacherDov Brodkin the charge
method calls/invokes/executes the isFullyCharged
method. That method then returns a boolean that tells us whether or not it's fully charged. If it's not fully charged, our while loop will run and charge it
Dov Brodkin
2,695 PointsThank you very much.
2 Answers
Jennifer Nordell
Treehouse TeacherI'm going to give a hint here, because by the looks of it you've got this pretty well under control otherwise. That while loop should not be in the isFullyCharged
method. It's supposed to be in the charge
method. We're first checking to see if it's fully charged. If it isn't, then we're going to charge it. Hope this helps!
Jennifer Nordell
Treehouse TeacherSimon Coates is correct. That while loop isn't even inside the isFullyCharged
method. I didn't see the closed braces above it. In fact, it's sort of just floating around your class
Jennifer Nordell
Treehouse TeacherBecause the challenge explicitly says it needs to go in the charge
method. Here are the relevant instructions.
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.
But currently, your code isn't in any method whatsoever.
Dov Brodkin
2,695 PointsWhen I write this: while (!isFullyCharged), do I need to write it like this : while(!isFulllyCharged())
Simon Coates
28,694 Pointsyou need the () as the isFUllyCharged is a method call.
Jennifer Nordell
Treehouse TeacherYour code is perfectly correct as it was. It's just misplaced. It simply wants you to change how the charge
method functions. Take a look:
public void charge() {
while (!isFullyCharged()){
mBarsCount++;
}
}
Simon Coates
28,694 Pointsyour while loop is outside the method body. And you should probably get rid of the second return statement inside the isFullyCharged method.
Dov Brodkin
2,695 PointsWhy does it need to be in the method body, why can't it be a new method.
Jennifer Nordell
Treehouse TeacherNo the return statement needs to stay
Simon Coates
28,694 PointsThe second return statement? Unless i'm missing something it will never execute, and if it executed is recursive.
Jennifer Nordell
Treehouse TeacherOh woops! You're right! I missed the second return statement. Yeah, that needs to go!
Dov Brodkin
2,695 PointsDov Brodkin
2,695 PointsDoes it automatically return the method?