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

///Complete the following blanks in the JAVA program public class HashTable<K,V>

ID: 651798 • Letter: #

Question

///Complete the following blanks in the JAVA program

public class HashTable<K,V>
{
   private HashItem[] table;
   private int count;
   private double loadFactor;

   private static final int DEFAULT_CAPACITY = 5;
   private static final double DEFAULT_LOAD_FACTOR = 0.75;

   private class HashItem <K, V>
   {
       private int hash;
       private K key;
       private V value;
       private HashItem <K, V> next;

       public HashItem(int hashIn, K keyIn, V valueIn, HashItem<K, V> nextIn)
       {
       hash = hashIn;
       key = keyIn;
       value = valueIn;
       next = nextIn;
       }
   }
     
   public HashTable(int initialCapacity, double loadFactorIn)
   {
       if (initialCapacity <= 0)
       {
       throw new IllegalArgumentException("Capacity must be > 0.");
       }

       if (loadFactorIn < 0)
       {
           throw new IllegalArgumentException() ;
       }
         
       loadFactor = loadFactorIn;
       table = new HashItem[initialCapacity];
   }
     
   public HashTable()
   {
       this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
   }
     
   public int size()
   {
       return count;
   }
     
   private void rehash()
   {
       HashItem[] oldTable = table;

       // create new table
       int capacity = oldTable.length * 2 + 1;
       table = new HashItem[capacity];

       // get elements at each old table index
       for (int i = 0; i < oldTable.length; i++)
       {
       HashItem <K, V> item = oldTable[i];
         
       // add the element from old tabl and its linked elements
       while (item != null)
          {
           put(item.key, item.value);
           item = item.next;
          }
       }
   }
         
   public V put(K keyIn, V valueIn)
       {
           if (valueIn == __________)
           {
               throw new
               IllegalArgumentException("Value cannot be null.");
           }
             
           int hash = Math.abs(keyIn.hashCode());
           int index = hash % table.length;
             
           // get hash item at target location (if any)
           HashItem<K, V> current = table[index];
             
           // iterate through linked nodes at that location (if any)
           while (current != null)
           {
           // if an item with the same hash & key is there, replace
           if (hash == current.hash && current.key.equals(hash))
           {
               V oldItem = current.value;
               current.value = valueIn;
               return oldItem;
           }
             
           current = current. __________;
           }

             
           int threshold = (int) (table.length * loadFactor);
           if (size() >= threshold)
           {
               rehash();
               index = hash % table.length;
           }
             
           current = table[index];
           table[index] = new HashItem <K, V>(hash, keyIn, valueIn, current);
           count++;

           return valueIn;
       }
         
       public V get(Object keyIn)
       {
           int hash = Math.abs(keyIn.hashCode());
           int index = hash % table.length;
           HashItem <K, V> item = table[index];
           while (item != null)
           {
               if (hash == item.hash && item.key.equals(keyIn))
               {
           return item.value;
               }
           item = item.next;
           }
           return null;
       }
         
       public boolean remove(Object keyIn)
       {
           int hash = Math.abs(keyIn.hashCode());
           int index = hash % table.length;

           HashItem<K, V> item = table[index];
           HashItem<K, V> previous = null;
           while (item != null)
           {
               if (item.key.equals(keyIn))
           {
                   // if it is not the root node, replace links
                   if (previous != null)
                   {
                   previous.next = item.next;
                   }
           // if it was the root, move next item in the chain down
           else
           {
           table[index] = item.next;
           }
           count--;
           return true;
           }
           previous = item;
           item = item.next;
           }

           return false;
       }
         
             
       public void makeEmpty()
       {
           table = new HashItem[table.length];
           count =0;
       }
}
     

Explanation / Answer

public V put(K keyIn, V valueIn)
       {
           if (valueIn == null)
           {
               throw new
               IllegalArgumentException("Value cannot be null.");
           }
             
           int hash = Math.abs(keyIn.hashCode());
           int index = hash % table.length;
             
           // get hash item at target location (if any)
           HashItem<K, V> current = table[index];
             
           // iterate through linked nodes at that location (if any)
           while (current != null)
           {
           // if an item with the same hash & key is there, replace
           if (hash == current.hash && current.key.equals(hash))
           {
               V oldItem = current.value;
               current.value = valueIn;
               return oldItem;
           }

current = current.next

}