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 trialhakan Gülay
8,489 PointsmPezcount=newAmount why we use this ?
Can anyone explain to me why we use mPezcount=newAmount here ? it was written in 2 minute of video
2 Answers
Vrezh Gulyan
11,161 PointsOriginally in the beginning of the video the load() method would just add a pez to the total amount each time you called load.
public void load(int pezAmount){
mPezcount += pezAmount;
}
+= indicates is the same as saying : mPezCount = mPezCount + pezAmount;
This was incorrect to do since the pez dispenser can only hold so many pez. What the instructor does is add a if statement which throws and exception ( something that lets you know something is wrong) to let you know if the pez dispenser is full. In the new code that you see mPezCount isn't being set. Instead newAmount is being set to mPezCount + pezAmount;
Next the method checks to see if newAmount is more than the maximum allowed by the dispenser.
If the code doesn't throw an exception then we get to the bottom line of code which you asked about which just does what we were doing originally
mPezcount = newAmount
which is equivalent to
mPezcount = mPezCount + pezAmount;
which is equivalent to the original code
mPezcount += pezAmount;
To sum it all up, the new code that is written in the load method just checks to see if the total amount of pez will be too high before assigning it to mPezcount. Hope this helps.
hakan Gülay
8,489 Pointsthx sir :) ı got it now thanks so much
Vrezh Gulyan
11,161 PointsNo problem :) Mark as best answer please.