// Hashing1.java class PhoneBook { private static int TABLE_SIZE = 117; private String[] table; // hash a string into a number private int hash(String name) { int value = 0; // add up all the ASCII values for (int i = 0; i < name.length(); i++) { value = value + name.charAt(i); } // mod by size to prevent overflow return value % TABLE_SIZE; } public PhoneBook() { table = new String[TABLE_SIZE]; } // insert a name public void insert(String name, String number) { int index = hash(name); table[index] = number; } // lookup a name public String lookup(String name) { // get the index they should be at int index = hash(name); // return the data return table[index]; } } public class Hashing1 { public static void main(String args[]) { PhoneBook table = new PhoneBook(); table.insert("Bob", "345-8214"); table.insert("Joe", "555-7374"); table.insert("John", "633-1214"); table.insert("Alice", "234-7234"); table.insert("Gary", "943-7236"); table.insert("Claire", "333-8231"); table.insert("Wendy", "361-3617"); table.insert("Jim", "824-6217"); table.insert("Elena", "347-3829"); table.insert("Elane", "654-1714"); System.out.println("John's number is " + table.lookup("John")); System.out.println("Bob's number is " + table.lookup("Bob")); System.out.println("Elena's number is " + table.lookup("Elena")); System.out.println("Elane's number is " + table.lookup("Elane")); } }