// For use with Bailey & Bailey, Java Elements. // (c) 1999 duane a. bailey // file http://www.mhhe.com/javaelements labs/Undict.java //+entire // Lab #: import element.*; import structure.*; import java.io.FileInputStream; public class Undict { protected String reserved = "!"; protected Hashtable h; protected boolean isVowel(char c) { // Students should add code here } protected String reduce(String s) { // Students should add code here } public Undict() { this("dict"); } public Undict(String filename) { long startTime = System.currentTimeMillis(); long time; FileInputStream file = null; h = new Hashtable(1553); List l; try { file = new FileInputStream(filename); } catch (Exception e){ System.out.println(e); System.exit(0); } structure.ReadStream r = new structure.ReadStream(file); for (r.skipWhite(); !r.eof(); r.skipWhite()) { String w,k; w = r.readString(); k = reduce(w); l = (List)h.get(k); if (l == null) { l = new SinglyLinkedList(); l.add(w); h.put(k,l); } else { l.add(w); } } time = System.currentTimeMillis()-startTime; System.out.println("[dictionary loaded in "+(time/1000.0)+" seconds]"); } public String like(String word) { return likeKey(reduce(word)); } public String likeKey(String key) { String result = ""; List l; l = (List)h.get(key); if (l == null) { return result; } Iterator li = l.elements(); while (li.hasMoreElements()) { result = li.value()+" "+result; li.nextElement(); } return result.trim(); } } //-entire