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

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

ID: 3666388 • Letter: 1

Question

1) 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).

2) 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:

a) The Listnode class is as described in the readings/lectures.

b) The Strings can be compared with the equals method.

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

d) 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.

e) A header node is not used by the chain of nodes.

Explanation / Answer

Multiple Questions with various sub and subsub parts: Answer Question 1

Part I :

a.) chain1 is empty and chain2 is empty
       returns : true
b.) chain1 contains Integers 11, 22, 33 and chain2 contains Integers 33, 22, 11
       returns : false
c.) chain1 contains Integers 33, 55, 77 and chain2 also contains Integers 33, 55, 77
       returns : true
d.) chain1 contains Integers 99, 22 and chain2 contains Integers 99, 22, 44
       returns : false
e.) both chain1 and chain2 contain Strings "This", "is, "a", "test"
       returns : true
f.) chain1 contains Strings "Hello", "from", "the", "outside" and chain2 contains "Hello", "from", "the", "other side"
       returns : false
g.) chain1 contains Strings "The Cat", "in", "the Hat" and chain2 is empty
       returns : false

Part II :

The method secret() checks whether the given two chains are equal or not. If both the chains are same or both are null, the methods returns true and else the method returns false.