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 trialBrie Spangler
927 PointsCode Challenge: Incrementing HALP
Hi guys! I am stuck on this challenge for a day now and have tried all the solutions I found in earlier questions, but they are not working for me. I even asked a developer friend for help and he gave me some code:
int factorial(n) { if (n == 1) return 1; // because 1! = 1 else return n * factorial(n-1); }
But it didn't work either.
I'm afraid I'm stuck in this pickle! Can someone please help me so I can continue with the lessons?
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() {
boolean wasCharged = false;
if (!isFullyCharged()) {
mBarsCount--;
wasCharged = true;
}
while (!isFullyCharged()) {
mBarsCount++;
}
{
}
return mBarsCount == MAX_ENERGY_BARS;
}
}
3 Answers
Grigorij Schleifer
10,365 PointsHi Brie,
you are soooo close ...
In this challenge you donΒ΄t need to modificate the isFullyCharge method at all because this method is already perfect. This method returns true if the GoKart is full charged or false if not. Use it inside the charge method to proof the charge state of your GoKart.
Before you charge your phone ... you are asking yourself ... is my phone charged or not ? .... Do I need to charge it ? .... you can apply the same logic here.
Inside your charge method you can create a while loop. Inside the parenthesis you need a condition that is "true"
while (!isFullyCharged()) // while GoKart is not fully charged the condition is true (mBarsCount == MAX_ENERGY_BARS is not equal)
please increment the mBarCount
Look at this code:
public void charge() {
while (!isFullyCharged()) {
// while not fully charged please increment the mBarsCount
//
mBarsCount++;
}
}
You can delete "mBarsCount = MAX_ENERGY_BARS" inside charge() because you set the mBarsCount if your GoKart is not FullyCharged :)
Let us know if you need more help and donΒ΄t give up
Grigorij
Brie Spangler
927 PointsThank you!! You are a good teacher. :)
Grigorij Schleifer
10,365 PointsYou are very welcome :)
Hit Best Answer, so other can see that your question was answered.
Thx
Greg
Christiaan Quyn
14,706 PointsHi I basically encountered the same problem .. but what I don't understand is why does the 'while' loop appear after 'public void'. Why do I place it there and why not anywhere else ?
public void charge() { while (!isFullyCharged()) { mBarsCount++;
Grigorij Schleifer
10,365 PointsHi Christiaan,
you place it inside the charge method (inside {} of the charge () method), so when you call the charge method the while loop will be executed
Brie Spangler
927 PointsBrie Spangler
927 PointsHi Grigorjj!!
Thank you so much for your help! I appreciate it very much. Unfortunately I am still having trouble with this challenge. I keep getting an error, so I will keep trying.
Watch this space, lol.
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHey Brie,
your code should looc like this:
Grigorij