|
001 packagepackage is used to name the directory or folder a class is in FA;
002 //start auto-imports
003 importimport means to make the classes and/or packages available in this program java.util.*;
004 //end auto-imports
005
006
007 /**
008 * Alternate Project: Problems from chapter 7.
009 * @authorthis is the Javadoc tag for documenting who created the source code Frank Anderson
010 * @authorthis is the Javadoc tag for documenting who created the source code Derrick Dixon */
011
012 /** The classclass is a group of fields and methods used for making objects Purse contains methods to:
013 * 1. add coins by name to an ArrayList.
014 2. Display the contents of a purse in the order coins are entered.
015 3. Display the contents of a purse in the reverse order of entry.
016 4. Transfers the contents of one purse to that of another.
017 5. Check to see ifif executes the next statement only if the condition in parenthesis evaluates to true the collection of coins in a purse are identical
018 in name and order.
019 6. Check to see ifif executes the next statement only if the condition in parenthesis evaluates to true the contents of the coins in a purse are identical,
020 regardless of the order of entry into the purse. */
021 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 Purse
022 {open braces start code blocks and must be matched with a close brace
023
024 privateprivate is used to restrict access to the current class only ArrayList<String> purseContents;
025 publicpublic is used to indicate unrestricted access (any other class can have access) Purse()
026 {open braces start code blocks and must be matched with a close brace
027 purseContents=this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<String>();
028 }close braces end code blocks and must match an earlier open brace
029
030
031 /** Problem 7.2 works*/
032 /** addCoin adds the name of a coin to the ArrayList, purseContents. */
033 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value addCoin(String coinName)
034 {open braces start code blocks and must be matched with a close brace
035 purseContents.add(coinName);
036 }close braces end code blocks and must match an earlier open brace
037
038 /** toString takes the elements of the ArrayList purseContents and displays
039 * them within bracket as a list separated by commas. The list is displayed
040 * in the same order as the coins were added to the ArrayList. */
041 /** @returnnull the list of elements in order of entry. */
042 publicpublic is used to indicate unrestricted access (any other class can have access) String toString()
043 {open braces start code blocks and must be matched with a close brace
044
045 String output=this assignment operator makes the left side equal to the right side"";
046 ifif executes the next statement only if the condition in parenthesis evaluates to true(purseContents.size()==this is the comparison operator which evaluates to true if both sides are the same0)
047 returnreturn means to provide the result of the method and/or cease execution of the method immediately "[]";
048 elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true(purseContents.size()==this is the comparison operator which evaluates to true if both sides are the same1)
049 returnreturn means to provide the result of the method and/or cease execution of the method immediately "["+adds two numbers together or concatenates Strings togetherpurseContents.get(0)+adds two numbers together or concatenates Strings together"]";
050 elseelse is what happens when the if condition is false
051 {open braces start code blocks and must be matched with a close brace
052 output=this assignment operator makes the left side equal to the right side"[";
053 forfor is a looping structure for repeatedly executing a block of code(intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0;i<purseContents.size()-1; i++this is the increment operator, which increases the variable by 1)
054 output =this assignment operator makes the left side equal to the right side output +adds two numbers together or concatenates Strings together purseContents.get(i)+adds two numbers together or concatenates Strings together", ";
055 output =this assignment operator makes the left side equal to the right side output +adds two numbers together or concatenates Strings together purseContents.get(purseContents.size()-1)+adds two numbers together or concatenates Strings together"]";
056 returnreturn means to provide the result of the method and/or cease execution of the method immediately output;
057 }close braces end code blocks and must match an earlier open brace
058 }close braces end code blocks and must match an earlier open brace
059
060 /** Problem 7.3 works */
061 /** The method reverse() returns a string of the contents of the ArrayList
062 * purseContents in the opposite order in which they were entered. The list
063 * is enclosed in brackets and each element is separated by a comma. */
064 /** @ returnreturn means to provide the result of the method and/or cease execution of the method immediately the list of elements presented in reverse order. */
065 publicpublic is used to indicate unrestricted access (any other class can have access) String reverse()
066 {open braces start code blocks and must be matched with a close brace
067 String output=this assignment operator makes the left side equal to the right side"";
068 output =this assignment operator makes the left side equal to the right side"[";
069 forfor is a looping structure for repeatedly executing a block of code(intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side purseContents.size()-1;i>=this evaluates to true if the left side is not less than the right side1; i--this is the decrement operator, which decreases the variable by 1)
070 output =this assignment operator makes the left side equal to the right side output +adds two numbers together or concatenates Strings together purseContents.get(i)+adds two numbers together or concatenates Strings together", ";
071 output =this assignment operator makes the left side equal to the right side output +adds two numbers together or concatenates Strings together purseContents.get(0)+adds two numbers together or concatenates Strings together"]";
072 returnreturn means to provide the result of the method and/or cease execution of the method immediately output;
073 }close braces end code blocks and must match an earlier open brace
074
075 /** Problem 7.4 */
076 /** Takes the contents of one purse and adds it to another;
077 empties the content of the purse whose contents were transferred. */
078 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value transfer(Purse other)
079 {open braces start code blocks and must be matched with a close brace
080 purseContents.addAll(other.purseContents);
081 other.purseContents.clear();
082 }close braces end code blocks and must match an earlier open brace
083
084
085 /** Problem 7.5 */
086 /** sameContents is a method that determins whether two purses have
087 * the same contents, meaning that they have the same coins in the
088 * same order. */
089 /** @returnnull truetrue is the boolean value that is the opposite of false or falsefalse is a value for the boolean type and means not true depending ifif executes the next statement only if the condition in parenthesis evaluates to true order is the same. */
090 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false sameContents(Purse other)
091
092 {open braces start code blocks and must be matched with a close brace
093 ifif executes the next statement only if the condition in parenthesis evaluates to true(purseContents.size()!=this is the not equals operator which evaluates to true if both sides are differentother.purseContents.size())
094 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
095 forfor is a looping structure for repeatedly executing a block of code(intint is the type for whole numbers and it is short for integer i=this assignment operator makes the left side equal to the right side0;i<other.purseContents.size();i++this is the increment operator, which increases the variable by 1)
096 {open braces start code blocks and must be matched with a close brace
097 ifif executes the next statement only if the condition in parenthesis evaluates to true(purseContents.get(i)!=this is the not equals operator which evaluates to true if both sides are different other.purseContents.get(i))
098 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
099 }close braces end code blocks and must match an earlier open brace
100 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
101 }close braces end code blocks and must match an earlier open brace
102 /** Problem 7.6 */
103 /** sameCoins is a method that determines whether two purses have
104 * the same coins, regardless of there order. */
105 /** @returnnull truetrue is the boolean value that is the opposite of false or falsefalse is a value for the boolean type and means not true depending ifif executes the next statement only if the condition in parenthesis evaluates to true coins in the purse are the same. */
106 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false sameCoins(Purse other)
107 {open braces start code blocks and must be matched with a close brace
108 intint is the type for whole numbers and it is short for integer count1Penny =this assignment operator makes the left side equal to the right side 0;
109 intint is the type for whole numbers and it is short for integer count2Penny =this assignment operator makes the left side equal to the right side 0;
110 intint is the type for whole numbers and it is short for integer count1Nickel =this assignment operator makes the left side equal to the right side 0;
111 intint is the type for whole numbers and it is short for integer count2Nickel =this assignment operator makes the left side equal to the right side 0;
112 intint is the type for whole numbers and it is short for integer count1Dime =this assignment operator makes the left side equal to the right side 0;
113 intint is the type for whole numbers and it is short for integer count2Dime =this assignment operator makes the left side equal to the right side 0;
114 intint is the type for whole numbers and it is short for integer count1Quarter =this assignment operator makes the left side equal to the right side 0;
115 intint is the type for whole numbers and it is short for integer count2Quarter =this assignment operator makes the left side equal to the right side 0;
116
117 ifif executes the next statement only if the condition in parenthesis evaluates to true(purseContents.size()!=this is the not equals operator which evaluates to true if both sides are differentother.purseContents.size())
118 {open braces start code blocks and must be matched with a close brace
119 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
120 }close braces end code blocks and must match an earlier open brace
121 forfor is a looping structure for repeatedly executing a block of code(intint is the type for whole numbers and it is short for integer i=this assignment operator makes the left side equal to the right side0;i<other.purseContents.size();i++this is the increment operator, which increases the variable by 1)
122 {open braces start code blocks and must be matched with a close brace
123 ifif executes the next statement only if the condition in parenthesis evaluates to true(purseContents.get(i).equals("penny"))
124 {open braces start code blocks and must be matched with a close brace
125 count1Penny++this is the increment operator, which increases the variable by 1;
126 }close braces end code blocks and must match an earlier open brace
127 ifif executes the next statement only if the condition in parenthesis evaluates to true(other.purseContents.get(i).equals("penny"))
128 {open braces start code blocks and must be matched with a close brace
129 count2Penny++this is the increment operator, which increases the variable by 1;
130 }close braces end code blocks and must match an earlier open brace
131 ifif executes the next statement only if the condition in parenthesis evaluates to true(purseContents.get(i).equals("nickel"))
132 {open braces start code blocks and must be matched with a close brace
133 count1Nickel++this is the increment operator, which increases the variable by 1;
134 }close braces end code blocks and must match an earlier open brace
135 ifif executes the next statement only if the condition in parenthesis evaluates to true(other.purseContents.get(i).equals("nickel"))
136 {open braces start code blocks and must be matched with a close brace
137 count2Nickel++this is the increment operator, which increases the variable by 1;
138 }close braces end code blocks and must match an earlier open brace
139 ifif executes the next statement only if the condition in parenthesis evaluates to true(purseContents.get(i).equals("dime"))
140 {open braces start code blocks and must be matched with a close brace
141 count1Dime++this is the increment operator, which increases the variable by 1;
142 }close braces end code blocks and must match an earlier open brace
143 ifif executes the next statement only if the condition in parenthesis evaluates to true(other.purseContents.get(i).equals("dime"))
144 {open braces start code blocks and must be matched with a close brace
145 count2Dime++this is the increment operator, which increases the variable by 1;
146 }close braces end code blocks and must match an earlier open brace
147 ifif executes the next statement only if the condition in parenthesis evaluates to true(purseContents.get(i).equals("quarter"))
148 {open braces start code blocks and must be matched with a close brace
149 count1Quarter++this is the increment operator, which increases the variable by 1;
150 }close braces end code blocks and must match an earlier open brace
151 ifif executes the next statement only if the condition in parenthesis evaluates to true(other.purseContents.get(i).equals("quarter"))
152 {open braces start code blocks and must be matched with a close brace
153 count2Quarter++this is the increment operator, which increases the variable by 1;
154 }close braces end code blocks and must match an earlier open brace
155 }close braces end code blocks and must match an earlier open brace
156 ifif executes the next statement only if the condition in parenthesis evaluates to true((count1Penny==this is the comparison operator which evaluates to true if both sides are the samecount2Penny) &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 &&) (count1Nickel==this is the comparison operator which evaluates to true if both sides are the samecount2Nickel) &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 &&) (count1Dime==this is the comparison operator which evaluates to true if both sides are the samecount2Dime) &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 &&) (count1Quarter==this is the comparison operator which evaluates to true if both sides are the samecount2Quarter) )
157 {open braces start code blocks and must be matched with a close brace
158 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
159 }close braces end code blocks and must match an earlier open brace
160 elseelse is what happens when the if condition is false
161 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
162 }close braces end code blocks and must match an earlier open brace
163
164 }close braces end code blocks and must match an earlier open brace
|