File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Need definitions of nextInt(), abs() and some other questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need definitions of nextInt(), abs() and some other questions" Watch "Need definitions of nextInt(), abs() and some other questions" New topic
Author

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

Andy Izumo
Greenhorn

Joined: Nov 18, 2010
Posts: 2
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Oct 13, 2005
Posts: 38441
    
  23
Welcome to the Ranch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Andy Izumo
Greenhorn

Joined: Nov 18, 2010
Posts: 2
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Need definitions of nextInt(), abs() and some other questions