posted 7 years ago

Greetings, i'm a first time poster and yes, I am very new at this so please be patient .

I'm trying to learn Java by reading Bruce Eckel's "Thinking in Java" (2nd edition, old I know) and always seem to run into road blocks regarding what, exactly, each line means. The context of the program listed below is that it's supposed to illustrate the use of

Output:

length of a = 4

a[0] = 0

a[1] = 0

a[2] = 0

a[3] = 0

Questions:

I don't understand line 5 & 6.

Line 5 is supposedly initializing int mod but is left with no quantity during initialization. Perhaps it was intialized to 0 automatically, in which case line 6 becomes return Math.abs(rand.nextInt()) % 0 + 1?

I'm confused about the order of the call...I'm assuming rand.nextInt() occurs first, followed by abs(rand.nextInt()), etc?

What does nextInt() do?

Also, i'm assuming abs() returns an absolute value?

line 10 is also confusing...why pRand(20)?

Do any of you know where I might find a comprehensive list of java method definitions/key words?

Thank you for your help and patience!

I'm trying to learn Java by reading Bruce Eckel's "Thinking in Java" (2nd edition, old I know) and always seem to run into road blocks regarding what, exactly, each line means. The context of the program listed below is that it's supposed to illustrate the use of

**new**to create elements in a array when you don't know exactly how many elements you need. I will number the lines for convenience.Output:

length of a = 4

a[0] = 0

a[1] = 0

a[2] = 0

a[3] = 0

Questions:

I don't understand line 5 & 6.

Line 5 is supposedly initializing int mod but is left with no quantity during initialization. Perhaps it was intialized to 0 automatically, in which case line 6 becomes return Math.abs(rand.nextInt()) % 0 + 1?

I'm confused about the order of the call...I'm assuming rand.nextInt() occurs first, followed by abs(rand.nextInt()), etc?

What does nextInt() do?

Also, i'm assuming abs() returns an absolute value?

line 10 is also confusing...why pRand(20)?

Do any of you know where I might find a comprehensive list of java method definitions/key words?

Thank you for your help and patience!

posted 7 years ago

- 1

What you are looking for is the Java API: http://download.oracle.com/javase/6/docs/api/

It has a rather extensive list of the classes that come as part of the Java language, and the methods they contain (along with descriptions)

It has a rather extensive list of the classes that come as part of the Java language, and the methods they contain (along with descriptions)

Steve

posted 7 years ago

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).

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.

Correct.

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.

Correct. The API for the Math class will explain more here as well.

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:

- 1

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:

Steve

posted 7 years ago

- 1

Welcome to JavaRanch; please UseCodeTags when you post source code - that makes the code much easier to read.