Andy Izumo wrote:
I don't understand line 5 & 6.
Line 5 is supposedly initializing int mod but is left with no quantity during initialization.
No, line 5 is a method definition - the method name is pRand, it returns an int value and it takes an int parameter (the name of the parameter is mod).
Perhaps it was intialized to 0 automatically, in which case line 6 becomes return Math.abs(rand.nextInt()) % 0 + 1?
No, the value of mod is assigned when the method gets called. In line 11. it gets called as pRand(20) - so when the method pRand gets executed, mod has a value of 20. So line 6. returns the resultant of Math.abs(rand.nextInt()) % 20 + 1. Of course 'mod' is variable, so it is 20 this time, but could be any value passed in to the pRand() method.
I'm confused about the order of the call...I'm assuming rand.nextInt() occurs first, followed by abs(rand.nextInt()), etc?
Correct.
What does nextInt() do?
The API will describe more detail - but for this purpose: Random is a pseudo random number generator. nextInt() returns the next integer created by the random number generator.
Also, i'm assuming abs() returns an absolute value?
Correct. The API for the Math class will explain more here as well.
line [11] is also confusing...why pRand(20)?
Line 11 (in my code tagged sample above) is making a new array of some unknown size. pRand() gives us some number, which we can't predict a value (thus the 'array of unknown size'). For the purpose of the exercise you probably aren't expected to know exactly what pRand() does, just that it generates an unpredictable value. In practice, the '20' is a cap on the size of the number which will be returned - you can expect pRand(20) to give you some number between 1 and 20. If you called pRand(3) you would get some number from 1 to 3. It may have been more clear if the author wrote:
and if he made the method signature for pRand like this: