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

In class we implemented a map, which stores values by key. Add the following met

ID: 3607267 • Letter: I

Question

In class we implemented a map, which stores values by key. Add the following methods to both ArrayMap and LinkedMap:

V remove (K key); V get (K key);

Then compile and run Maps.java. Your output should look like this for ArrayMap (and the mirror image for LinkedMap, since it adds to the front):

Here's the group {Maurice=20, Sylvia=18}

Leo is joining them {Maurice=20, Sylvia=18, Leo=7}

Sylvia just had a birthday
Her previous age was 18 {Maurice=20, Sylvia=19, Leo=7}

Here are the people in the group [Maurice, Sylvia, Leo]

Is Sylvia in the group? yes {Maurice=20, Sylvia=19, Leo=7}

Sylvia has moved away
She was 19
{Maurice=20, Leo=7}
Is Sylvia in the group? no Trying to remove Sylvia again null

How old is Maurice? 20
Trying to add Kyle (age unknown) Key/value can't be null

Explanation / Answer

Hi, Here is the code that you have requested for.

Please rate this answer if it helps.

Note: To indent code in Eclipse IDE, you can select the code in each of the java file press "Ctrl + A" and then "Ctrl + i".


The output is as follows
With ArrayMap

Here's the group
{Maurice=20, Sylvia=18}
Leo is joining the group
{Maurice=20, Sylvia=18, Leo=7}
Sylvia just had a birthday
Her old age was 18
{Maurice=20, Sylvia=19, Leo=7}
Here are the people in the group
[Maurice, Sylvia, Leo]
Is Sylvia in the group? yes
{Maurice=20, Sylvia=19, Leo=7}
Sylvia has moved away
Her age when she left 19
{Maurice=20, Leo=7}
Is Sylvia in the group? no
Trying to remove Sylvia again
null
How old is Maurice? 20
Trying to add Kyle (his age is unknown
Key/value can't be null


With LinkedMap

Here's the group
{Sylvia=18, Maurice=20}
Leo is joining the group
{Leo=7, Sylvia=18, Maurice=20}
Sylvia just had a birthday
Her old age was 18
{Leo=7, Sylvia=19, Maurice=20}
Here are the people in the group
[Leo, Sylvia, Maurice]
Is Sylvia in the group? yes
{Leo=7, Sylvia=19, Maurice=20}
Sylvia has moved away
Her age when she left 19
{Leo=7, Maurice=20}
Is Sylvia in the group? no
Trying to remove Sylvia again
null
How old is Maurice? 20
Trying to add Kyle (his age is unknown
Key/value can't be null







// ================================== Code ============================================

//======================== ArrayMap.java ================
/*
* array implementation of a map
*
* data stored by key, with no duplicate keys allowed
*/
public class ArrayMap<K, V> implements Map<K, V>
{
private class Pair
{
private K key;
private V value;
public Pair(K key, V value)
{
this.key = key;
this.value = value;
}
public String toString()
{
return key + "=" + value;
}
}
public static final int DEFAULT_CAPACITY = 10;
private Pair [] collection;
private int size;
public ArrayMap ()
{
this(DEFAULT_CAPACITY);
}
@SuppressWarnings("unchecked")
public ArrayMap (int capacity)
{
collection = (Pair []) new ArrayMap.Pair[capacity];
size = 0;
}
  
/*
* if key in map, replaces old value with new and returns old value
* otherwise adds key value pair and returns null
*/

public V add (K key, V value)
{
checkForNull(key, value);
for (int i = 0; i < size; i++)
{
if (collection[i].key.equals(key))
{
V oldValue = collection[i].value;
collection[i].value = value;
return oldValue;
}
}
  
ensureSpace();
collection[size] = new Pair(key, value);
size++;
return null;
}
/*
* removes key value pair from map and returns value
* if key not found, returns null
*/

public V remove (K key)
{
if (key == null)
{
throw new IllegalArgumentException("Key/value can't be null");
}

for (int i = 0; i < size; i++)
{
if (collection[i].key.equals(key))
{
V value = collection[i].value;
  
//Create a new Array without the removed element
Pair [] newCollection = (Pair []) new ArrayMap.Pair[size*2];
int newIndex = 0;
for(newIndex = 0; newIndex < i; newIndex++) {
newCollection[newIndex] = collection[newIndex];
}

for(;newIndex < size; newIndex++) {
newCollection[newIndex] = collection[newIndex+1];
}

size--;
collection = newCollection;

return value;   
}
}

return null;
}
/*
* returns value stored for key
* if key not found, returns null
*/

public V get(K key)
{
if (key == null)
{
throw new IllegalArgumentException("Key/value can't be null");
}
for (int i = 0; i < size; i++)
{
if (collection[i].key.equals(key))
{
return collection[i].value;   
}
}
  
return null;
}
/*
* returns true if key is in map
*/

public boolean containsKey(K key)
{
for (int i = 0; i < size; i++)
{
if (collection[i].key.equals(key))
{
return true;
}
}
return false;
}
public int size()
{
return size;
}
public String getKeys()
{
if (size == 0)
{
return "[]";
}
  
String s = "[";
for (int i = 0; i < size; i++)
{
s+= collection[i].key;
if (i < size-1) s+= ", ";
}
return s + "]";
}
public String toString()
{
if (size == 0)
{
return "{}";
}
  
String s = "{";
for (int i = 0; i < size; i++)
{
s+= collection[i];
if (i < size-1) s += ", ";
}
return s + "}";
}
private void checkForNull (K key, V value)
{
if (key == null || value == null)
{
throw new IllegalArgumentException("Key/value can't be null");
}
}
private void ensureSpace()
{
if (size == collection.length)
{
@SuppressWarnings("unchecked")
Pair [] larger = (Pair []) new ArrayMap.Pair[size*2];
  
for (int i = 0; i < size; i++)
{
larger[i] = collection[i];
}
collection = larger;
larger = null;
}
}
}



//=================LinkedMap.java=======================
/*
* linked implementation of a map
*
* data stored by key, with no duplicate keys allowed
*/
public class LinkedMap<K, V> implements Map<K, V>
{
private class Node
{
private K key;
private V value;
private Node next;
public Node(K key, V value)
{
this.key = key;
this.value = value;
next = null;
}
public String toString()
{
return key + "=" + value;
}
}
private Node head;
private int size;
public LinkedMap ()
{
head = null;
size = 0;
}
  
/*
* if key in map, replaces old value with new and returns old value
* otherwise adds key value pair and returns null
*/
public V add (K key, V value)
{
checkForNull(key, value);
Node current = head;
for (int i = 0; i < size; i++)
{
if (current.key.equals(key))
{
V oldValue = current.value;
current.value = value;
return oldValue;
}
current = current.next;
}
Node newest = new Node(key, value);
newest.next = head;
head = newest;
size++;
return null;
}
/*
* removes key value pair from map and returns value
* if key not found, returns null
*/

public V remove (K key)
{
if (key == null)
{
throw new IllegalArgumentException("Key/value can't be null");
}


Node current = head, prev = null;
for (int i = 0; i < size; i++)
{
if (current.key.equals(key))
{
V value = current.value;
  
if(prev != null) {
prev.next = current.next;
}
  
size--;
return value;
}
  
prev = current;
current = current.next;
}
  
return null;
}
/*
* returns value associated with key
* if key not found, returns null
*/

public V get(K key)
{
if (key == null)
{
throw new IllegalArgumentException("Key/value can't be null");
}
Node current = head;
for (int i = 0; i < size; i++)
{
if (current.key.equals(key))
{
return current.value;
}
current = current.next;
}

return null;
}
/*
* returns true if key is in map
*/

public boolean containsKey(K key)
{
Node current = head;
while (current != null)
{
if (current.key.equals(key))
{
return true;
}
current = current.next;
}
return false;
}
public int size()
{
return size;
}
public String getKeys()
{
String list = "[";
  
Node current = head;
  
while (current.next != null)
{
list += current.key + ", ";
current = current.next;
}
  
return list + current.key + "]";
}
public String toString()
{
if (size == 0)
{
return "{}";
}
String list = "{";
Node current = head;
  
while (current.next != null)
{
list += current + ", ";
current = current.next;
}
  
return list + current + "}";
}
private void checkForNull (K key, V value)
{
if (key == null || value == null)
{
throw new IllegalArgumentException("Key/value can't be null");
}
}
private Node getNode (K key)
{
Node current = head;
int count = 0;
while (current != null)
{
if (current.key.equals(key))
{
return current;
}
  
current = current.next;
count++;
}
return null;
}
}



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