Question: I'm running into trouble implementing a chained hashtable using interface and generics. I need to be implementing the following interface: public interface HashTable { public

I'm running into trouble implementing a chained hashtable using interface and generics. I need to be implementing the following interface:

public interface HashTable {

public void add(K key, V value);

public V remove(K key);

public V lookup(K key);

public Object[] getValuesList(); public V[] getSortedList(V[] list); public void printReport();

}

Here is the code I have:

import java.util.ArrayList; import java.lang.Math;

public class Hashtbl/* implements HashTable*/ { //This section that is commented out needs to be fixed ArrayList>bucket=new ArrayList<>(); int numBuckets=10; int size; public Hashtbl() { for(int i=0;ihead=bucket.get(index); HashNodetoAdd=new HashNode<>(); toAdd.key=key; toAdd.value=value; if(head==null) { bucket.set(index, toAdd); size++; } else { while(head!=null) { if(head.key.equals(key)) { head.value=value; size++; break; } head=head.next; } if(head==null) { head=bucket.get(index); toAdd.next=head; bucket.set(index, toAdd); size++; } } if((1.0*size)/numBuckets>0.7) { //do something ArrayList>tmp=bucket; bucket=new ArrayList<>(); numBuckets=2*numBuckets; for(int i=0;i headNode:tmp) { while(headNode!=null) { add(headNode.key, headNode.value); headNode=headNode.next; } } } }

public V remove(K key) { String a = key.toString(); char[] b = a.toCharArray(); int index= additiveHashing(b,numBuckets); HashNodehead=bucket.get(index); if(head==null) { return null; } if(head.key.equals(key)) { V val=head.value; head=head.next; bucket.set(index, head); size--; return val; } else { HashNodeprev=null; while(head!=null) { if(head.key.equals(key)) { prev.next=head.next; size--; return head.value; } prev=head; head=head.next; } size--; return null; } }

public V lookup(K key) { String a = key.toString(); char[] b = a.toCharArray(); int index= additiveHashing(b,numBuckets); HashNode head=bucket.get(index); while(head!=null) { if(head.key.equals(key)) { return head.value; } head=head.next; } return null; } /* @Override public V[] getSortedList(V[] list) { // TODO Auto-generated method stub return null; }

@Override public void printReport() { // TODO Auto-generated method stub } public Object[] getValuesList(){ // TODO } */ public static void main(String[] args) { Hashtblmap=new Hashtbl<>(); map.add("this",1 ); map.add("blah", 2); map.add("please", 3); System.out.println(map.lookup("this")); } }

public class HashNode { K key; V value; HashNodenext; public HashNode() { this.key=key; this.value=value; } }

Can you please fix this code so that it properly implements all the methods in the first interface? I beleive they should all work, but I need help finishing getSortedList(V[] list), getValuesList(), and printReport().

The printReport() method should print to the console the following statistics:

The Load Fator, that is, the ratio of used to total number of buckets.

The longest chain in the table, that is, the maximum number of collisions for a particular bucket.

The Density Factor, that is, the ratio of elements stored elements to total number of buckets.

The Chanining Factor, that is, the average length of any chain in the table.

The V[] getSortedList(V[] list) method should return a sorted list with all the elements in the array.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!