// open hash table of students, first version, least flexible, non-polymorphic import java.util.*; // for ArrayList interface STHTable { public int hash(int id); public void insert(student s); public student delete(int id); public student search(int id); } public class stuhash implements STHTable { // HT is an array of linked lists! protected ArrayList[] HT; // the hash table public stuhash(int max) // constructor sets table size { // The following array creation causes a compiler warning: ignore HT = (ArrayList[]) new ArrayList[max]; for(int i=0;i(); // creates empty arraylists }// constructor // The hash function given a student: use last digits of 700 number: public int hash(student S) { return S.id % HT.length; } // The following version of the hash takes the id directly: public int hash(int id) { return id % HT.length; } // inserting a new student object into the table: public void insert(student S) { int h = hash(S); HT[h].add(S); // add student to array list at hashed position } // searching for a student by 700 number: returns null or student object public student search(int id) { ArrayList L = HT[ hash(id) ]; student answer = null; for(int i=0;i L = HT[ hash(id) ]; student answer = null; for(int i=0;i R : HT) for(student s : R) System.out.println(s); } /* uncomment main for testing: */ public static void main(String[] args) { stuhash SH = new stuhash(100); SH.insert(new student("mary",700234123,3.5,1)); SH.insert(new student("larz",700555123,2.0,3)); SH.insert(new student("narx",700444223,2.8,0)); for(int i=0;i<10;i++) SH.insert(new student()); // random student student l = SH.search(700555123); SH.delete(700234123); student m = SH.search(700234123); System.out.println(l); System.out.println(m); SH.printall(); } }//stuhash // what if I want to vary the hash function? one solution is to make hash // abstract, and delay its definition until a subclass is defined! // But let's make more than just the hash function abstract.