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 trialAudi Lou Abatayo
2,645 Pointspublic boolean dispense(){...}
In the code:
public boolean dispense(){
boolean wasDispensed = false;
if (!isEmpty()) {
mPezCount--;
wasDispensed = true;
}
return wasDispensed;
}
Am I wrong to think that this code goes into a loop of dispensing until the Pez Dispenser is actually empty?? I mean, since if the Pez Dispenser isn't empty then the wasDispensed will be true, then the first statement that "boolean wasDispensed = false;" would be false. Then to make it true, then it would have to keep on dispensing until the "boolean wasDispensed = false" becomes true.
or is it if I call the dispense method, only 1 candy gets eaten??
2 Answers
Dennis Mårtensson
7,400 PointsThe 'wasDispensed' boolean variable is first set to false, so it has something to return if the following if statement doesn't execute. Because if the if block does not get executed, then it was neverdispensed.
Then it asks "If this is NOT empty, first decrement the 'mPezCount' variable by ONE, then set 'wasDispensed' to true."
So only one pez gets dispensed with each function call, given it is not empty. Hope that clears up your confusion. (:
Audi Lou Abatayo
2,645 PointsOkay, I get it. Thanks! :D