Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following secret generic method: Part I: What is returned by method

ID: 3667000 • Letter: C

Question

Consider the following secret generic method:

Part I: What is returned by method secret() given the contents of the chains of nodes described below. To get the maximum practice from this question, carefully trace the code rather than attempting to run it.

chain1 is empty and chain2 is empty

chain1 contains Integers 11, 22, 33 and chain2 contains Integers 33, 22, 11

chain1 contains Integers 33, 55, 77 and chain2 also contains Integers 33, 55, 77

chain1 contains Integers 99, 22 and chain2 contains Integers 99, 22, 44

both chain1 and chain2 contain Strings "This", "is, "a", "test"

chain1 contains Strings "Hello", "from", "the", "outside" and chain2 contains "Hello", "from", "the", "other side"

chain1 contains Strings "The Cat", "in", "the Hat" and chain2 is empty

Part II: What does method secret() do in general? Do not just redescribe the code. Instead, briefly state what the method does at a high level (i.e., give its purpose).

Part III:

Complete the following method that is passed a String and a chain of nodes containing Strings. The method removes all occurrences of the target String in the chain of nodes and returns a count of how many times that target String was removed.

Be careful to handle boundary cases, e.g., when the first node and/or last node have Strings that match the target as well as when the chain is empty or has only one node. You may assume the following:

The Listnode<E> class is as described in the readings/lectures.

The Strings can be compared with the equals method.

The Strings in the chain are guaranteed not to be null.

The chain is guaranteed to be a correctly formed singly-linked chain of nodes with the next field of the last node set to null.

Corrected: A header node is used by the chain of nodes.

Explanation / Answer

Part I:
   a. chain1 is empty and chain2 is empty : true
   b. chain1 contains Integers 11, 22, 33 and chain2 contains Integers 33, 22, 11 : false
   c. chain1 contains Integers 33, 55, 77 and chain2 also contains Integers 33, 55, 77 : true
   d. chain1 contains Integers 99, 22 and chain2 contains Integers 99, 22, 44 : false
   e. both chain1 and chain2 contain Strings "This", "is, "a", "test"
   f. chain1 contains Strings "Hello", "from", "the", "outside" and : flase
       chain2 contains "Hello", "from", "the", "other side"
   g. chain1 contains Strings "The Cat", "in", "the Hat" and chain2 is empty: false
  
Part II: this method is checking if both list contains same number of nodes and each corresponding nodes from
       start has same data value of not
      
Part III:
       public static int removeAll(String target, Listnode<String> chain) {
           if(target == null){
               throw new IllegalArgumentException("chain is empty");
           }
           if(chain == null)
               return 0;
           int count = 0;
           while (chain != null) {
               if (chain.getData().equals(target)) {
                   count++;
               }
               chain = chain.getNext();
           }
           return count;
       }