|
Dictionary |
|
1 // joi/4/dictionary/Dictionary.java
2 //
3 //
4 // Copyright 2003 Bill Campbell and Ethan Bolker
5
6 import java.util.*;
7
8 /**
9 * Model a dictionary with a TreeMap of (word, Definition) pairs.
10 *
11 * @see Definition
12 *
13 * @version 4
14 */
15
16 public class Dictionary
17 {
18 private TreeMap entries;
19
20 /**
21 * Construct an empty Dictionary.
22 */
23
24 public Dictionary()
25 {
26 entries = new TreeMap();
27 }
28
29 /**
30 * Add an entry to this Dictionary.
31 *
32 * @param word the word being defined.
33 * @param definition the Definition of that word.
34 */
35
36 public void addEntry( String word, Definition definition )
37 {
38 entries.put( word, definition );
39 }
40
41 /**
42 * Look up an entry in this Dictionary.
43 *
44 * @param word the word whose definition is sought
45 * @return the Definition of that word, null if none.
46 */
47
48 public Definition getEntry( String word )
49 {
50 return (Definition)entries.get(word);
51 }
52
53 /**
54 * Get the size of this Dictionary.
55 *
56 * @return the number of words.
57 */
58
59 public int getSize()
60 {
61 return entries.size();
62 }
63
64 /**
65 * Construct a String representation of this Dictionary.
66 *
67 * @return a multiline String representation.
68 */
69
70 public String toString()
71 {
72 String str = "";
73 String word;
74 Definition definition;
75 Set allWords = entries.keySet();
76 Iterator wordIterator = allWords.iterator();
77 while ( wordIterator.hasNext() ) {
78 word = (String)wordIterator.next();
79 definition = this.getEntry( word );
80 str += word + ":\n" + definition.toString() + "\n";
81 }
82 return str;
83 }
84 }
85
|
Dictionary |
|