Define the interface map a class that realizes map has to


Generic data structures

It is possible to use generic types to implement classes or interfaces. These can be reused in many contexts, with parameterized types. Remember the interface Comparable! By using the generic type, we make sure to keep the compiler validation.

Implement the interface Map

Define the interface Map. A class that realizes Map has to save the key-value association. In our case, we need a Map to contain identical keys, in which case the method get, put, replace, and remove will refer to the association containing this key that is the further left (the last). Map is a generic class with two types of parameters, K and V, where K is the type of the Key and V is the type of the values. A Map has the following methods:

V get(K key) : Returns the leftmost value associated to the key specified in the parameter.
boolean contains(K key) : Returns true if there is an association for the key specified in the parameter
void put(K key, V value) : Creates a new key-value association
void replace(K key, V value) : Replaces the value of the leftmost occurrence of the association for the specified key
V remove(K key) : Removes the leftmost occurrence of the specified key and returns the value that was associated to this key
Note that no parameter can be null in any of these methods

Implement the class Dictionary
Dictionary keeps track of String-Integer associations (key-value).
The Dictionary class implements the interface Map .

It uses an array (first instance variable) to store each association. The elements in this array are of type Pair , a class that will be public. A Pair type object must store an association, "key" and "value" of type String and Integer respectively. Note this is an alternative solution until nested classes are taught in class.

You must treat your array of items as a stack. So the bottom of your stack will be element 0, while the top of the stack will be the non-null element at the highest position. Use a counter (second instance variable) that will tell you how many items are in your table.

Finally, because the Dictionary class stores an arbitrarily large number of associations, you will need to use the dynamic table technique (as shown in part 1). The initial size of the array will be 10, while its increment will be 5 whenever the array is full. These two values will be treated as constants.

You clearly need to implement all methods of the interface. Do not forget to consider that the leftmost association (element of type Pair) will always be the top most of the stack! You will therefore have to go through your table in the opposite direction.
Add a toString() method that prints your table elements from last to first (top of stack to bottom of stack).

DictionaryTest.java includes a series of tests for your implementation.

Attachment:- DictionaryTest.rar

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Define the interface map a class that realizes map has to
Reference No:- TGS02384735

Now Priced at $15 (50% Discount)

Recommended (93%)

Rated (4.5/5)