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 trialJae Kim
3,044 PointsStill confused on Method Signatures.
Hey everybody!
So, I just landed on the video where Craig talks about Method Signatures and am having some trouble understanding the load() method he creates.
Can anyone explain to me in these blocks of code what exactly is going on?
My confusion stems from the first line of the code where 'load(MAX_PEZ)' is utilized. So if I get this straight utilizing the load method calls upon 'load(MAX_PEZ)' which will in turn load the PEZ dispenser to the default max amount of PEZ being 12?
However, when we go back to the Example.java file when inputting a number into dispenser.load() does the default value change to what the user input is?
I apologize for the long question and your help would be most appreciated! public void load() { load(MAX_PEZ); }
public void load(int pezAmount) { int newAmount = mPezCount + pezAmount; if(newAmount > MAX_PEZ) { throw new IllegalArgumentException("Too many PEZ!!!"); } mPezCount = newAmount; }
2 Answers
Evan Demaris
64,262 PointsHi Jae,
The reason there are two methods is so we will have a default load() with no arguments - which when called, will assume a full load is required (no Pez currently in dispenser), because of how it's been written. And then, to create a specific load() which won't be used unless an argument to load only that many Pez is provided. So if you called the load() on a Pez dispenser object with any Pez currently in it, you'll throw an exception - you'd need to know how many were in the Pez dispenser object, and add an appropriate amount. But if you had 2 Pez in a Pez dispenser object, you could add up to 10 more with a call to load(10). You could make more load() functions with different arguments - for example, a load(String pezFlavor, int pezAmount) if you wanted to add an option to load flavored Pez - and still keep these functions for default flavored Pez, or loading a full count of default flavored Pez.
// Reference the second function with the MAX_PEZ constant as the argument, 12.
public void load() { load(MAX_PEZ); }
public void load(int pezAmount) {
// The new amount will be the member variable of the current Pez count
// plus the number of Pez being passed as an argument.
int newAmount = mPezCount + pezAmount;
// Check if the number of Pez being added by the argument will be over the MAX_PEZ amount.
if(newAmount > MAX_PEZ) {
throw new IllegalArgumentException("Too many PEZ!!!");
// If an exception isn't thrown by having too many Pez, then make the member variable
// equal to the new amount of the old Pez count plus the ones added by this function.
} mPezCount = newAmount;
}
Please let me know if you have any other questions about method signatures!
Jae Kim
3,044 PointsOkay, this makes more sense now! One method is so that when no arguments are passed the PEZ count refills with a full load while the other method takes an argument passed within dispense.load(); and checks to see how many PEZ should be loaded to get MAX_PEZ. If the argument passed goes over 12 then an exception is thrown.
Thank you so much Evan!