public class StringTable { // overview: StringTable is a set of entries, // where the String values are unique keys. A typical StringTable // is {, , ... }. // //@ghost public int numEntries ; // The number of entries in the table public StringTable () // effects: Initializes this as an empty table: { }. //@ensures numEntries == 0; { } public StringTable (java.io.InputStream instream) // requires: The stream instream is a names file containing lines of the form // : // where the name is a string of non-space characters and the rate is // a floating point number. // modifies: instream // effects: Initializes this as a names table using the data from instream. { } public void addName (/*@non_null@*/ String key, double value) throws DuplicateEntryException // requires: The parameter name is not null. (This is what the // ESC/Java /*@non_null@*/ annotation means.) // modifies: this // effects: If key matches the value of String in this, throws DuplicateEntryException. // Otherwise, inserts into this. // e.g., if this_pre = {, , } // and s0, s1 and s2 are all different from key // then this_post = {, , , }. // //@modifies numEntries //@ensures numEntries == \old(numEntries) + 1; { } public double getValue (String key) // EFFECTS: Returns the value associated with key in this. If there is no entry matching // key, returns 0. // Note: it would be better to throw an exception (but we haven't covered that yet). { } public /*@non_null@*/ String getNthLowest (int index) // requires: The parameter index is non-negative and less than // the the number of entries in this. //@requires index >= 0; //@requires index < numEntries; // EFFECTS: Returns the key such that there are exactly index entries in the table for // with the value of the entry is lower than the value of the returned key. If two // keys have the same value, they will be ordered in an arbitrary way such that // getNthLowest (n) returns the first key and getNthLowest (n + 1) returns the second key. // // e.g., getNthLowest (0) returns the key associated with the lowest value in the table. // getNthLowest (size () - 1) returns the key associated with the highest value in the table. { } public int size () // EFFECTS: Returns the number of entries in this. //@ensures \result == numEntries; { } public String toString () // EFFECTS: Returns a string representation of this. { } public /*@non_null@*/ StringIterator names () // Note: this should be called keys, but is called names in // the implementation we provided. // EFFECTS: Returns a StringIterator that will iterate through all the keys in this in // order from lowest to highest. { } }