






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







// An applet that prints out the words of a file in the order of their appearance followed by // A word count. Punctuation marks are considered to be words. public class Words extends TextApplet {
public void run () { String filename = WordEnumeration.chooseFilename(); // State variables for iteration WordEnumeration words = WordEnumeration.fileToWords(filename); int count = 0; println("Processing file " + filename + "\n"); println("Here are the words in the file in order of appearance:"); println("-----------------------------------------"); while (words.hasMoreElements()) { println(words.nextElement()); count = count + 1; // Could also say count++ } println("-----------------------------------------"); println("The file has " + count + " words"); }
// An applet that prints the number of distinct words in the file, // followed by a list of distinct words in dictionary order. public class WordsDistinctSorted extends TextApplet {
public void run () { String filename = WordEnumeration.chooseFilename(); println("Processing file " + filename + "\n"); // State variables for iteration WordEnumeration words = WordEnumeration.fileToWords(filename); Vector set = new Vector(); // set contains sorted sequence of strings seen so far.
... Main loop goes here. See next slide... // Print results println("There are " + set.size() + " distinct words in the file:"); println("-----------------------------------------"); Enumeration distinct = set.elements(); while (distinct.hasMoreElements()) { println(distinct.nextElement()); } println("-----------------------------------------"); }
// This code belongs in the context of the previous slide. // Insert all words into the set while (words.hasMoreElements()) { String word = (String) words.nextElement(); int index = StringVectorOps.binarySearchSorted(word, set); // Only insert word if its not already in set. if ((index == set.size()) || (! (word.equals(set.elementAt(index))))) { set.insertElementAt(word, index); }
// Assume that string in vec are sorted from low to high // according to the string compareTo() method. // If x is in vec, returns the least index at which x appears. // (There may be more than one.) // If x is not in vec, returns the index at which x should be // inserted in vec in sorted order. // Use linear left-to-right search to find the index. public static int linearSearchSorted(String x, Vector vec) { for (int i = 0; i < vec.size(); i++) { if (x.compareTo((String) vec.elementAt(i)) <= 0) { return i; } } // Only reach this point if x is greater than all other elements // in which case insertion point is at end of vec. return vec.size();
// Assume that objects in vec are sorted from low to high // according to the string compareTo() method. // If x is in vec, returns an index at which vec appears. // (There may be more than one.) // If x is not in vec, returns the index at which x should be // inserted in vec in sorted order. // Use binary search to find the index. public static int binarySearch(String x, Vector vec) { int lo = 0; int hi = vec.size() - 1; // Loop invariants: // * All elements at indices < lo are less than x. // * All elements at indices > hi are greater than x.
... Main loop goes here (see next slide)... // lo must be hi + 1 at this point. // By invariants, insertion point must be lo. return lo;