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 trialJames Pask
13,600 PointsAdding the ! symbol and while loop to check that the GoKart is fully charged.
I think I'm getting confused with our GoKart code compared to the one shown in the example. Craig is putting code into two different files but we're only using one. The "while" part is causing me the most confusion as he appears to be calling code from the second file:
while (dispenser.dispense()) {
System.out.println("Chomp!");
}
I don't really know what to put within the brackets of the while statement since I'm not sure which property I'm supposed to be checking. Because we're incrementing the charge, we'd want it to look at a property for charging (so I used isCharging() but got a whole heap of errors because its not declared anywhere else within the code.
Am I missing something really obvious here?
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 isCharging() {
boolean isFullyCharged = false;
if (!isBatteryEmpty()) {
mBarsCount++;
isFullyCharged = true;
}
return isFullyCharged;
}
while (isCharging()) {
System.out.println("Charging!");
}
}
2 Answers
Kristian Terziev
28,449 PointsYou have everything needed to complete the task. There is no need to add new methods. Now, let's look at the problem:
What does the isFullyChared method do? - Checks if the Kart is fully charged.
What does the charge method do? - Charges the Kart with energy.
What is required in the task? - To increment the Kart's energy until it reached the maximum.
What is to increment? - To increase. In our case with 1. //variable++;
So with those questions answered, how would you increment the current energy while the cart IS NOT fully charged?
Hint: The charge method will be 5 lines long. You're supposed to check if the Kart is fully charged. Therefore you will use a while loop, the isFullyCharged method with the ! symbol (since you need to know when the Kart is not charged) and you will increment something inside the loop.
Try figuring the code on your own. If you have more troubles, be sure to come back and ask again :)
James Pask
13,600 PointsThanks to Kristian the answer finally clicked. Somehow it just came to me whilst I was at work!
I ended up with this code:
public void charge() {
mBarsCount = MAX_ENERGY_BARS;
while (!isFullyCharged()) {
mBarsCount++;
}
}
I think I was just trying to make my life more complicated than it needed to be!
James Pask
13,600 PointsJames Pask
13,600 PointsI've now attempted it again and have put in this code:
I'm getting a compile error about an unreachable statement. In my head the "while" bit should be the charging.. while it's empty, charge it.. Does the While actually mean something different in this case?
I know it still looks wrong - particularly the while (isFullyCharged()) bit but I just can't figure out what it should be.
Kristian Terziev
28,449 PointsKristian Terziev
28,449 PointsAll right. Every method should serve its purpose. The method isFullyCharged should check if the Kart is fully charged, and the method charge should charge the Kart if isFullyCharged returns false. Therefore, it's logical (the task says it as well) to not change the isFullyCharged method since it already does what it should. However, you are told to use it to change the charge method.
The code you've written has couple of mistakes. First of all, you do not need a boolean variable (or any variable for that matter) to return true/false, simply because the method isFullyCharged does that. Second, an if statement runs only one time (unless it's in a loop). Meaning it's not suitable for our goal here. Even more, you are required to use while.
So we made it clear that the isFullyCharged method shouldn't be touched and you should write your code inside the charge method. So what should it do? Simple - increment the Kart's energy bars while isFullyCharged returns false. So now that I've told you what it does, I need you to tell me - Does this method return anything?
Yes - What type should it be then?
No - What type should it be then?
Try to transform my words into code (the once saying what should the method do (after Simple - )) See where it takes you and come back if you need more help.