26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 Methods 273<br />

parameter list. Method rollDice returns the sum of the two dice, so a return type of int<br />

is indicated in the method’s header.<br />

The user clicks Roll Dice <strong>to</strong> roll the dice. This action invokes method action-<br />

Performed (line 70) of the applet. Method actionPerformed checks the boolean<br />

variable firstRoll (line 73) <strong>to</strong> determine if it is true or false. If it is true, this roll<br />

is the first roll of the game. Line 74 calls rollDice, which picks two random values from<br />

1 <strong>to</strong> 6, displays the value of the first die, second die and the sum of the dice in the first three<br />

JTextFields, respectively, and returns the sum of the dice. Note that the integer values<br />

are converted <strong>to</strong> Strings (lines 131–133) with static method Integer.<strong>to</strong>String,<br />

because JTextFields can display only Strings. After the first roll, the nested<br />

switch structure at line 76 in actionPerformed determines if the game has been won<br />

or lost, or if the game should continue with another roll. After the first roll, if the game is<br />

not over, sumOfDice is saved in myPoint and displayed in pointField.<br />

Line 115 calls method displayMessage (defined at lines 141–161) <strong>to</strong> display the<br />

current status of the game. The if/else structure at line 144 uses applet method show-<br />

Status <strong>to</strong> display a String in the applet container’s status bar. Line 145 displays<br />

Roll again.<br />

if gameStatus is equal <strong>to</strong> CONTINUE. Lines 150–151 display<br />

Player wins. Click Roll Dice <strong>to</strong> play again.<br />

if gameStatus is equal <strong>to</strong> WON. Lines 153–154 display<br />

Player loses. Click Roll Dice <strong>to</strong> play again.<br />

if gameStatus is equal <strong>to</strong> LOST. Method showStatus receives a String argument<br />

and displays it in the status bar of the applet container. If the game is over (i.e., it has been<br />

won or lost) line 158 sets firstRoll <strong>to</strong> true <strong>to</strong> indicate that the next roll of the dice is<br />

the first roll of the next game.<br />

The program then waits for the user <strong>to</strong> click the Roll Dice but<strong>to</strong>n again. Each time the<br />

user presses Roll Dice but<strong>to</strong>n, method actionPerformed invokes method rollDice<br />

<strong>to</strong> produce a new sumOfDice. If the current roll is a continuation of an incomplete game,<br />

the code in lines 103–112 executes. In line 107, if sumOfDice matches myPoint, line<br />

108 sets gameStatus <strong>to</strong> WON, and the game is complete. In line 110, if sumOfDice is<br />

equal <strong>to</strong> 7, line 111 sets gameStatus <strong>to</strong> LOST, and the game is complete. When the game<br />

completes, displayMessage displays an appropriate message, and the user can click<br />

the Roll Dice but<strong>to</strong>n <strong>to</strong> begin a new game. Throughout the program, the four JText-<br />

Fields are updated with the new values of the dice and the sum on each roll, and the<br />

pointField is updated each time a new game begins.<br />

Note the interesting use of the various program control mechanisms we have discussed.<br />

The craps program uses four methods—init, actionPerformed, rollDice<br />

and displayMessage—and the switch, if/else and nested if structures. Note also<br />

the use of multiple case labels in the switch structure <strong>to</strong> execute the same statements<br />

(lines 79 and 85). Also, note that the event-handling mechanism acts as a form of program<br />

control. In this program, event-handling enables user-controlled repetition—each time the<br />

user clicks Roll Dice, the program rolls the dice again. In the exercises, we investigate various<br />

interesting characteristics of the game of craps.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!