Java Reference
In-Depth Information
Logic of the main Method
Line 26 in main calls rollDice , which picks two random values from 1 to 6, displays the
values of the first die, the second die and their sum, and returns the sum. Method main
next enters the switch statement (lines 29-45), which uses the sumOfDice value from line
26 to determine whether the game has been won or lost, or should continue with another
roll. The values that result in a win or loss on the first roll are declared as private static
final int constants in lines 14-18. The identifier names use casino parlance for these
sums. These constants, like enum constants, are declared by convention with all capital let-
ters, to make them stand out in the program. Lines 31-34 determine whether the player
won on the first roll with SEVEN ( 7 ) or YO_LEVEN ( 11 ). Lines 35-39 determine whether the
player lost on the first roll with SNAKE_EYES ( 2 ), TREY ( 3 ), or BOX_CARS ( 12 ). After the first
roll, if the game is not over, the default case (lines 40-44) sets gameStatus to Sta-
tus.CONTINUE , saves sumOfDice in myPoint and displays the point.
If we're still trying to “make our point” (i.e., the game is continuing from a prior roll),
lines 48-58 execute. Line 50 rolls the dice again. If sumOfDice matches myPoint (line 53),
line 54 sets gameStatus to Status.WON , then the loop terminates because the game is com-
plete. If sumOfDice is SEVEN (line 56), line 57 sets gameStatus to Status.LOST , and the
loop terminates because the game is complete. When the game completes, lines 61-64 dis-
play a message indicating whether the player won or lost, and the program terminates.
The program uses the various program-control mechanisms we've discussed. The
Craps class uses two methods— main and rollDice ( called twice from main )— and the
switch , while , if else and nested if control statements. Note also the use of multiple
case labels in the switch statement to execute the same statements for sums of SEVEN and
YO_LEVEN (lines 31-32) and for sums of SNAKE_EYES , TREY and BOX_CARS (lines 35-37).
Why Some Constants Are Not Defined as enum Constants
You might be wondering why we declared the sums of the dice as private static final
int constants rather than as enum constants. The reason is that the program must compare
the int variable sumOfDice (line 26) to these constants to determine the outcome of each
roll. Suppose we declared enum Sum containing constants (e.g., Sum.SNAKE_EYES ) represent-
ing the five sums used in the game, then used these constants in the switch statement (lines
29-45). Doing so would prevent us from using sumOfDice as the switch statement's con-
trolling expression, because Java does not allow an int to be compared to an enum constant.
To achieve the same functionality as the current program, we would have to use a variable
currentSum of type Sum as the switch 's controlling expression. Unfortunately, Java does
not provide an easy way to convert an int value to a particular enum constant. This could
be done with a separate switch statement. This would be cumbersome and would not im-
prove the program's readability (thus defeating the purpose of using an enum ).
6.11 Scope of Declarations
You've seen declarations of various Java entities, such as classes, methods, variables and pa-
rameters. Declarations introduce names that can be used to refer to such Java entities. The
scope of a declaration is the portion of the program that can refer to the declared entity by
its name. Such an entity is said to be “in scope” for that portion of the program. This sec-
tion introduces several important scope issues.
 
 
Search WWH ::




Custom Search