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 trialDamir Demirovic
Courses Plus Student 446 PointsWhat am i missing here?
I need to have method that says whether or not battery is empty.
public class GoKart {
public static final int MAX_BARS = 8;
private String mColor;
private int mBarsCount;
public isBatteryEmpty() {
if (mBarcCount == 0) {
charge();
}else{
System.out.printf("No need to be charged");
} }
public GoKart(String color) {
mColor = color;
mBarsCount = 0;
}
public String getColor() {
return mColor;
}
public void charge() {
mBarsCount = MAX_BARS;
}
}
2 Answers
Steve Hunter
57,712 PointsHi Damir,
You need to create a helper method that returns whether the battery is empty or not. The battery being empty, as you have coded, is represented by mBarsCount
being equal to zero, mBarsCount == 0
.
A helper method would normally return just a true
or false
when used; a boolean
. Inside the method, we can just return the result of the above comparison. The caller of the method can then decide how to use the returned boolean to, say, call the charge()
method, or not. The helper just answers the question, "is the battery empty?" with a true or a false.
One solution could look like:
public boolean isBatteryEmpty(){
return mBarsCount == 0;
}
I hope that makes sense.
Steve
Shane May
5,114 PointsYou need to return a data type after 'public' and before 'isBatteryEmpty()'