import structure.*; public class PhoneBook2 { public static void main(String args[]) { PhoneEntry a = new PhoneEntry("name","title",3,"bulding",5); ComparableInt b = new ComparableInt(3); a.lessThan(b); /* PhoneEntry data[] = new PhoneEntry[30]; ReadStream r = new ReadStream(); int count = 0; while (!r.eof()) { String name, title, building; int room, extension; name = r.readString()+" "+r.readString(); title = r.readString(); extension = r.readInt(); room = r.readInt(); building = r.readString(); r.readLine(); data[count++] = new PhoneEntry(name,title,extension,building,room); } insertionSort(data,count); for (int i = 0; i < count; i++) { System.out.println(data[i]); } */ } //+swap protected static void swap(PhoneEntry data[], int i, int j) // pre: 0 <= i,j < data.length // post: data[i] and data[j] are exchanged { PhoneEntry temp; temp = data[i]; data[i] = data[j]; data[j] = temp; } //-swap //+insertionSort public static void insertionSort(PhoneEntry data[], int n) // pre: n <= data.length // post: values in data[0..n-1] are in ascending order { int numSorted = 0; // number of values in place int index; // general index while (numSorted < n) { // take the first unsorted value PhoneEntry temp = data[numSorted]; // ...and insert it among the sorted: for (index = numSorted; index > 0; index--) { if (temp.lessThan(data[index-1])) { data[index] = data[index-1]; } else { break; } } // re-insert value data[index] = temp; numSorted++; } } //-insertionSort } //+record class PhoneEntry implements Comparable { String name; // person's name String title; // person's title int extension; // telephone number String building; // office building int room; // number of room public PhoneEntry(String n, String t, int e, String b, int r) // post: construct a new phone entry { //-record /* //+record ... //-record */ name = n; title = t; building = b; room = r; extension = e; //+record } public boolean lessThan(Comparable other) // pre: other is non-null // post: return true if this record is less than other { Assert.pre(other instanceof PhoneBook, "other implements phonebook"); return this.extension < ((PhoneEntry)other).extension; } //-record /* //+lessThan2 public boolean lessThan(Comparable other) // pre: other is non-null // post: return true iff this record is less than other { Assert.pre(other instanceof PhoneBook, "other implements phonebook"); if (this.extension < other.extension) return true; else if (this.extension == other.extension && this.name.compareTo(other.name) < 0) return true; else return false; } //-lessThan2 */ public String toString() { return name+" "+title+" "+building+" "+room+" "+extension; } //+record } //-record class PhoneVector extends Vector { //+vectorSort protected void swap(int i, int j) // pre: 0 <= i,j < this.size // post: elements i and j are exchanged within the vector { Object temp; temp = elementAt(i); setElementAt(elementAt(j),i); setElementAt(temp,j); } //-swap //+vectorSort public void insertionSort() // post: values of vector are in ascending order { int numSorted = 0; // number of values in place int index; // general index while (numSorted < size()) { // take the first unsorted value PhoneEntry temp = (PhoneEntry)elementAt(numSorted); // ...and insert it among the sorted: for (index = numSorted; index > 0; index--) { if (temp.lessThan((PhoneEntry)elementAt(index-1))) { setElementAt(elementAt(index-1),index); } else { break; } } // re-insert value setElementAt(temp,index); numSorted++; } } //-vectorSort }