Question

In: Computer Science

Using Java. The following is a constructor I need to implement but I am not sure...

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

Solutions

Expert Solution

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;
   }

}


Related Solutions

I need this in Java please: Lab11B: Understanding the constructor. Remember that the constructor is just...
I need this in Java please: Lab11B: Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the...
Hi I am getting error in implement some test case using Java. I am adding my...
Hi I am getting error in implement some test case using Java. I am adding my code and relevant files here, and the failed test. Please let me know where my problem is occuring and fix my code. Thanks! I will upvote. Implement a class to perform windowing of a Hounsfield value Implement the class described by this API. A partial implementation is provided for you in the eclipse project; however, unlike the previous class, very little work has been...
I need this in java using textpad. I am missing a few lines where I added...
I need this in java using textpad. I am missing a few lines where I added in comments. I don't know what I need to add in. Here are the two programs as pasteable code.The comments in the code say what I need done. The two programs are below. I need it to work with the generic version of SLLNode. It is posted at the bottom. public class ListDemoHw { public static void printLinkedList(SLLNode node) { // display all elements...
I need this in java using textpad. I am missing a few lines where I added...
I need this in java using textpad. I am missing a few lines where I added in comments. I don't know what I need to add in. Here are the two programs as pasteable code.The comments in the code say what I need done. The two programs are below. public class ListDemoHw { public static void printLinkedList(SLLNode node) { // display all elements in the linked list while(node != null) { System.out.print(node.info + " "); node = node.next; // move...
I just need 3 and 5. I am not sure what I am doing wrong. I...
I just need 3 and 5. I am not sure what I am doing wrong. I get different numbers every time. Superior Markets, Inc., operates three stores in a large metropolitan area. A segmented absorption costing income statement for the company for the last quarter is given below: Superior Markets, Inc. Income Statement For the Quarter Ended September 30 Total North Store South Store East Store Sales $ 4,800,000 $ 960,000 $ 1,920,000 $ 1,920,000 Cost of goods sold 2,640,000...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure to label class and methods with clear descriptions describing what is taking place with the source code. Attach a snipping photo of source code and output
I am to complete the following for Java but getting compiler errors...Implement a base class called...
I am to complete the following for Java but getting compiler errors...Implement a base class called Student that contains a name and major. Implement the subclass GraduateStudent, which adds a property called stipend. Here is what I have for code with the compiler error following. public class GraduateStudent extends Student {       private double stipend;       public GraduateStudent(String name, String major, double stipend) {               super(name);        super(major);           this.stipend = stipend;...
I am using NetBeans IDE Java to code and I am seeking comment to the code...
I am using NetBeans IDE Java to code and I am seeking comment to the code as well (for better understanding). Magic squares. An n x n matrix that is filled with the numbers 1, 2, 3, …, n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that reads in 16 values from the keyboard, displays them in a 4...
Hello, I need to convert this java array into an array list as I am having...
Hello, I need to convert this java array into an array list as I am having trouble please. import java.util.Random; import java.util.Scanner; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); int[] data = new int[1000]; int count = 0; while (!choice.equals("No")) { int randomInt = 2 * (random.nextInt(5) + 1); System.out.println(randomInt); data[count++] = randomInt; System.out.print("Want another random number (Yes / No)? "); choice =...
IN JAVA: I am using binary and linear search methods in java. How can I Generate...
IN JAVA: I am using binary and linear search methods in java. How can I Generate a new array with 10000 elements, and initialize the elements to random values using Math.random() and how can i sort the array using Arrays.sort(). I just need examples, no exact code is necessary. The array must be of doubles.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT