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 trialNathan Beauchamp
2,456 Pointsfalse == 0; ?
I'm setting my isBatteryEmpty return to return mBarsCount ==0; but because false == 0 could i just have the return read return !mBarsCount; I'm pretty sure this works in C++, but I have very little experience in Java.
Or just to be on the safe side, even though the mBarsCount should never be able to go bellow 0, should the return be return mBarsCount < 1;
public class GoKart {
public static final int MAX_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_BARS;
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
}
1 Answer
Dan Johnson
40,533 PointsUnlike in C/C++, conditional expressions must result in type boolean. There is no implicit conversion.
As for using mBarsCount < 1 instead, I would stick with mBarsCount == 0. Negative values would probably indicate a logic error somewhere else in the class which would need to be fixed, rather than be built around.