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 trialDominic Cruz-Pena
546 Pointsneed of help
Create a new public method named isBatteryEmpty that returns true if the battery has 0 bars remaining, and false otherwise.
class GoKart {
public static final int MAX_BARS = 8;
private String color;
private int barCount;
public GoKart(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void charge() {
barCount = MAX_BARS;
}
}
1 Answer
Joseph Wasden
20,407 PointsA good place to start would be to examine the existing code and see if there are parts of it you can replicate to answer this question. For example, are there other examples of public methods that return a value in this code? what parts of them can we imitate? Also, does the question prompt us in a certain direction?
In this case, the question tells us we need the method to be public. we also know that we want it to return a true or false value, so that's a boolean, right? And what should we name it? isBatteryEmpty. So let's start with that.
public boolean isBatteryEmpty() {
return true;
}
Great! now we have a public method that returns a boolean. However, as it is currently written, it will always return true. The challenge indicates that it should only return true if the battery is at 0 bars. that sounds like it has a condition...like a conditional statement... like an if statement?
public boolean isBatteryEmpty() {
if (barCount == 0) {
return true;
} else {
return false;
}
}
So, to walk this code line by line when it is executed, if the barCount is set to zero, then it will return a value of true, else (meaning if it is not set to zero) then it will return a value of false.