Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi i neeeeeed heeeeeelp to complete this project i really am stuck on it can som

ID: 3696368 • Letter: H

Question

Hi i neeeeeed heeeeeelp to complete this project i really am stuck on it can some one please help me thank you

1) In the code shown below, complete the add method. It should compute the hashing index using the hashCode method for the element to be inserted. If the table at that index is empty, a new LinkedList should be created first and the element then added to the list.

A tester class,HashTableTester, is also provided for you to test your code.

2) The private method find is used to find and return an element in the table, or to remove an element from the table based upon a remove flag. If the table at that index is empty, a null is returned. Otherwise, the LinkedList at that position is searched for an element where its hashCode matches the key. If an element is removed and the list is empty, the table at that index is set to null. Test your code.

3) In the HashTableTester class, a variable, factor, is used to bias the number added to the table. Change it to 1 and see how the structure of the table changes. Now try a value of 29, the default size of the hash table and note how it changes

here what i have so far

import java.util.Iterator;
import java.util.LinkedList;

public class HashTable<T>
{
private static final int DEFAULT_SIZE = 29;
private LinkedList<T>[] hashTable;
  
public HashTable()
{
this (DEFAULT_SIZE);

}

public HashTable(int size)
{
hashTable = new LinkedList[size];
}
/*
* Add an element to the table. Return false if not successful
* @param element
* @return true if element was added to the table
*/
public boolean add(T element)
{
int i = element.hashCode();
  
if (hashTable[i].contains(null))
{
hashTable[i] = new LinkedList<T>();
hashTable[i].add(element);
return true;
}
else
return false;
}
/*
* Remove an element from the table
* @param key
* @return element removed or null if not found
*/

public T remove(int key)
{
return find(key, true);
}
/*
* Return the element in the table with the desired key
* @param key
* @return element or null if not found
*/
public T find(int key)
{
return find(key, false);
}

private T find(int key, boolean remove)
{
int index = hashIndex(key);
  
if (hashTable[index].isEmpty())
return null;
else
{
for (int i = 0; i < hashTable[index].size(); i++)
{
T e = hashTable[index].get(i);

  
  
if (remove)
return e;
{
if (e.hashCode() == key)
hashTable[index].remove(e);
  
if (hashTable[index].isEmpty())
hashTable[index] = null;
}
}

}
}

private int hashIndex(int key)
{
return key % hashTable.length;
}

public void dumpTable()
{
for (int i=0; i<hashTable.length; i++)
{
if (hashTable[i] == null)
{
System.out.println("index: " + i + " Empty");
}
else
{
LinkedList<T> list = hashTable[i];
System.out.print("Index: " + i + " (" + list.size() + ") ");
System.out.println(list);
}
}
}
}

and the tester

public class HashTableTester
{
public static void main(String[] args)
{

HashTable<Integer> table = new HashTable<>();

int factor = 17;

for (int i = 0; i < 15; i++)
{
table.add(i * factor);
}
  
table.dumpTable();
// Test find
System.out.println(table.find(factor * 4));
// Test remove
System.out.println("Remove key " + (factor*4) + " "
+ table.remove(factor*4));

table.dumpTable();
}
}

i cant seam to get the second part of it but i got the first part finish for the add method and i just need help on the find and remove method can someone please help me i really appreciate it

Explanation / Answer

please find the below remove method to find and remove an element from hashtable :

Find method to find whether an element exists in hashtable

Hope the above code helps

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote