Question

Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements...

Implement the missing methods in java! Thanks!

import java.util.Iterator;
import java.util.NoSuchElementException;

public class HashTableOpenAddressing<K, V> implements DictionaryInterface<K, V> {
        private int numEntries;
        private static final int DEFAULT_CAPACITY = 5;
        private static final int MAX_CAPACITY = 10000;
        private TableEntry<K, V>[] table;
        private double loadFactor;
        private static final double DEFAULT_LOAD_FACTOR = 0.75;

        public HashTableOpenAddressing() {
                this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
        }

        public HashTableOpenAddressing(int initialCapacity, double loadFactorIn) {
                numEntries = 0;
                if (loadFactorIn <= 0 || initialCapacity <= 0) {
                        throw new IllegalArgumentException("Initial capacity and load " +
                                        "factor must be greater than 0");
                }
                else if (initialCapacity > MAX_CAPACITY)
                        throw new IllegalStateException("Attempt to create a dictionary " +
                                        "whose capacity is larger than " + MAX_CAPACITY);

                loadFactor = loadFactorIn;
                // Set up hash table:
                // Initial size of hash table is same as initialCapacity if it is prime;
                // otherwise increase it until it is prime size
                int tableSize = getNextPrime(initialCapacity);

                @SuppressWarnings("unchecked")
                TableEntry<K, V>[] temp = (TableEntry<K, V>[]) new TableEntry[tableSize];
                table = temp;
        }

        /** Task: Adds a new entry to the dictionary. If the given search
         *        key already exists in the dictionary, replaces the 
         *        corresponding value.
         *  @param key    an object search key of the new entry
         *  @param value  an object associated with the search key
         *  @return either null if the new entry was added to the dictionary or the
         *          value that was associated with key if that value was replaced */
        @Override
        public V add(K keyIn, V valueIn) {
                // TODO
                return null;
        }

                private int linearProbe(int index, K keyIn) {
                boolean found = false;
                int removedStateIndex = -1; // Index of first removed location
                if(table[index] == null){ 
                        return index;
                }
                while (!found && table[index] != null) {        
                        if (table[index].isIn()) {
                                if (keyIn.equals(table[index].getKey())) {
                                        found = true;           // Key found
                                }
                                else{                           // Follow probe sequence
                                        index = (index + 1) % table.length;  
                                }
                        }
                        else {                                          // Skip entries that were removed
                                // Save index of first location in removed state
                                if (removedStateIndex == -1) {
                                        removedStateIndex = index;
                                }
                                index = (index + 1) % table.length;          
                        } 
                } 
                if (found || (removedStateIndex == -1) ) {
                        return index;                  // Index of either key or null
                }
                else {
                        return removedStateIndex;      // Index of an available location
                }
        }

         private int quadraticProbe(int index, K key) {
                // TODO
                 return -1;
         }

        private int getHashIndex(K key) {
                int hashIndex = -1;
                // TODO
                return hashIndex;
        }

        /** Task: Removes a specific entry from the dictionary.
         *  @param key  an object search key of the entry to be removed
         *  @return either the value that was associated with the search key 
         *          or null if no such object exists */
        @Override
        public V remove(K key) {
                V removedValue = null;
                int index = getHashIndex(key);
                index = linearProbe(index, key);
//              index = quadraticProbe(index, key);

                if (index != -1){
                        // Key found; flag entry as removed and return its value
                        removedValue = table[index].getValue();
                        table[index].setToRemoved();
                        numEntries--;
                } 
                // Else not found; result is null
                return removedValue;
        } 

        /** Task: Retrieves the value associated with a given search key.
         *  @param key  an object search key of the entry to be retrieved
         *  @return either the value that is associated with the search key 
         *          or null if no such object exists */
        @Override
        public V getValue(K key) {
                // TODO 
                return null;
        }

        /** Task: Sees whether a specific entry is in the dictionary.
         *  @param key  an object search key of the desired entry
         *  @return true if key is associated with an entry in the
         *          dictionary */
        @Override
        public boolean contains(K keyIn) {
                return getValue(keyIn) != null;
        }

        /** Task: Creates an iterator that traverses all search keys in the 
         *        dictionary.
         *  @return an iterator that provides sequential access to the search 
         *          keys in the dictionary */
        @Override
        public Iterator<K> getKeyIterator() {
                return new KeyIterator();
        }

        /** Task: Creates an iterator that traverses all values in the 
         *        dictionary.
         *  @return an iterator that provides sequential access to the values 
         *          in the dictionary */
        @Override
        public Iterator<V> getValueIterator() {
                return new ValueIterator();
        }

        /** Task: Gets the size of the dictionary.
         *  @return the number of entries (key-value pairs) currently
         *          in the dictionary */
        @Override
        public int getSize() {
                        // TODO
                return -1;
        }

        /** Task: Sees whether the dictionary is empty.
         *  @return true if the dictionary is empty */
        @Override
        public boolean isEmpty() {
                        // TODO
                return false;
        }

        /** Task: Sees whether the dictionary is full.
         *  @return true if the dictionary is full */
        @Override
        public boolean isFull() {
                        // TODO
                return false;
        }

