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 trialJames Mackey
Courses Plus Student 994 PointsMy while loop is wrong i think
.
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 charging() {
boolean wasCharged = false;
if (!isFullyCharged()) {
mBarsCount--;
wasCharged = true;
}
return wasCharged;
} while (charging()){
System.out,println("Charging");
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_ENERGY_BARS;
}
}
3 Answers
Jeremy Hill
29,567 PointsIt looks like you might be overthinking it a little. Here is what I got:
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;
while(!isFullyCharged()){
mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_ENERGY_BARS;
}
}
James Mackey
Courses Plus Student 994 PointsSo the while loop allows for the battery to be charged except when its fully charged??? :/...
Jeremy Hill
29,567 PointsThe while loop checks a condition; in this case it is checking to see if the battery is fully charged- if it is false then it will continue to increment the mBarsCount until isFullyCharged returns true. The isFullyCharged() method will check to see if mBarsCount is equal to MAX_ENERGY_BARS, it will return true if mBarsCount is equal. So the while loop runs the code while the condition (isFullyCharged()) returns false, as soon as it returns true the loop ends.
I hope this helps.
Esther Strom
8,028 PointsEsther Strom
8,028 PointsJust FYI, you have a comma in your output where there should be a dot.
System.out,println("Charging");
should be
System.out.println("Charging");