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 trialjatinder kumar
Courses Plus Student 4,886 Pointswhat is wrong with this code?
public void charge() {
while(!isFullyCharged())
{
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;
}
l
public String getColor() {
return mColor;
}
public void charge() {
while(!isFullyCharged())
{
mBarsCount=mBarsCount+1;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_ENERGY_BARS;
}
}
2 Answers
Jennifer Nordell
Treehouse TeacherAbsolutely nothing is wrong with it. If I take the code that you posted at the top, and paste it in to the current charge
method, the challenge passes! However, if I try to use the code posted in the second bit, it fails because it explicitly
wants you to use the unary ++
operator. The question is why you aren't passing the challenge. My best suggestion is to close the browser and reopen it, and start the challenge again. Paste the code you have in the top section into the current charge
function and press "check work". If it's still failing you might have to contact support. Happy coding!
Leonard Morrison
Courses Plus Student 32,914 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;
}
//l
//^ Get rid of this spare l.
public String getColor() {
return mColor;
}
public void charge() {
while(!isFullyCharged())
{
//Try using the ++ instead of +1.
mBarsCount=mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_ENERGY_BARS;
}
}