Question
*Java* Please complete the methods: containsKey, containsValue, get, isEmpty, put, remove, size and values. In a separate test docuemnt the previous methods should all return
*Java*
Please complete the methods: containsKey, containsValue, get, isEmpty, put, remove, size and values.
In a separate test docuemnt the previous methods should all return a green check for the following:
MyHashMap.java:
java.util.ArrayList; java.util.Collection; java.util.Map; java.util.Set;
public class MyHashMap implements Map {
private ArrayList table;
private int maxTableSize = 50; private int tableSize = 0;
public MyHashMap() { table = new ArrayList(); this.clear(); }
public MyHashMap(int maxSize) { table = new ArrayList(); maxTableSize = maxSize; this.clear(); }
public void clear() { table.clear(); tableSize = 0; for (int i = 0; i
}
public boolean containsKey(Object key) { return false; }
public boolean containsValue(Object arg0) { return false; }
public V get(Object key) { return null; }
public boolean isEmpty() { return false; }
public V put(K key, V value) { return null; }
public V remove(Object key) { return null; }
public int size() { return tableSize; }
public Collection values() { return null; }
// Do not implement methods passed this point public Set> entrySet() { return null; }
public Set keySet() { return null; }
public void putAll(Map arg0) {
} }
MyHashMapTest.java:
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.jupiter.api.Test;
/** * Test class for MyHashMap * */ class MyHashMapTest {
@Test void get_EmptyHashMap_ShouldReturnNull() { var hm = new MyHashMap
@Test void get_HashMapHasKey_ShouldReturnPairedValue() { var hm = new MyHashMap
var res = hm.get(key);
assertEquals(value, res); }
@Test void put_NewKey_ShouldContainKeyValue() { var hm = new MyHashMap
var res = hm.get(key);
assertEquals(value, res); }
@Test void put_KeyAlreadyExists_ShouldContainNewValue() { var hm = new MyHashMap
var res = hm.get(key);
assertEquals(secondValue, res); }
@Test void clear_HashMapHasSeveralKeys_ShouldRemoveAllItems() { var hm = new MyHashMap
hm.clear();
assertNull(hm.get(key)); assertNull(hm.get(key2)); }
@Test void containsKey_HashMapHasKey_ShouldReturnTrue() { var hm = new MyHashMap
hm.put(key, value);
assertTrue(hm.containsKey(key)); }
@Test void containsKey_HashMapDoesNotHaveKey_ShouldReturnTrue() { var hm = new MyHashMap
hm.put(key, value);
assertFalse(hm.containsKey(keyNotMapped)); }
@Test void containesValue_HashMapHasValue_ShouldReturnTrue() { var hm = new MyHashMap
hm.put(key, value);
assertTrue(hm.containsValue(value)); }
@Test void containesValue_HashMapDoesNotHaveValue_ShouldReturnTrue() { var hm = new MyHashMap
hm.put(key, value);
assertFalse(hm.containsValue(valueNotMapped)); }
@Test void empty_HashMapIsEmpty_ShouldReturnTrue() { var hm = new MyHashMap
assertTrue(hm.isEmpty()); }
@Test void empty_HashMapIsNotEmpty_ShouldReturnFalse() { var hm = new MyHashMap
hm.put(key, value);
assertFalse(hm.isEmpty()); }
@Test void remove_HashMapContainsKey_ShouldRemoveKey() { var hm = new MyHashMap
hm.put(key, value);
var removed = hm.remove(key);
assertEquals(value, removed); assertNull(hm.get(key)); }
@Test void size_HashMapContainsSeveralKeyValue_ShouldReturnExpectedSize() { var hm = new MyHashMap
hm.put(1, "JAVA"); hm.put(2, "JAVA"); hm.put(3, "JAVA"); hm.put(4, "JAVA"); hm.put(5, "JAVA");
assertEquals(expectedSize, hm.size()); }
void values_HashMapContainsSeveralKeyValue_ShouldReturnValues() { var hm = new MyHashMap
for (int i = 0; i
var res = hm.values();
assertEquals(expectedValues, res); }
void values_HashMapIsEmpty_ShouldReturnEmptyCollection() { var hm = new MyHashMap
var res = hm.values();
assertTrue(res.isEmpty()); } }
MyHashMap Test (Runner: JUnit 5] (0,211 s) empty_HashMapIsNotEmpty_ShouldReturnFalse0 (0,062 s) ** remove_HashMap ContainsKey_ShouldRemoveKey0 (0,005 s) ** contains Key_HashMapDoes Not HaveKey_ShouldReturn Truel ( clear_HashMapHasSeveralKeys_ShouldRemoveAllltems() (0,009 containesValue_HashMapHasValue_ShouldReturn Truel (0,006 s containesValue_HashMapDoes NotHaveValue_ShouldReturn Truel (o i put_KeyAlreadyExists_Should ContainNewValue() (0,007 s) x3 put_NewKey_Should ContainKeyValue() (0,010 s) get_EmptyHashMap_ShouldReturnNullO (0,011 s) 3 size_HashMap ContainsSeveralKeyValue_ShouldReturnExpectedSizel empty_HashMaplsEmpty_ShouldReturn Truel (0,016 s) contains Key_HashMapHasKey_ShouldReturn Truel (0,004 s) get_HashMapHasKey_ShouldReturnPairedValue() (0,005 s)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started