Question: Need some of Java quetion 3.1 Implement the interface Map Define the interface Map . A class that realizes Map has to save the key-value

Need some of Java quetion

3.1 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 right (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:

  1. V get(K key) : Returns the rightmost value associated to the key specified in the parameter.
  2. boolean contains(K key) : Returns true if there is an association for the key specified in the parameter
  3. void put(K key, V value) : Creates a new key-value association
  4. void replace(K key, V value) : Replaces the value of the rightmost occurrence of the association for the specified key
  5. V remove(K key) : Removes the rightmost 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

3.2 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. Study the pre-solved file provided. 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 rightmost 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).
  • It has getters, getCount() which returns the current number of items in the dictionary, and getCapacity() which returns the current length of the array.
  • DictionaryTest.java includes a series of tests for your implementation.

Note that you do not have to handle exceptional inputs in your solution (for example, removing, getting, or replacing a key that doesn't exist.)

//map.java:

public interface Map { /* Make the necessary abstract method definitions */ }

//Pair.java:

public class Pair { private final String key; private Integer value; public Pair(String key, Integer value) { this.key = key; this.value = value; } public String getKey() { return key; } public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } @Override public String toString() { return "{key=" + key + ",value=" + value + "}"; } }

// Need some of Java quetion 3.1 Implement the interface Map Define theinterface Map. A class that realizes Map has to save the key-value

public class Dictionary implements Map { private final static int INITIAL_CAPACITY = 10; private final static int INCREMENT = 5; private int count; private Pair[] elems; public int getCount() { return count; } public int getCapacity() { return elems.length; } public Dictionary() { /* Your code here */ } @Override public void put(String key, Integer value) { /* Your code here */ } private void increaseCapacity() { /* Your code here. Use this in put() where necessary. */ } @Override public boolean contains(String key) { /* Your code here. */ } @Override public Integer get(String key) { /* Your code here. */ } @Override public void replace(String key, Integer value) { /* Your code here. */ } @Override public Integer remove(String key) { /* Your code here. */ } @Override public String toString() { String res; res = "Dictionary: {elems = ["; for (int i = count-1; i >= 0 ; i--) { res += elems[i]; if(i > 0) { res += ", "; } } return res +"]}"; } }

1 publie class Dictionary implements Mapstring, Integer> private final static int INITIAL CAPACITY - 10 private final static int INCREMENT5; private int count; private Pair elems public int getcCount 10 return count 12 13 14 15 16 17 18 19 20 21 public int getcapacityO I return elema.length; public Dictionary) ( /Your code here/ public void put (string key Integer value) f 23 24 25 26 27 28 29 30 31 32 /Your code here private void increaseCapacity /Your code here Use this in put) where ncessary. */ Override public boolean contains(String key) Your code here/ 34 35 36 37 38 39 40 4 1 42 43 override public Integer get (String key) Your code here/ Override public void replace (String key, integer value /Your code here. / 43 45 4 47 override public Integer remove (3tring key) t Your code hexe. 49 50 51 52 53 54 override public String toatring) I string res; raa-"Dictionary: [elama = [" for (int 1-count-1; >-0 ; --) { res -elems [i 57 58 59 60 61 62 return res t"1

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!