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 trialBenneth Paredis
4,597 PointsJava objects incrementing code challenge - need help
It say's that deCharged is wrong but i dont understand why. Can someone help me ?
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 isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_ENERGY_BARS;
}
public boolean deCharged () {
boolean wasDeCharged = false;
if (isFullyCharged()) {
mBarsCount--;
wasMinusCharged = true;
}
while (GoKart.deCharged()){
System.out.printf ("bzzz!");
}
}
}
3 Answers
Steve Hunter
57,712 PointsHi there,
Have you linked to the challenge you are struggling with? It involves amending the charge
method?
Just checking as I wrote a long answer on this yesterday. Have a look at this thread for that.
Let me know how you get on.
Steve.
Kourosh Raeen
23,733 PointsBenneth, the instructions for the challenge are asking you to change the implementation details of the charge method, not to create a deCharged method. You need to add a while loop to the charge method. Then inside the loop you need to increment mBarsCount. The loop should terminate when the battery is fully charged and for that we need to use the isFullyCharged method. Take a look at the following code:
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() {
while(!isFullyCharged()) {
mBarsCount++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_ENERGY_BARS;
}
}
Justin Wilson
461 Points*facepalm
I have been working on this for 2 hours trying to figure it out on my own. Its as simple as 2 lines of code. Thanks for the help guys.