I’m supposed to build and implement a phone book that uses an array-based hash table as the underlying structure and chaining to handle collisions with the following classes : Phonebook Demo, Phonebook, Hash, Linked List, and Node. My teacher is also supposed to be able to change the default size for the table when he goes in to look at our code with minimal issues. We are also supposed to have the table double if the load factor is greater than 0.7. We are also not able to use the built-in Java hashcode functions and instead come up with our own hash function that minimizes collisions
Anyways…. I have been having a ton of trouble with inserting and deleting people to the phone book… I keep receiving Null Pointer errors or sometimes array index out of bounds (I’m not sure how to minimize this as the teacher will be changing the array size so I feel like there would always be a possibility of this happening). I also have no idea what to use as my hash function as it has to be hashed based on the person’s name. I honestly really don’t know what I’m doing in this class as well as this project. Would really appreciate any help
public class PhoneBookDemo { public static void main(String[] args) { PhoneBook p = new PhoneBook(); p.insert("Zachary", 12345); p.retrieve("Zachary"); p.delete("Zachary"); } }
public class PhoneBook { Hash h; public PhoneBook(){ int DEFAULT_SIZE = 10; h = new Hash(DEFAULT_SIZE); } public void insert(String name, int number){ h.insert(name, number); System.out.println("You have added " + name + ": " + number); } public void retrieve(String name){ int number = h.find(name); System.out.println(name + "'s number is: " + number); } public void delete(String name){ int number = h.find(name); h.delete(name); System.out.println("You have deleted" + name + ": " + number); } public void print(){ h.print(); } }
public class Hash { float topThreshold = 0.7f; float bottomThreshold = 0.5f; private int size = 0; LinkedList[] table; public Hash(int defSize){ size = defSize; table = new LinkedList[defSize]; for(int i = 0; i < defSize; i++){ table[i] = null; } } public int hashkey(String name){ int hash = 0; for(int i = 0; i < name.length(); i++){ hash += name.charAt(i); } return hash % 100; } public void insert(String name, int number){ System.out.println(hashkey(name)); table[hashkey(name)] = new LinkedList(name, number); size++;; if(loadFactor() != 0){ resize(loadFactor()); } } public void delete(String name){ table[hashkey(name)].delete(name); size--; if(loadFactor() != 0){ resize(loadFactor()); } } public int loadFactor(){ double topFactor = size * topThreshold; double bottomFactor = size * bottomThreshold; if(size > topFactor) return (table.length * 2); if(size < bottomFactor) return (table.length / 2); else return 0; } public void resize(int newSize){ LinkedList[] oldTable = table; table = new LinkedList[newSize]; size = 0; for(int i = 0; i < oldTable.length; i++){ if(oldTable[i] != null){ table[i] = oldTable[i]; } } } public int find(String name){ return table[hashkey(name)].find(name); } public void print(){ for(int i = 0; i < size; i++){ if(table[i] != null){ table[i].print(); } } } public int size(){ int elements = 0; for(int i = 0; i < size; i++) { if(table[i] != null){ elements = elements + table[i].size(); } } return (elements*size); } }
public class LinkedList { public Node head; public Node tail; int sum = 0; public LinkedList(String name, int number){ add(name, number); } public boolean isEmpty(){ if(size() == 0){ return true; } else return false; } public int size(){ Node temp = this.head; int size = 0; while(temp != null){ size++; temp = temp.next; } return size; } public void add(String name, int number){ Node cursor = new Node(name, number); Node last = head; cursor.setNext(null); if(head == null){ cursor.setPrev(null); head = cursor; return; } while(last.next != null){ last = last.next; } last.next = cursor; cursor.prev = last; } public int find(String name){ Node cursor = head; while(cursor.key != name){ cursor = cursor.next; } return cursor.getValue(); } public String delete(String name){ Node cursor = head; while(cursor.key != name){ cursor = cursor.next; } (cursor.prev).setNext(cursor.next); (cursor.next).setPrev(cursor.prev); cursor.setNext(null); cursor.setPrev(null); return name; } public void print(){ if(isEmpty()){ return; } Node cursor = head; int i = 1; while(cursor != null){ System.out.println("Contact #" + i + ":\n" + cursor.getKey() + ": " + cursor.getValue() + "\n"); i++; } } public boolean sameName(String name, boolean sameName){ Node cursor = head; while(cursor != null){ if(cursor.key == name){ sameName = true; } } return sameName; } }
public class Node { String key; int value; Node next; Node prev; public Node(String key, int value){ this.key = key; this.value = value; } public void setValue(int value){ this.value = value; //stores the phone number } public void setKey(String key){ this.key = key; //stores the name of the contact } public void setNext(Node next){ this.next = next; } public void setPrev(Node prev){ this.prev = prev; } public String getKey(){ return key; //gets the name of the contact } public int getValue(){ return value; //gets the phone number } public Node getPrev(){ return prev; } public Node getNext(){ return next; } }
The post In Java, NullPointer Errors and Bad Hash Functions for My Phone Book Assignment appeared first on 100% Private Proxies - Fast, Anonymous, Quality, Unlimited USA Private Proxy!.