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 trialDarin Thompson
5,305 PointsAnother method for loading Pez
In the java objects lessons, specifically in the increment and decrement and next the Overloading ideas, we learn the .fill() and then the .fill(int pezAmount).
In the .fill() with no parameters we just set pezAmount = MAX_PEZ; and then go on. They later say you don't want to overfill the pez so a method was created that takes parameters to specify the number of pez we want to add. I understand the parameters and why you would need them, but why not just have a pezAmount = MAX_PEZ - getCurrentPezCount() or something to that nature. That way, there is never an overfilling?
Any insights? Thanks in advance!
1 Answer
Brendon Butler
4,254 PointsUsing the fill()
method will set pezAmount
equal to MAX_PEZ
. This will never overfill as it will always be equal to the max amount.
Using the fill(int)
method will attempt to add the specified quantity to the dispenser. There are many ways to accomplish this, but in this tutorial (from what I recall) wants to implement a loop of some sort. This will allow you to insert one Pez at a time, then stop once you have filled to the MAX_PEZ
quantity.
In a perfect world, you would never attempt to add more Pez than the maximum amount. So you could just simply do pezAmount = pezAmount + quantity
and you would never have issues. However, we do not live in a perfect world, so you need a way to prevent overfilling. You could check to see if pezAmount + quantity
is greater than the maximum amount. If not, add the specified quantity. If so, set the Pez amount to the maximum.
That solution works, however, the problem with this is that it makes you think less about how that physical object is intended to work in the real world (adding one Pez at a time, hence the loop). Which is what object-oriented programming is all about, especially when you're just learning. Also, as I mentioned earlier, there are a bunch of ways to implement a solution to this. The one in the video is just one of these many solutions. Thinking outside of the box like this is a good thing -- continue to do this and to challenge the instructor's methods.
Darin Thompson
5,305 PointsDarin Thompson
5,305 PointsHey man, thanks again for responding to my question. Very knowledgeable. I just keeping on, keeping on.