Java Reference
In-Depth Information
no arguments, so it has an empty parameter list. Each time it's called, rollDice returns
the sum of the dice, so the return type int is indicated in the method header (line 68).
Although lines 71 and 72 look the same (except for the die names), they do not necessarily
produce the same result. Each of these statements produces a random value in the range 1-
6. Variable randomNumbers (used in lines 71-72) is not declared in the method. Instead it's
declared as a private static final variable of the class and initialized in line 8. This en-
ables us to create one SecureRandom object that's reused in each call to rollDice . If there
were a program that contained multiple instances of class Craps , they'd all share this one
SecureRandom object.
Method main 's Local Variables
The game is reasonably involved. The player may win or lose on the first roll, or may win
or lose on any subsequent roll. Method main (lines 21-65) uses local variable myPoint (line
23) to store the “point” if the player doesn't win or lose on the first roll, local variable
gameStatus (line 24) to keep track of the overall game status and local variable sumOfDice
(line 26) to hold the sum of the dice for the most recent roll. Variable myPoint is initialized
to 0 to ensure that the application will compile. If you do not initialize myPoint , the com-
piler issues an error, because myPoint is not assigned a value in every case of the switch
statement, and thus the program could try to use myPoint before it's assigned a value. By
contrast, gameStatus is assigned a value in every case of the switch statement (including
the default case)—thus, it's guaranteed to be initialized before it's used, so we do not
need to initialize it in line 24.
enum Type Status
Local variable gameStatus (line 24) is declared to be of a new type called Status (declared
at line 11). Type Status is a private member of class Craps , because Status will be used
only in that class. Status is a type called an enum type , which, in its simplest form, declares
a set of constants represented by identifiers. An enum type is a special kind of class that's
introduced by the keyword enum and a type name (in this case, Status ). As with classes,
braces delimit an enum declaration's body. Inside the braces is a comma-separated list of
enum constants , each representing a unique value. The identifiers in an enum must be
unique . You'll learn more about enum types in Chapter 8.
Good Programming Practice 6.1
Use only uppercase letters in the names of enum constants to make them stand out and re-
mind you that they're not variables.
Variables of type Status can be assigned only the three constants declared in the enum
(line 11) or a compilation error will occur. When the game is won, the program sets local
variable gameStatus to Status.WON (lines 33 and 54). When the game is lost, the program
sets local variable gameStatus to Status.LOST (lines 38 and 57). Otherwise, the program
sets local variable gameStatus to Status.CONTINUE (line 41) to indicate that the game is
not over and the dice must be rolled again.
Good Programming Practice 6.2
Using enum constants (like Status.WON , Status.LOST and Status.CONTINUE ) rather
than literal values (such as 0, 1 and 2) makes programs easier to read and maintain.
 
Search WWH ::




Custom Search