/* Polymorphic open hash tables - 2nd version. Compared to the first version, here we declare our own interface and use it to specify something hashable. The interfact contains the abstract getkey, next and setnext operationis. We place these requirements inside a separate interface as opposed to directly inside the abstract superclass. We also took the "next" out of the account class, so the "accounts" can be used in different circumstances. Instead, we used inheritance again to implement a subclass of account called "accnt", which implements the Hashable interface as well as provide the "next pointer". */ interface Hashable { Keytype getkey(); Datatype next(); void setnext(Datatype n); } abstract class hashtable, Keytype> { public abstract int hash(Keytype id); Datatype[] Table; // insert data in front of linked list public void insert(Datatype A) { int i = hash(A.getkey()) % Table.length; A.setnext(Table[i]); Table[i] = A; } public Datatype lookup(Keytype id) { int i = hash(id) % Table.length; Datatype A = Table[i]; while (A!=null && !A.getkey().equals(id)) A = A.next(); return A; // A is either null or record with same key } public void delete(Keytype id) { int i = hash(id) % Table.length; Datatype A = Table[i]; if (A==null) return; if (A.getkey().equals(id)) { Table[i] = A.next(); return; } while (A.next()!=null && !A.next().getkey().equals(id)) A = A.next(); if (A.next()!=null) A.setnext(A.next().next()); } } // abstract class hashtable // class account does not directly implement "Hashable" class account { String owner; // owner of bank account int ssn; // social security number double balance; // current balance; String address; // other info ... static double interest_rate = 0.01; void withdraw(double amt) { balance -= amt; } void deposit(double amt) { balance += amt; } public account(String o, int s) // constructor { owner=o; ssn=s; balance = 100; } public String toString() {return owner+", ssn "+ssn+", balance "+balance;} } // class account // class that extends account and make it into something "Hashable": class accnt extends account implements Hashable { accnt next; // pointer to next account public accnt(String o, int s) { super(o,s); next=null; } public String getkey() { return this.owner; } public accnt next() { return next; } public void setnext(accnt n) { next = n; } } // class accnt; // accnt hash table class: class accht extends hashtable { public accht(int max) { Table = new accnt[max]; } public int hash(String id) { int n = id.length(); int sum = 0; for(int i=0;i