|
001 packagepackage is used to name the directory or folder a class is in scg.ch14.dictionary;
002
003 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
004 importimport means to make the classes and/or packages available in this program java.util.Collections;
005 importimport means to make the classes and/or packages available in this program java.util.Scanner;
006
007 importimport means to make the classes and/or packages available in this program scg.ch14.util.AttributeValuePair;
008 importimport means to make the classes and/or packages available in this program scg.ch14.util.ReadAndWrite;
009
010 /**
011 * A {open braces start code blocks and must be matched with a close brace@link Dictionary}close braces end code blocks and must match an earlier open brace file is a collection of lines of the form<br />
012 * alias=this assignment operator makes the left side equal to the right sidecommand
013 *
014 * <p>The file is read (ignoring leading and trailing spaces)
015 */
016 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 Dictionary {open braces start code blocks and must be matched with a close brace
017 /** the entries forfor is a looping structure for repeatedly executing a block of code the Dictionary */
018 ArrayList<AttributeValuePair> allEntries;
019
020 /**
021 * Construct a newnew is used to create objects by calling the constructor, empty, Dictionary
022 */
023 publicpublic is used to indicate unrestricted access (any other class can have access) Dictionary() {open braces start code blocks and must be matched with a close brace
024 allEntries =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<AttributeValuePair>();
025 }close braces end code blocks and must match an earlier open brace
026
027 /**
028 * Read the dictionary from the file. Each entry is on a single line
029 * with an =this assignment operator makes the left side equal to the right side between the alias and the word.
030 *
031 * @paramnull scanner open file reader to process
032 */
033 publicpublic is used to indicate unrestricted access (any other class can have access) Dictionary(Scanner scanner) {open braces start code blocks and must be matched with a close brace
034 thisthis means the current object (the implicit parameter)();
035 whilewhile is a looping structure for executing code repeatedly (scanner.hasNextLine()) {open braces start code blocks and must be matched with a close brace
036 String attribute =this assignment operator makes the left side equal to the right side ReadAndWrite.readAttribute(scanner);
037 /* throwthrow means to make an exception occur away */ ReadAndWrite.readMatch(scanner, "=");
038 String value =this assignment operator makes the left side equal to the right side ReadAndWrite.readValue(scanner);
039 ifif executes the next statement only if the condition in parenthesis evaluates to true (attribute.length() !=this is the not equals operator which evaluates to true if both sides are different 0) {open braces start code blocks and must be matched with a close brace
040 allEntries.add(newnew is used to create objects by calling the constructor AttributeValuePair(attribute, value));
041 }close braces end code blocks and must match an earlier open brace
042 }close braces end code blocks and must match an earlier open brace
043 Collections.sort(allEntries);
044 }close braces end code blocks and must match an earlier open brace
045
046 /**
047 * Get the value in the dictionary associated with the given key; will
048 * returnreturn means to provide the result of the method and/or cease execution of the method immediately nullnull is the value used to refer to a non-existant object ifif executes the next statement only if the condition in parenthesis evaluates to true there is not matching entry.
049 *
050 * @paramnull key the key of the entry forfor is a looping structure for repeatedly executing a block of code which to find a value
051 *
052 * @returnnull value associated with key ifif executes the next statement only if the condition in parenthesis evaluates to true key is in dictionary; nullnull is the value used to refer to a non-existant object
053 * otherwise
054 */
055 publicpublic is used to indicate unrestricted access (any other class can have access) String get(String key) {open braces start code blocks and must be matched with a close brace
056 String value =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
057 intint is the type for whole numbers and it is short for integer ndx =this assignment operator makes the left side equal to the right side indexOfKey(key);
058 ifif executes the next statement only if the condition in parenthesis evaluates to true (ndx >=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
059 value =this assignment operator makes the left side equal to the right side allEntries.get(ndx).getValue();
060 }close braces end code blocks and must match an earlier open brace
061 returnreturn means to provide the result of the method and/or cease execution of the method immediately value;
062 }close braces end code blocks and must match an earlier open brace
063
064 /**
065 * Does the {open braces start code blocks and must be matched with a close brace@link Dictionary}close braces end code blocks and must match an earlier open brace contain the given key?
066 *
067 * @paramnull key key to lookup
068 *
069 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true there is an entry with the given key; falsefalse is a value for the boolean type and means not true
070 * otherwise
071 */
072 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false hasKey(String key) {open braces start code blocks and must be matched with a close brace
073 returnreturn means to provide the result of the method and/or cease execution of the method immediately (indexOfKey(key) >=this evaluates to true if the left side is not less than the right side 0);
074 }close braces end code blocks and must match an earlier open brace
075
076 /**
077 * Add the key, value pair to the {open braces start code blocks and must be matched with a close brace@link Dictionary}close braces end code blocks and must match an earlier open brace ifif executes the next statement only if the condition in parenthesis evaluates to true the key is not
078 * already found there. Replace the value of the entry forfor is a looping structure for repeatedly executing a block of code key with
079 * the newnew is used to create objects by calling the constructor value ifif executes the next statement only if the condition in parenthesis evaluates to true it is already in the dictionary.
080 *
081 * @paramnull key the key forfor is a looping structure for repeatedly executing a block of code being able to lookup the entry
082 * @paramnull value the value to set forfor is a looping structure for repeatedly executing a block of code the key
083 *
084 * @returnnull the previous value associated with the key or nullnull is the value used to refer to a non-existant object ifif executes the next statement only if the condition in parenthesis evaluates to true
085 * there was no previous value
086 */
087 publicpublic is used to indicate unrestricted access (any other class can have access) String put(String key, String value) {open braces start code blocks and must be matched with a close brace
088 String previousValue =this assignment operator makes the left side equal to the right side get(key);
089 intint is the type for whole numbers and it is short for integer ndx =this assignment operator makes the left side equal to the right side indexOfKey(key);
090 ifif executes the next statement only if the condition in parenthesis evaluates to true (ndx >=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
091 allEntries.get(ndx).setValue(value);
092 }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
093 allEntries.add(newnew is used to create objects by calling the constructor AttributeValuePair(key, value));
094 Collections.sort(allEntries);
095 }close braces end code blocks and must match an earlier open brace
096 returnreturn means to provide the result of the method and/or cease execution of the method immediately previousValue;
097 }close braces end code blocks and must match an earlier open brace
098
099 /**
100 * Generate a string representation of the {open braces start code blocks and must be matched with a close brace@link Dictionary}close braces end code blocks and must match an earlier open brace.
101 */
102 @Override
103 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
104 String retval =this assignment operator makes the left side equal to the right side "";
105 String separator =this assignment operator makes the left side equal to the right side "";
106 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 !=this is the not equals operator which evaluates to true if both sides are different allEntries.size(); ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
107 AttributeValuePair curr =this assignment operator makes the left side equal to the right side allEntries.get(i);
108 retval +=this increases the variable on the left by the value on the right separator +adds two numbers together or concatenates Strings together curr.getAttribute() +adds two numbers together or concatenates Strings together " = " +adds two numbers together or concatenates Strings together
109 curr.getValue();
110 separator =this assignment operator makes the left side equal to the right side "\n";
111 }close braces end code blocks and must match an earlier open brace
112 returnreturn means to provide the result of the method and/or cease execution of the method immediately retval;
113 }close braces end code blocks and must match an earlier open brace
114
115 /**
116 * Binary search forfor is a looping structure for repeatedly executing a block of code index of entry with the given key. The algorithm
117 * is to track the range [brackets are typically used to declare, initialize and index (indicate which element of) arrayslowerNdx, upperNdx), the range where the
118 * matching index must be (ifif executes the next statement only if the condition in parenthesis evaluates to true there is one). When there are no Entry
119 * in the range, then we know there is no match.
120 *
121 * <p>Each time through the loop, get the middle index between the
122 * extremes of the range. Compare middle entry to matchKey; ifif executes the next statement only if the condition in parenthesis evaluates to true middle
123 * smaller, bump lower up to mid +adds two numbers together or concatenates Strings together 1 (ignore the one that didn't
124 * match); ifif executes the next statement only if the condition in parenthesis evaluates to true middle bigger, bump upper down to mid (which ignores the
125 * one that didn't match) and ifif executes the next statement only if the condition in parenthesis evaluates to true mid matches, set returnreturn means to provide the result of the method and/or cease execution of the method immediately value and get
126 * out.
127 *
128 * <p>The invariant remains in force because the list is sorted. Thus
129 * ifif executes the next statement only if the condition in parenthesis evaluates to true mid is small, then key must be above it in the list; ifif executes the next statement only if the condition in parenthesis evaluates to true mid is
130 * big, then key must be below it in the list. The loop ends because
131 * each time through rangeSize(lowerNdx, upperNdx) is at least one
132 * smaller than it was; it must go down to 0 or smaller in a finite
133 * number of loops.
134 *
135 * @paramnull matchKey the key to match.
136 *
137 * @returnnull -1 ifif executes the next statement only if the condition in parenthesis evaluates to true not found, otherwise the index of the matching
138 * {open braces start code blocks and must be matched with a close brace@link AttributeValuePair}close braces end code blocks and must match an earlier open brace
139 */
140 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer indexOfKey(String matchKey) {open braces start code blocks and must be matched with a close brace
141 intint is the type for whole numbers and it is short for integer matchingNdx =this assignment operator makes the left side equal to the right side -1;
142 intint is the type for whole numbers and it is short for integer lowerNdx =this assignment operator makes the left side equal to the right side 0;
143 intint is the type for whole numbers and it is short for integer upperNdx =this assignment operator makes the left side equal to the right side allEntries.size();
144 // invariant: if matchKey is in allEntries then
145 // matchNdx is on the range [lowerNdx, upperNdx).
146 whilewhile is a looping structure for executing code repeatedly (rangeSize(lowerNdx, upperNdx) > 0) {open braces start code blocks and must be matched with a close brace// any remaining entries?
147 intint is the type for whole numbers and it is short for integer midNdx =this assignment operator makes the left side equal to the right side (lowerNdx +adds two numbers together or concatenates Strings together upperNdx) / 2;
148 intint is the type for whole numbers and it is short for integer compareMidToMatch =this assignment operator makes the left side equal to the right side allEntries.get(midNdx).getAttribute()
149 .compareTo(matchKey);
150 ifif executes the next statement only if the condition in parenthesis evaluates to true (compareMidToMatch > 0) {open braces start code blocks and must be matched with a close brace
151 // allEntries[midNdx] < matchKey; search upper half of remaining
152 upperNdx =this assignment operator makes the left side equal to the right side midNdx;
153 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (compareMidToMatch ==this is the comparison operator which evaluates to true if both sides are the same 0) {open braces start code blocks and must be matched with a close brace
154 // allEntries[midNdx] == matchKey; return midNdx
155 matchingNdx =this assignment operator makes the left side equal to the right side midNdx;
156 breakbreak terminates the loop immediately;
157 }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// if (compareMidToMatch > 0
158 // matchKey < allEntries[midNdx]; search lower half of remaining
159 lowerNdx =this assignment operator makes the left side equal to the right side midNdx +adds two numbers together or concatenates Strings together 1;
160 }close braces end code blocks and must match an earlier open brace
161 }close braces end code blocks and must match an earlier open brace
162 returnreturn means to provide the result of the method and/or cease execution of the method immediately matchingNdx;
163 }close braces end code blocks and must match an earlier open brace
164
165 /**
166 * Return the number of entries in the index range
167 * [brackets are typically used to declare, initialize and index (indicate which element of) arrayslowerNdx-upperNdx). Notice that the index range is not symetric!this is the not operator, which changes true to false and false to true
168 *
169 * @paramnull lowerNdx the lower bound of the index range
170 * @paramnull upperNdx the upper (excluded) bound of the index rang
171 *
172 * @returnnull size of the range [brackets are typically used to declare, initialize and index (indicate which element of) arrayslowerNdx-upperNdx) (interval notation).
173 */
174 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer rangeSize(intint is the type for whole numbers and it is short for integer lowerNdx, intint is the type for whole numbers and it is short for integer upperNdx) {open braces start code blocks and must be matched with a close brace
175 returnreturn means to provide the result of the method and/or cease execution of the method immediately (upperNdx - lowerNdx);
176 }close braces end code blocks and must match an earlier open brace
177 }close braces end code blocks and must match an earlier open brace
178
179 //Uploaded on Mon Mar 29 21:42:00 EDT 2010
|