From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch11;
002
003 importimport means to make the classes and/or packages available in this program java.util.Random;
004
005 /**
006 * Represents a Pig player. A player has a name and a score. When it is
007 * the player's turn, takeTurn is called. takeTurn uses console I/O
008 * defined in the Pig classclass is a group of fields and methods used for making objects (staticstatic means that an instance is not required for access (class level access) methods answersYes and getLine) forfor is a looping structure for repeatedly executing a block of code
009 * communication with the player.<br />
010 * Invariant: The score will never be negative. The name is immutable
011 * after construction.
012 */
013 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 Player {open braces start code blocks and must be matched with a close brace
014 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) Random random =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Random();
015
016 /** thisthis means the current object (the implicit parameter) player's name */
017 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) String name;
018
019 /** thisthis means the current object (the implicit parameter) player;s current score */
020 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer score;
021
022 /**
023 * Construct a newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@link Player}close braces end code blocks and must match an earlier open brace. Players start with a score of 0 and
024 * the given name
025 *
026 * @paramthis is the Javadoc tag for documenting the purpose of parameters name the player's name
027 */
028 publicpublic is used to indicate unrestricted access (any other class can have access) Player(String name) {open braces start code blocks and must be matched with a close brace
029 thisthis means the current object (the implicit parameter).name =this assignment operator makes the left side equal to the right side name;
030 setScore(0);
031 }close braces end code blocks and must match an earlier open brace
032
033 /**
034 * Get the player's name.
035 *
036 * @returnnull the player's name
037 */
038 publicpublic is used to indicate unrestricted access (any other class can have access) String getName() {open braces start code blocks and must be matched with a close brace
039 returnreturn means to provide the result of the method and/or cease execution of the method immediately name;
040 }close braces end code blocks and must match an earlier open brace
041
042 /**
043 * Get the player's score.
044 *
045 * @returnnull the player's current score.
046 */
047 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 getScore() {open braces start code blocks and must be matched with a close brace
048 returnreturn means to provide the result of the method and/or cease execution of the method immediately score;
049 }close braces end code blocks and must match an earlier open brace
050
051 /**
052 * Take thisthis means the current object (the implicit parameter) player's turn. The video game loop is here, a little
053 * tilted on its side. The state update happens at the top of the
054 * loop. Since the loop keeps going around and around, thisthis means the current object (the implicit parameter) is fine.
055 */
056 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value takeTurn() {open braces start code blocks and must be matched with a close brace
057 intint is the type for whole numbers and it is short for integer turnTotal =this assignment operator makes the left side equal to the right side 0;
058 booleanboolean is a type that is either true or false rolledPig =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
059 booleanboolean is a type that is either true or false heldPoints =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
060 whilewhile is a looping structure for executing code repeatedly (!this is the not operator, which changes true to false and false to truerolledPig &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 &&) !this is the not operator, which changes true to false and false to trueheldPoints) {open braces start code blocks and must be matched with a close brace
061 // ----- update state of turn -----
062 intint is the type for whole numbers and it is short for integer roll =this assignment operator makes the left side equal to the right side rollOneDie();
063 rolledPig =this assignment operator makes the left side equal to the right side (roll ==this is the comparison operator which evaluates to true if both sides are the same 1);
064 // ----- show state of turn -----
065 ifif executes the next statement only if the condition in parenthesis evaluates to true (rolledPig) {open braces start code blocks and must be matched with a close brace
066 System.out.println(name +adds two numbers together or concatenates Strings together ": Rolled PIG!");
067 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
068 System.out.println(name +adds two numbers together or concatenates Strings together ": Rolled " +adds two numbers together or concatenates Strings together roll);
069 }close braces end code blocks and must match an earlier open brace
070 // ----- get user input for turn -----
071 ifif executes the next statement only if the condition in parenthesis evaluates to true (!this is the not operator, which changes true to false and false to truerolledPig) {open braces start code blocks and must be matched with a close brace
072 turnTotal +=this increases the variable on the left by the value on the right roll;
073 heldPoints =this assignment operator makes the left side equal to the right side Pig.answersYes(name +adds two numbers together or concatenates Strings together " with " +adds two numbers together or concatenates Strings together turnTotal +adds two numbers together or concatenates Strings together
074 " points this turn, would you like to hold your points?");
075 }close braces end code blocks and must match an earlier open brace
076 }close braces end code blocks and must match an earlier open brace
077
078 // Could have gotten here for two different reasons; only update
079 // score if player held the points.
080 ifif executes the next statement only if the condition in parenthesis evaluates to true (heldPoints) {open braces start code blocks and must be matched with a close brace
081 incrementScore(turnTotal);
082 }close braces end code blocks and must match an earlier open brace
083
084 System.out.println(name +adds two numbers together or concatenates Strings together " ends turn with " +adds two numbers together or concatenates Strings together score +adds two numbers together or concatenates Strings together " points.");
085 }close braces end code blocks and must match an earlier open brace
086
087 /**
088 * Get a string representation forfor is a looping structure for repeatedly executing a block of code thisthis means the current object (the implicit parameter) player. The string
089 * representation is the name, a colon, and the current score.
090 *
091 * @returnnull string representation of thisthis means the current object (the implicit parameter) {open braces start code blocks and must be matched with a close brace@link Player}close braces end code blocks and must match an earlier open brace
092 */
093 @Override
094 publicpublic is used to indicate unrestricted access (any other class can have access) String toString() {open braces start code blocks and must be matched with a close brace
095 returnreturn means to provide the result of the method and/or cease execution of the method immediately name +adds two numbers together or concatenates Strings together ": " +adds two numbers together or concatenates Strings together score;
096 }close braces end code blocks and must match an earlier open brace
097
098 /**
099 * Increment the player's score by the given amount. score silently
100 * left unchanged ifif executes the next statement only if the condition in parenthesis evaluates to true it would be negative.
101 *
102 * @paramthis is the Javadoc tag for documenting the purpose of parameters delta amount to add to the player's score.
103 */
104 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value incrementScore(intint is the type for whole numbers and it is short for integer delta) {open braces start code blocks and must be matched with a close brace
105 setScore(score +adds two numbers together or concatenates Strings together delta);
106 }close braces end code blocks and must match an earlier open brace
107
108 /**
109 * Roll one 6-sided die. A random number on the rannge [brackets are typically used to declare, initialize and index (indicate which element of) arrays1-n]brackets are typically used to declare, initialize and index (indicate which element of) arrays can be
110 * generated with a random number on the range [brackets are typically used to declare, initialize and index (indicate which element of) arrays0-n) +adds two numbers together or concatenates Strings together 1 (there are n
111 * different values and the smallest value is 1).
112 *
113 * @returnnull random intint is the type for whole numbers and it is short for integer on [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
114 */
115 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer rollOneDie() {open braces start code blocks and must be matched with a close brace
116 returnreturn means to provide the result of the method and/or cease execution of the method immediately random.nextInt(6) +adds two numbers together or concatenates Strings together 1;
117 }close braces end code blocks and must match an earlier open brace
118
119 /**
120 * Update the player's score.
121 *
122 * @paramthis is the Javadoc tag for documenting the purpose of parameters score the newnew is used to create objects by calling the constructor score value; silently ignored (score
123 * unchanged) ifif executes the next statement only if the condition in parenthesis evaluates to true negative value passed in
124 */
125 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setScore(intint is the type for whole numbers and it is short for integer score) {open braces start code blocks and must be matched with a close brace
126 ifif executes the next statement only if the condition in parenthesis evaluates to true (score >=this evaluates to true if the left side is not less than the right side 0) {open braces start code blocks and must be matched with a close brace
127 thisthis means the current object (the implicit parameter).score =this assignment operator makes the left side equal to the right side score;
128 }close braces end code blocks and must match an earlier open brace
129 }close braces end code blocks and must match an earlier open brace
130 }close braces end code blocks and must match an earlier open brace
131
132 //Uploaded on Mon Mar 29 21:40:10 EDT 2010
|
Download/View scg/ch11/Player.java