Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?