In: Computer Science
Using Java. The following is a constructor I need to implement but I am not sure how. It also must pass the Junit test. Please give reasoning while answering.
public class LZWDictionary {
// FIELDS
// map (for ease of checking existence of entries)
// list (ease of recall of an entry)
LinkedHashMap<String, Integer> map;
List<String> list;
/**
* Initializes the LZWDictionary to have an initial set of entries taken from
* the set of unique characters in a provided string
*
*
*
* Unique characters are added to a map as they are encountered in the provided
* input string, with an increasing index value (beginning at index 0). At the same
* time, the characters are added to a list. The indices associated with each
* dictionary entry in the map thus relate to their index (position) in the list
*
*
*
*
* @param characters a string of initial characters that may include duplicates
*
* @throws an IllegalArgumentException if the characters string is empty
*
*/
public LZWDictionary(String characters) {
// NOTE: Complete the accessors getMap() and getList() first before running
// your tester on this ctor
}
Must pass this Junit Test
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public class LZWDictionary {
// FIELDS
// map (for ease of checking existence of entries)
// list (ease of recall of an entry)
LinkedHashMap<String, Integer> map;
List<String> list;
/**
*
* Initializes the LZWDictionary to have an initial set
of entries taken from
*
* the set of unique characters in a provided
string
*
*
*
*
*
*
*
* Unique characters are added to a map as they are
encountered in the provided
*
* input string, with an increasing index value
(beginning at index 0). At the
* same
*
* time, the characters are added to a list. The
indices associated with each
*
* dictionary entry in the map thus relate to their
index (position) in the list
*
*
*
*
*
*
*
*
*
* @param characters a string of initial characters
that may include duplicates
*
*
*
* @throws an IllegalArgumentException if the
characters string is empty
*
*
*
*/
public LZWDictionary(String characters) {
// NOTE: Complete the accessors getMap() and getList() first before running
// your tester on this ctor
//Initialising map
map = new
LinkedHashMap<>();
//Initialising list
list = new
ArrayList<>();
//Declaring String variable for
holding character
String str;
//Declaring index variable for
holding Value
int index;
//If block for checking empty
string.
if (characters.length() == 0)
//Throwing
IllegealArgumentException if String is empty.
throw new
IllegalArgumentException("Input should not be empty!!!");
else {
//Iterating
over the String
for (int i = 0;
i < characters.length(); i++) {
//Taking particular character in the
Iteration
str = "" + characters.charAt(i);
//Checking value of a particular character in
the map
if(map.get(str)==null) {
//If not present in map, then
add that to the map and list
map.put(str, 1);
list.add(str);
}
else {
index = map.get(str);
map.replace(str,
index+1);
}
}
}
}
public LinkedHashMap<String, Integer>
getMap() {
return map;
}
public List<String> getList() {
return list;
}
}