From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch10;
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 /**
053 * Increment the player's score by the given amount. score silently
054 * left unchanged ifif executes the next statement only if the condition in parenthesis evaluates to true it would be negative.
055 *
056 * @paramthis is the Javadoc tag for documenting the purpose of parameters delta amount to add to the player's score.
057 */
058 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
059 setScore(score +adds two numbers together or concatenates Strings together delta);
060 }close braces end code blocks and must match an earlier open brace
061
062 /**
063 * Update the player's score.
064 *
065 * @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
066 * unchanged) ifif executes the next statement only if the condition in parenthesis evaluates to true negative value passed in
067 */
068 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
069 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
070 thisthis means the current object (the implicit parameter).score =this assignment operator makes the left side equal to the right side score;
071 }close braces end code blocks and must match an earlier open brace
072 }close braces end code blocks and must match an earlier open brace
073
074 /**
075 * Take thisthis means the current object (the implicit parameter) player's turn. The video game loop is here, a little
076 * tilted on its side. The state update happens at the top of the
077 * loop. Since the loop keeps going around and around, thisthis means the current object (the implicit parameter) is fine.
078 */
079 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
080 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;
081 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;
082 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;
083 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
084 // ----- update state of turn -----
085 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();
086 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);
087 // ----- show state of turn -----
088 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
089 System.out.println(name +adds two numbers together or concatenates Strings together ": Rolled PIG!");
090 }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
091 System.out.println(name +adds two numbers together or concatenates Strings together ": Rolled " +adds two numbers together or concatenates Strings together roll);
092 }close braces end code blocks and must match an earlier open brace
093 // ----- get user input for turn -----
094 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
095 turnTotal +=this increases the variable on the left by the value on the right roll;
096 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
097 " points this turn, would you like to hold your points?");
098 }close braces end code blocks and must match an earlier open brace
099 }close braces end code blocks and must match an earlier open brace
100
101 // Could have gotten here for two different reasons; only update
102 // score if player held the points.
103 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
104 incrementScore(turnTotal);
105 }close braces end code blocks and must match an earlier open brace
106
107 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.");
108 }close braces end code blocks and must match an earlier open brace
109
110 /**
111 * 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
112 * representation is the name, a colon, and the current score.
113 *
114 * @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
115 */
116 @Override
117 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
118 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;
119 }close braces end code blocks and must match an earlier open brace
120
121 /**
122 * 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
123 * 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
124 * different values and the smallest value is 1).
125 *
126 * @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
127 */
128 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
129 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;
130 }close braces end code blocks and must match an earlier open brace
131 }close braces end code blocks and must match an earlier open brace
132
133 //Uploaded on Mon Mar 29 21:40:49 EDT 2010
|
Download/View scg/ch10/Player.java