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 trialJari Koopman
Python Web Development Techdegree Graduate 29,349 PointsWhy use 'while' and were place it and how finish the challenge?
Okay, dear reader, I'm full of questions again :-)... but willing to solve it! First question: do I use while (instead of if) because I don't know where the loop ends? (Should I use if, when it says: it has to charge at a stage of 4; so it is a special number?)
Do I have to place the 'while loop' at the public boolean isFullyCharged? or (like I did) near the other part with 'charge'? And how do I normally know where to place an extra code....
I think I wrote: while it is not fully charged (!isFullyCharged()), then put 1 extra to it (mBarsCount ++), but it doesn't work, so I think I'm missing something.
Please can someone help me out? Thanks!
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;
while (!isFullyCharged()){
mBarsCount ++;
}
}
public boolean isBatteryEmpty() {
return mBarsCount == 0;
}
public boolean isFullyCharged() {
return mBarsCount == MAX_ENERGY_BARS;
}
}
2 Answers
Steve Hunter
57,712 PointsHi Jari,
You can use a while
loop when you don't know how many times, or if at all, you want to loop through a set of code. Th loop iterates while
a certain condition is, or is not, in place. The condition may exist already, so the loop won't run, or it may never occur, so the loop will continue forever. Usualy, you add a control mechanism into the loop so you make sure the loop will stop at some point, but that isn't necessary; some things are meant to persist permanently, perhaps. An if
statement tests something once, a loop tests it as many times as is necessary. If you do know how many iterations you want, such as iterating over the contents of an array of known length, you can use an alternative for
loop.
In your code, you have controlled the while
loop well. Your test is correct and you will increment mBarsCount
until isFullyCharged
returns true
. In isFullyCharged
you are testing to see if mBarsCount
holds a value equal to MAX_ENERGY_BARS
. So, there's no need for the line in your code that assigns the value of MAX_ENERGY_BARS
to mBarsCount
- that is why you are incrementing in the first place.
So your loop carries on until mBarsCount
equals MAX_ENERGY_BARS
through the use of the helper method isFullyCharged
. The while
loop replaces the assignment of MAX_ENERGY_BARS
. Your charge
method will look like:
public void charge() {
while(!isFullyCharged()){
mBarsCount++;
}
}
I hope that helps.
Steve.
Rajen Tandel
25,511 Points- Yes, use while, if you were to use an "if" statement, the code inside the block would only run once, and increment the variable only once.
- Your while loop is correct. It is basically saying "while NOT fully charged, keep adding energy bars". Once it is full, then set the mBarsCount to MAX_ENERGY_BARS.
- You're really close. The problem is that your while loop should come before the assignment.
while (!isFullyCharged()){
mBarsCount ++; // no space before ++
}
mBarsCount = MAX_ENERGY_BARS // unnecessary line and omitted trailling ;
[MOD: added correction comments]
Jari Koopman
Python Web Development Techdegree Graduate 29,349 PointsThanks Rajen! Happy to see I was almost there :-)
Jari Koopman
Python Web Development Techdegree Graduate 29,349 PointsJari Koopman
Python Web Development Techdegree Graduate 29,349 PointsThank you Steve, your answer really helped me to get further!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHey, no problem! I'm glad you got through the challenge and understood the concepts.