Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need definitions of nextInt(), abs() and some other questions

 
Andy Izumo
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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!
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch; please UseCodeTags when you post source code - that makes the code much easier to read.
 
Andy Izumo
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the welcome (and the reminder), and thank you very much for your help Steve. I should've made the connection between line 5 & 11.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic