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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

270 Methods Chapter 6<br />

and implements ActionListener. A class can inherit existing attributes and behaviors<br />

(data and methods) from another class specified <strong>to</strong> the right of keyword extends in the<br />

class definition. In addition, a class can implement one or more interfaces. An interface<br />

specifies one or more behaviors (i.e., methods), which you must define in your class definition.<br />

Implementing interface ActionListener forces us <strong>to</strong> define a method with the<br />

first line<br />

public void actionPerformed( ActionEvent actionEvent )<br />

in our Craps class. This method’s task is <strong>to</strong> process a user’s interaction with the JBut<strong>to</strong>n<br />

(called Roll Dice on the user interface). When the user presses the but<strong>to</strong>n, this method will<br />

be called au<strong>to</strong>matically in response <strong>to</strong> the user interaction. This process is called event handling.<br />

The event is the user interaction (i.e., pressing the but<strong>to</strong>n). The event handler is method<br />

actionPerformed. We discuss the details of this interaction and method<br />

actionPerformed shortly. Chapter 9, Object-Oriented <strong>Program</strong>ming, discusses interfaces<br />

in detail. For now, as you develop your own applets that have graphical user interfaces,<br />

mimic the features that support event handling of the GUI components we present.<br />

The game of craps is reasonably involved. The player may win or lose on the first roll,<br />

or may win or lose on any roll. Line 14 creates variables that define the three states of a<br />

game of craps: Game won (WON), game lost (LOST) or continue rolling the dice (CON-<br />

TINUE). Keyword final at the beginning of the declaration indicates that these are constant<br />

variables. When a program declares a final variable, the program must initialize<br />

the variable before using the variable and cannot modify the variable thereafter. If the variable<br />

is an instance variable, this initialization normally occurs in the variable’s declaration.<br />

The initialization also can occur in a special method of a class called a construc<strong>to</strong>r (discussed<br />

in Chapter 8). Constant variables are often called named constants or read-only<br />

variables. We provide more details on keyword final in Chapter 7 and Chapter 8.<br />

Common <strong>Program</strong>ming Error 6.12<br />

After declaring and initializing a final variable, attempting <strong>to</strong> assign another value <strong>to</strong> that<br />

variable is a syntax error. 6.12<br />

Good <strong>Program</strong>ming Practice 6.5<br />

Use only uppercase letters (with underscores between words) in the names of final variables.<br />

This format makes these constants stand out in a program. 6.5<br />

Good <strong>Program</strong>ming Practice 6.6<br />

Using meaningfully named final variables rather than integer constants (such as 2) makes<br />

programs more readable. 6.6<br />

Lines 17–20 declare several instance variables that are used throughout the Craps<br />

applet. Variable firstRoll is a boolean variable that indicates whether the next roll<br />

of the dice is the first roll in the current game. Variable sumOfDice maintains the sum of<br />

the dice for the last roll. Variable myPoint s<strong>to</strong>res the “point” if the player does not win or<br />

lose on the first roll. Variable gameStatus keeps track of the current state of the game<br />

(WON, LOST or CONTINUE).<br />

Lines 23–25 declare references <strong>to</strong> the GUI components used in this applet’s graphical<br />

user interface. References die1Label, die2Label, sumLabel and pointLabel all<br />

refer <strong>to</strong> JLabel objects. A JLabel contains a string of characters <strong>to</strong> be displayed on the<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!