Java Reference
In-Depth Information
We defined the five dice as global variables for the class because most of the methods
in the class will access them. For example, consider the method that generates the initial
values for the five dice.
public static void rollDice() {
d1 = getRandomDieValue () ;
d2 = getRandomDieValue () ;
d3 = getRandomDieValue () ;
d4 = getRandomDieValue () ;
d5 = getRandomDieValue () ;
}
Since arguments in Java are passed by value and every method can return at most one
entity, it is not obvious how to write a method that returns five values. This is the reason
why the variables for the dice are declared as global variables.
The getRandomDieValue method simply returns a random number between 1 and 6
(the value of the die). It can be implemented as shown below.
public static int getRandomDieValue () {
return ( int ) (Math . random ( ) 6+1) ;
}
Recall that Math.random() returns a random double between 0 (inclusive) and 1(ex-
clusive). Multiplying it by 6 will give us a double in the range [0,6). Adding one will shift
the range to [1,7). When a double in this range is cast to an integer, then the result will be
an integer between 1 and 6.
Our code so far has one major drawback: it is not versatile. For example, suppose that
we decide to modify the game to include four instead of five dice. Then a significant portion
of the code will need to be rewritten. A different shortcoming of our code is that we need
to duplicate some of the code five times: once for every die (e.g., the rollDice method).
It will be beneficial if we can somehow combine the variables d1 , d2 , d3 , d4 ,and d5 so
that we can declare them in a single statement and we can manipulate them using a single
loop. This will make the code more compact. More compact code is usually not only easier
to write, but also easier to maintain (on average, 70% of the cost to develop commercial
software goes into maintenance). Fortunately for us, arrays come to the rescue!
An array allows us to declare multiple variables of the same type in a single statement. In
our example, if we need to create one hundred instead of five dice, then defining one hundred
variables will surely be impractical. However, using an array we can create as many dice as
we need in a single statement.
In Java, an array is allocated using the new keyword.
new i n t [100];
The above line creates an array of one hundred integers. Initially, the array will be
populated with zeros. In general, the new keyword is used to create a new object, where
an array is a special kind of an object. The operator new returns a reference to the object.
The best way to think of this reference is as a unique identifier of the object. This identifier
can be used by the Java Virtual Machine to quickly locate the object. The new keyword
does not return the address of the object because the Java Virtual Machine may move the
object during the execution of the program, while the unique identifier remains constant.
Consider the following code.
System. out . println ( new i n t [100]) ;
A possible result is: “[I@3e25a5”. This means that the allocated space is for an array of
integers. 3E25A5 is the hexadecimal representation of the result of applying a function to
 
Search WWH ::




Custom Search