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

Java Programming: Complete the following method that is passed a String and a ch

ID: 3667570 • Letter: J

Question

Java Programming:

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 (has only the header node) or has only one node containing an item. You may assume the following:

-The Listnode class is as described

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

- A header node is used by the chain of nodes.

Explanation / Answer

ChainOfString.java

=========================================================

public class ChainOfString {

   public static void main(String[] args) throws Exception {
       // constructing the chain of strings

       String[] data = new String[] { "Str1", "Str2", "Str3", "Str4", "Str5",
               "Str6", "Str3", "Str3" };
       // Header
       Listnode<String> headerchainStr = new Listnode<String>("Str1");
       Listnode<String> nextchainStr;
       Listnode<String> temp = headerchainStr;
       for (int i = 1; i < data.length; i++) {
           nextchainStr = new Listnode<String>(data[i]);
           temp.setNextNode(nextchainStr);
           temp = nextchainStr;
       }

       System.out.println(removeAll("Str3", headerchainStr));
   }

   public static int removeAll(String target, Listnode chain) throws Exception {

       if (target == null || chain == null) {
           throw new IllegalArgumentException();
       }
       Listnode<String> temp = chain;
       int count = 0;
       while (true) {
           if (temp == null) {
               break;
           }
           if (target.equals(temp.getData())) {
               count++;
           }
           temp = temp.getNextNode();
       }
       return count;
   }
}

========================================================

Listnode.java

public class Listnode<E> {

   private E data;

   private Listnode<E> nextNode;

   public Listnode(E data) {
       this(data, null);
   }

   public Listnode(E data, Listnode<E> nextNode) {
       this.data = data;
       this.nextNode = nextNode;
   }

   public E getData() {
       return data;
   }

   public void setData(E data) {
       this.data = data;
   }

   public Listnode<E> getNextNode() {
       return nextNode;
   }

   public void setNextNode(Listnode<E> nextNode) {
       this.nextNode = nextNode;
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote