From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch05;
02
03 importimport means to make the classes and/or packages available in this program fang2.core.Game;
04 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
05
06 /**
07 * This classclass is a group of fields and methods used for making objects, extending StringSprite, represents one six-sided die.
08 * That is, a cube used forfor is a looping structure for repeatedly executing a block of code generating random numbers. The faces are
09 * marked from 1 to 6.
10 */
11 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects OneDie
12 extendsextends means to customize or extend the functionality of a class StringSprite {open braces start code blocks and must be matched with a close brace
13 /** the value of the die; range is 1-6 */
14 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer face;
15
16 /**
17 * Initialize a OneDieStringSprite object with defaultdefault is what is executed when no cases are matched values
18 */
19 publicpublic is used to indicate unrestricted access (any other class can have access) OneDie() {open braces start code blocks and must be matched with a close brace
20 setFace(1);
21 }close braces end code blocks and must match an earlier open brace
22
23 /**
24 * Get the current value of face.
25 *
26 * @returnnull [brackets are typically used to declare, initialize and index (indicate which element of) arrays1..6]brackets are typically used to declare, initialize and index (indicate which element of) arrays, the current value of the die
27 */
28 publicpublic is used to indicate unrestricted access (any other class can have access) intint is the type for whole numbers and it is short for integer getFace() {open braces start code blocks and must be matched with a close brace
29 returnreturn means to provide the result of the method and/or cease execution of the method immediately face;
30 }close braces end code blocks and must match an earlier open brace
31
32 /**
33 * Roll thisthis means the current object (the implicit parameter) die: get currentGame(), use randomInt and setFace.
34 */
35 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value roll() {open braces start code blocks and must be matched with a close brace
36 setFace(Game.getCurrentGame().randomInt(1, 6));
37 }close braces end code blocks and must match an earlier open brace
38
39 /**
40 * Set value of face to newFace and update displayed value ifif executes the next statement only if the condition in parenthesis evaluates to true newFace
41 * is legal; otherwise leave face unchanged.
42 *
43 * @paramthis is the Javadoc tag for documenting the purpose of parameters newFace the newnew is used to create objects by calling the constructor value forfor is a looping structure for repeatedly executing a block of code face
44 */
45 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setFace(intint is the type for whole numbers and it is short for integer newFace) {open braces start code blocks and must be matched with a close brace
46 ifif executes the next statement only if the condition in parenthesis evaluates to true ((1 <=this evaluates to true if the left side is not more than the right side newFace) &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) (newFace <=this evaluates to true if the left side is not more than the right side 6)) {open braces start code blocks and must be matched with a close brace
47 face =this assignment operator makes the left side equal to the right side newFace;
48 setText(Integer.toString(face));
49 }close braces end code blocks and must match an earlier open brace
50 }close braces end code blocks and must match an earlier open brace
51 }close braces end code blocks and must match an earlier open brace
52
53 //Uploaded on Mon Mar 29 21:42:31 EDT 2010
|
Download/View scg/ch05/OneDie.java