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 trial

Java Java Objects (Retired) Harnessing the Power of Objects Incrementing and Decrementing

Chomp 12 time ??

Hi.

i dont really get it...how it know to say chomp 12 times? somebody can help me with this please?

4 Answers

You've got to remember that the while loop will execute until its statement is false. Dispense() is a method which returns a boolean false when the PezDispenser is empty. If it is full, a Pez is dispensed and the method returns true, so the while loop will keep going. I don't remember off the top of my head, but earlier in the code the Pez Dispenser is probably loaded with 12 candies. Thus, dispense will return true 12 times and you'll see Chomp! printed to the screen each time.

But how (dispenser.dispense()) know there is a 12 chomp time?

Because the number of pez in the PezDispenser is stored in a member variable, and the while loop in dispense will execute as long as that member variable is nonzero (so if you have 12 candies, the while loop executes 12 times).

ohhh now i got it thank you very much!

Hey Yadin,

Can you provide some code you're struggling with?

Hi.

When we type this while (dispenser.dispense()) { System.out.println("Chomp!"); }

how it know to print 12 chomp?