        /** Task: Removes all entries from the dictionary. */
        @Override
        public void clear() {
                        // TODO

        }
0 0
Add a comment Improve this question Transcribed image text
Answer #1
abc

package com.nt.abc;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.*;

public class HashTableOpenAddressing<K, V> implements DictionaryInterface<K, V> {
private int numEntries;
private static final int DEFAULT_CAPACITY = 5;
private static final int MAX_CAPACITY = 10000;
private TableEntry<V>[] table;
private double loadFactor;
private static final double DEFAULT_LOAD_FACTOR = 0.75;
private int tableSize;
private int count=0;
public HashTableOpenAddressing() {
this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
}

public HashTableOpenAddressing(int initialCapacity, double loadFactorIn) {
numEntries = 0;
if (loadFactorIn <= 0 || initialCapacity <= 0) {
throw new IllegalArgumentException("Initial capacity and load " +
"factor must be greater than 0");
}
else if (initialCapacity > MAX_CAPACITY)
throw new IllegalStateException("Attempt to create a dictionary " +
"whose capacity is larger than " + MAX_CAPACITY);

loadFactor = loadFactorIn;
  
// Set up hash table:
// Initial size of hash table is same as initialCapacity if it is prime;
// otherwise increase it until it is prime size
tableSize = getNextPrime(initialCapacity);
  
  
@SuppressWarnings("unchecked")
TableEntry< V>[] temp = (TableEntry< V>[]) new TableEntry[tableSize];
table = temp;
}
public int getNextPrime(int initialCapacity) {
           if(tableSize %2 == 0) {
               count++;
           }
if(count==0) {       
               tableSize=DEFAULT_CAPACITY;
              
           }
else {
   count++;
}
               return initialCapacity;
              
              
  
       }

      
      
       /** Task: Adds a new entry to the dictionary. If the given search
* key already exists in the dictionary, replaces the
* corresponding value.
* @param key an object search key of the new entry
* @param value an object associated with the search key
* @return either null if the new entry was added to the dictionary or the
* value that was associated with key if that value was replaced */
public V add(K keyIn, V valueIn) {
// TODO
return null;
}

private int linearProbe(int index, K keyIn) {
boolean found = false;
int removedStateIndex = -1; // Index of first removed location
if(table[index] == null){
return index;
}
while (!found && table[index] != null) {
if (( table[index]).isIn()) {
if (keyIn.equals(table[index].getKey())) {
found = true; // Key found
}
else{ // Follow probe sequence
index = (index + 1) % table.length;
}
}
else { // Skip entries that were removed
// Save index of first location in removed state
if (removedStateIndex == -1) {
removedStateIndex = index;
}
index = (index + 1) % table.length;
}
}
if (found || (removedStateIndex == -1) ) {
return index; // Index of either key or null
}
else {
return removedStateIndex; // Index of an available location
}
}

private int quadraticProbe(int index, K key) {
// TODO
return -1;
}

private int getHashIndex(K key) {
int hashIndex = -1;
// TODO
return hashIndex;
}

/** Task: Removes a specific entry from the dictionary.
* @param key an object search key of the entry to be removed
* @return either the value that was associated with the search key
* or null if no such object exists */
public V remove(K key) {
V removedValue = null;
int index = getHashIndex(key);
index = linearProbe(index, key);
// index = quadraticProbe(index, key);

if (index != -1){
  
                       // Key found; flag entry as removed and return its value
removedValue = table[index].getValue();
table[index].setToRemoved();
numEntries--;
}
// Else not found; result is null
return removedValue;
}

/** Task: Retrieves the value associated with a given search key.
* @param key an object search key of the entry to be retrieved
* @return either the value that is associated with the search key
* or null if no such object exists */
public V getValue(K key) {
  
return null;
}

/** Task: Sees whether a specific entry is in the dictionary.
* @param key an object search key of the desired entry
* @return true if key is associated with an entry in the
* dictionary */
public boolean contains(K keyIn) {
return getValue(keyIn) != null;
}

/** Task: Creates an iterator that traverses all search keys in the
* dictionary.
* @return an iterator that provides sequential access to the search
* keys in the dictionary */
public Iterator<K> getKeyIterator() {
  
}

/** Task: Creates an iterator that traverses all values in the
* dictionary.
* @return an iterator that provides sequential access to the values
* in the dictionary */
public Iterator<V> getValueIterator() {
  
}

/** Task: Gets the size of the dictionary.
* @return the number of entries (key-value pairs) currently
* in the dictionary */
public int getSize() {
// TODO
   tableSize.getSize();
return -1;
}

/** Task: Sees whether the dictionary is empty.
* @return true if the dictionary is empty */
public boolean isEmpty() {
if(tableSize==0) {
   System.out.println("Dictionary is empty");
}
return false;
}

/** Task: Sees whether the dictionary is full.
* @return true if the dictionary is full */
public boolean isFull() {

   if(tableSize>MAX_CAPACITY) {
       System.out.println("Dicionary is full");
   }
return false;
}

/** Task: Removes all entries from the dictionary. */
public void clear() {
table.clear();

}
}

Add a comment
Know the answer?
Add Answer to:
Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT