Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ENTRY.JAVA CLASS * @param - type of the key * @param - type of the values associated to key */ public class Entry { private

ENTRY.JAVA CLASS

* @param - type of the key * @param - type of the values associated to key */ public class Entry { private K key; private List values;

/** * @param key */ public Entry(K key) { this.setKey(key); values = new ArrayList(); } /** * creates a new entry. * add value to the list of values * @param key * @param value */ public Entry(K key, V value) { this.setKey(key); values = new ArrayList(); values.add(value); }

/** * @return the key */ public K getKey() { return key; }

/** * @param key the key to set */ public void setKey(K key) { this.key = key; } /** * values getter * @return values */ public List getValues(){ return values; } }

MULTIMAP.JAVA CLASS

@param * - the type of keys maintained by this map * @param * - the type of mapped values */ public interface Multimap { /** * Associates the specified value with the specified key in this map. If the * multimap previously contained a mapping for the key, the new value is * appended to the list of old values. Throws: NullPointerException - if the * specified key or value is null IllegalStateException - if the value is * already associated with key (no duplicate values) * * @param key * key with which the specified value is to be associated * @param value * value to be associated with the specified key * @return The last (added) previous value associated with key, or null if * there was no mapping for key */ public V put(K key, V value);

/** * @return Returns true if this map contains no key-value mappings. */ public boolean isEmpty();

/** * Returns a list of values to which the specified key is mapped, or null if * this map contains no mapping for the key. Throws: NullPointerException - * if the specified key is null.IllegalStateException - if the key is not in * the multimap. * * @param key * - the key whose associated value is to be returned * @return the list of values mapped to the key or null if the map contained * no mapping for the key */ public List get(Object key);

/** * Replaces the entry for the specified key only if currently mapped to the * specified value. Throws: NullPointerException - if the specified key or * oldValue or newValue is null. IllegalStateException - if the key is not * in the multimap, or oldValue is not already associated with key; or if * oldValue and newValue are the same. * * @param key * - key with which the specified value is associated * @param oldValue * - value expected to be associated with the specified key * @param newValue * - value to be associated with the specified key * @return true if the value was replaced */ public boolean replace(K key, V oldValue, V newValue);

/** * Removes the mapping for a key from this map if it is present. The key and * its associated values are removed. Throws: NullPointerException - if the * specified key is null. IllegalStateException - if key is not in the * multimap * * @param key * - key whose mapping is to be removed from the multimap * @return the list of values mapped to the key or null if the map contained * no mapping for the key */ public List remove(Object key);

/** * Removes the entry for the specified key only if it is currently mapped to * the specified value. value is removed from the list of values. If value * is the only value associated with key, then key is also removed. Throws: * NullPointerException - if the specified key or value is null. * IllegalStateException - if key is not in the multimap. * key is not removed if it is not mapped with value * * @param key * - key with which the specified value is associated * @param value * - value expected to be associated with the specified key * @return true if the value was removed. */ public boolean remove(Object key, Object value);

/** * @return the number of keys mappings in this multimap */ public int size();

}

QUESTION TO BE ANSWERED

I- Entry class

Download the starter package in eclipse. It contains two classes Multimap.java and Entry.java.

Entry.java has two data fields; key that will store the key and values (a list) that will store all the

values associated with key. For example, Allentown can be the key and the precipitations the values.

I.1 Add equals method

Before we can test the Entry class we need to add the equals method.

Add a public boolean equals(Object o)method to Entry class. You should

return true only if the keys are the same. do not compare the values.

I.2 Test Entry.java

Create EntryTest.java and test Entry.java thoroughly. Do not forget to extend TestCase.

To test Entry with the information in the table above, declare an Entry (in this case map) as

Entry map ; the type of the key is String and the values are Double.

I - Implement Multimap

Create a class ListMultimap.java that implements Multimap.

Let eclipse add the unimplemented methods.

Add a ArrayList data field, you will store the data in that variable.

Declare a no argument constructor and instantiate the ArrayList in it.

Create a class to ListMultimapTest.java to test ListMultimap.java.

It is good practice to test each method before implementing it

Note that eclipse will not be able to infer generic arguments in all situations especially

when manipulating the values field of an Entry object.

Do not forget to test the exceptions.

A multimap is a collection that associates more that one value to a specific key.

Lets imagine that we are trying to store the following table in a program.

Average monthly precipitation in inch. The city will be the key and the

monthly precipitation numbers the values.

City

Jan

Feb

Mar

Apr

May

Jun

Jul

Aug

Sep

Oct

Nov

Dec

Philadelphia

3.03

2.64

3.78

3.54

3.7

3.43

4.33

3.5

3.78

3.19

2.99

3.54

Allentown

3.03

2.72

3.39

3.54

4.13

4.29

4.96

3.7

4.61

3.9

3.5

3.58

Phoenix

0.91

0.91

0.98

0.28

0.12

0.04

1.06

0.98

0.63

0.59

0.67

0.87

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is a verb?

Answered: 1 week ago

Question

10. Microsoft Corporation

Answered: 1 week ago

Question

4. EMC Corporation

Answered: 1 week ago