Question: Maps In class we implemented a map, which stores values by key. Add the following methods to both ArrayMap and LinkedMap: V remove (K key);

Maps

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

/* * driver program for ArrayMap and LinkedMap */ public class Maps { public static void main (String [] args) { try { //Map ages = new ArrayMap(); Map ages = new LinkedMap(); // creating map ages.add("Maurice", 20); ages.add("Sylvia", 18); System.out.println(" Here's the group " + ages); // adding key not in map ages.add("Leo", 7); System.out.println(" Leo is joining the group " + ages); // adding key already in map (= replacing value) System.out.println(" Sylvia just had a birthday"); System.out.println("Her old age was " + ages.add("Sylvia", 19)); System.out.println(ages); // printing keys System.out.println(" Here are the people in the group"); System.out.println(ages.getKeys()); // searching for key System.out.println(" Is Sylvia in the group? " + (ages.containsKey("Sylvia") ? "yes" : "no")); System.out.println(ages); // removing pair System.out.println(" Sylvia has moved away"); System.out.println("Her age when she left " + ages.remove("Sylvia")); System.out.println(ages); System.out.println("Is Sylvia in the group? " + (ages.containsKey("Sylvia") ? "yes" : "no")); // trying to remove pair that isn't in map System.out.println("Trying to remove Sylvia again"); System.out.println(ages.remove("Sylvia")); // getting value for key System.out.println(" How old is Maurice? " + ages.get("Maurice")); // trying to add null value System.out.println(" Trying to add Kyle (his age is unknown"); ages.add("Kyle", null); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } } } 
/* * interface for a map * * data stored by key, with no duplicate keys allowed */ public interface Map { V add (K key, V value); V remove (K key); V get(K key); boolean containsKey(K key); String getKeys(); int size(); String toString(); } 
/* * array implementation of a map * * data stored by key, with no duplicate keys allowed */ public class ArrayMap implements Map { 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) { return null; } /* * returns value stored for key * if key not found, returns null */ public V get(K key) { 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; } } } 
/* * linked implementation of a map * * data stored by key, with no duplicate keys allowed */ public class LinkedMap implements Map { 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) { return null; } /* * returns value associated with key * if key not found, returns null */ public V get(K key) { 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; } } 

Next, write a program Seuss2.java that uses a map to get the number of occurrences of each word in the book Green Eggs and Ham. The program should work similarly to the program Seuss.java that you wrote for Lab 4 (i.e., ignore punctuation and capitalization on the words). The data that gets written to the output file should look like this:

[thank=2, good=2, if=1, try=4, so=5, boat=3, goat=4, rain=4, dark=7, the=11, say=5, on=7, train=9, be=4, me=4, let=4, tree=6, see=4, will=21, may=4, are=2, they=2, car=7, could=14, fox=7, box=7, eat=25, mouse=8, with=19, house=8, a=59, in=40, anywhere=8, there=9, or=8, here=11, would=26, them=61, ham=11, and=25, eggs=11, green=11, you=34, like=44, not=84, do=37, that=3, sam=19, am=16, i=85]

//Previous Seuss program written for sets is below, this Seuss2 program should be similar but apply to maps producing the above output. The contents of the text file of Green Eggs and Ham is below the sets code

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class Seuss {

// returns a string retaining only characters and converting to lower case

// all punctuation are removed

public static String removePunc(String s) {

String result = "";

s = s.toLowerCase();

for (int i = 0; i < s.length(); i++) {

char ch = s.charAt(i);

if (isLetter(ch))

result += ch;

}

return result;

}

public static boolean isLetter(char ch) {

if (ch >= 'a' && ch <= 'z')

return true;

else if (ch >= 'A' && ch <= 'Z')

return true;

else

return false;

}

public static void main(String[] args) {

String filename = "greenEggs.txt";

try {

Scanner inFile = new Scanner(new File(filename));

String word;

LinkedSet set = new LinkedSet();

while (inFile.hasNext()) {

word = inFile.next();

word = removePunc(word);

set.add(word);

}

inFile.close();

// write the set contents to a file

PrintWriter pw = new PrintWriter(new File("words.txt"));

pw.write(set.toString());

pw.close();

System.out.println("Saved the words in set to file words.txt ");

System.out.println(set.toString());

} catch (FileNotFoundException e) {

System.out.println(e.getMessage());

}

}

}

I am Sam I am Sam Sam I am

That Sam I am! That Sam I am! I do not like that Sam I am!

Do you like green eggs and ham?

I do not like them, Sam I am. I do not like green eggs and ham.

Would you like them here or there?

I would not like them here or there. I would not like them anywhere. I do not like green eggs and ham. I do not like them, Sam I am.

Would you like them in a house? Would you like them with a mouse?

I do not like them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them anywhere. I do not like green eggs and ham. I do not like them, Sam I am.

Would you eat them in a box? Would you eat them with a fox?

Not in a box. Not with a fox. Not in a house. Not with a mouse. I would not eat them here or there. I would not eat them anywhere. I would not eat green eggs and ham. I do not like them, Sam I am.

Would you? Could you? In a car? Eat them! Eat them! Here they are.

I would not, could not, in a car.

You may like them. You will see. You may like them in a tree!

I would not, could not in a tree. Not in a car! You let me be!

I do not like them in a box. I do not like them with a fox. I do not like them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them anywhere. I do not like green eggs and ham. I do not like them, Sam I am.

A train! A train! A train! A train! Could you, would you, on a train?

Not on a train! Not in a tree! Not in a car! Sam! Let me be!

I would not, could not, in a box. I could not, would not, with a fox. I will not eat them with a mouse. I will not eat them in a house. i will not eat them here or there. I will not eat them anywhere. I do not eat green eggs and ham. I do not like them, Sam I am.

Say! In the dark? Here in the dark! Would you, could you, in the dark?

I would not, could not, in the dark.

Would you, could you, in the rain?

I would not, could not, in the rain. Not in the dark. Not on a train. Not in a car. Not in a tree. I do not like them, Sam, you see. Not in a house. Not in a box. Not with a mouse. Not with a fox. I will not eat them here or there. I do not like them anywhere!

You do not like green eggs and ham?

I do not like them, Sam I am.

Could you, would you with a goat?

I would not, could not, with a goat!

Would you, could you, on a boat?

I could not, would not, on a boat. I will not, will not, with a goat. I will not eat them in the rain. I will not eat them on a train. Not in the dark! Not in a tree! Not in a car! You let me be! I do not like them in a box. I do not like them with a fox. I will not eat them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them ANYWHERE

I do not like green eggs and ham!

I do not like them, Sam I am.

You do not like them. So you say. Try them! Try them! And you may. Try them and you may, I say.

Sam! If you will let me be, I will try them. You will see.

Say! I like green eggs and ham! I do! I like them, Sam I am! And I would eat them in a boat. And I would eat them with a goat...

And I will eat them in the rain. And in the dark. And on a train. And in a car. And in a tree. They are so good, so good, you see!

So I will eat them in a box. And I will eat them with a fox. And I will eat them in a house. And I will eat them with a mouse. And I will eat them here and there. Say! I will eat them ANYWHERE!

I do so like green eggs and ham! Thank you! Thank you, Sam I am!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!