From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch10;
02
03 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
04
05 /**
06 * Implements a score keeper. Score is kept (and displayed) as a ratio
07 * between wins and games played. Initially the score is set to 0/0
08 * (zero games won of zero games played). The win method records a
09 * winning game; the lose method records a loss.
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 ScoreSprite
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 /** number of games played */
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 games;
15
16 /** number of games won */
17 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer wins;
18
19 /**
20 * Default constructor: 0/0 is initial score.
21 */
22 publicpublic is used to indicate unrestricted access (any other class can have access) ScoreSprite() {open braces start code blocks and must be matched with a close brace
23 thisthis means the current object (the implicit parameter)(0, 0);
24 }close braces end code blocks and must match an earlier open brace
25
26 /**
27 * Create newnew is used to create objects by calling the constructor ScoreSprite with the given score. Games must be greater
28 * than or equal to wins (or elseelse is what happens when the if condition is false wins will be reset to games).
29 *
30 * @paramthis is the Javadoc tag for documenting the purpose of parameters wins games won
31 * @paramthis is the Javadoc tag for documenting the purpose of parameters games games played
32 */
33 publicpublic is used to indicate unrestricted access (any other class can have access) ScoreSprite(intint is the type for whole numbers and it is short for integer wins, intint is the type for whole numbers and it is short for integer games) {open braces start code blocks and must be matched with a close brace
34 thisthis means the current object (the implicit parameter).wins =this assignment operator makes the left side equal to the right side wins;
35 thisthis means the current object (the implicit parameter).games =this assignment operator makes the left side equal to the right side games;
36 validateScore();
37 fixTextDisplay();
38 }close braces end code blocks and must match an earlier open brace
39
40 /**
41 * Record a losing game.
42 */
43 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value lose() {open braces start code blocks and must be matched with a close brace
44 ++this is the increment operator, which increases the variable by 1games;
45 fixTextDisplay();
46 }close braces end code blocks and must match an earlier open brace
47
48 /**
49 * Record a winning game.
50 */
51 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value win() {open braces start code blocks and must be matched with a close brace
52 ++this is the increment operator, which increases the variable by 1games;
53 ++this is the increment operator, which increases the variable by 1wins;
54 fixTextDisplay();
55 }close braces end code blocks and must match an earlier open brace
56
57 /**
58 * Implementation method: display the currently recorded score.
59 */
60 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value fixTextDisplay() {open braces start code blocks and must be matched with a close brace
61 setText(wins +adds two numbers together or concatenates Strings together "/" +adds two numbers together or concatenates Strings together games);
62 }close braces end code blocks and must match an earlier open brace
63
64 /**
65 * Make sure that the score makes sense; ifif executes the next statement only if the condition in parenthesis evaluates to true there are more wins than
66 * games, reset wins to match games.
67 */
68 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value validateScore() {open braces start code blocks and must be matched with a close brace
69 ifif executes the next statement only if the condition in parenthesis evaluates to true (games < wins) {open braces start code blocks and must be matched with a close brace
70 wins =this assignment operator makes the left side equal to the right side games;
71 }close braces end code blocks and must match an earlier open brace
72 }close braces end code blocks and must match an earlier open brace
73 }close braces end code blocks and must match an earlier open brace
74
75 //Uploaded on Mon Mar 29 21:42:15 EDT 2010
|
Download/View scg/ch10/ScoreSprite.java