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 }
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();
}
}
Implement the missing methods in java! Thanks! import java.util.Iterator; import java.util.NoSuchElementException; public class HashTableOpenAddressing<K, V> implements...
Please implement MyMaxPriorityQueue below. Thanks import net.datastructures.Entry; public class MyMaxPriorityQueue<K, V> { @Override public int size() { // TODO Auto-generated method stub return 0; } @Override public Entry<K, V> insert(K key, V value) throws IllegalArgumentException { // TODO Auto-generated method stub return null; } @Override public Entry<K, V> max() { // TODO Auto-generated method stub return null; } ...
//MultiValuedTreeMap.java import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; //import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> { private static final long serialVersionUID = -6229569372944782075L; public void add(K k, V v) { // Problem 1 method // empty linked list, with key=k if (!containsKey(k)) { put(k, new LinkedList<V>()); } // adding v to the linked list associated with key k get(k).add(v); } public V removeFirst(K k)...
package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic * collection to store elements where indexes represent priorities and the * priorities can change in several ways. * * This collection class uses an Object[] data structure to store elements. * * @param <E> The type of all elements stored in this collection */ // You will have an error until you have have all methods // specified in interface PriorityList included inside this...
import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> { private static final long serialVersionUID = -6229569372944782075L; public void add(K k, V v) { if (!containsKey(k)) { put(k, new LinkedList<V>()); } get(k).add(v); } public V removeFirst(K k) { if (!containsKey(k)) { return null; } V value = get(k).removeFirst(); if (get(k).isEmpty()) { super.remove(k); } return value; }...
Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the overlapping content of the bags. Intersections are explained in more detail in Chapter 1, #6. An intersecion might contain duplicates. The method should not alter either bag. The current bag and the bag sent in as a parameter should be the same when the method ends. The method header is: public BagInterface<T> intersection(ResizableArrayBag <T> anotherBag) Example: bag1 contains (1, 2, 2, 3) bag2 contains...
In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT { private BinaryTreeNode root; /** * Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; } /** * Creates a binary tree from an existing root. */ public LinkedBinaryTree(BinaryTreeNode root) { this.root = root; } /** * Creates a binary tree with the specified element...
JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator; /* * GroupsQueue class supporting addition and removal of items * with respect to a given number of priorities and with * respect to the FIFO (first-in first-out) order for items * with the same priority. * * An example, where GroupsQueue would be useful is the airline * boarding process: every passenger gets assigned a priority, * usually a number, e.g., group 1,...
Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E> { /** Reference to the first node in our linked list. This will be null if the list is empty. */ private Entry head; /** * Creates an empty list. */ public SinglyLinkedList() { reset(); } /** * This method, which is only used within the SinglyLinkedList class, returns the instance to its initial state. This * call will reset the head to be...
Suppose that you have several numbered billiard balls on a pool table. The smallest possible number on the ball is “1”. At each step, you remove a billiard ball from the table. If the ball removed is numbered n, you replace it with n balls randomly numbered less than n. For example, if you remove the “5” ball, you replace it with balls numbered “2”, “1”, “1”, “4”, and “3”, where numbers 2, 1, 1, 4, and 3 were randomly...
/** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; } /** * Create a String that...