Question: /* =================================================================================== Create a new collection class named SortedLinkedCollection that implements a collection using a sorted linked list. Include a toString method as described in

/* =================================================================================== Create a new collection class named SortedLinkedCollection that implements a collection using a sorted linked list. Include a toString method as described in Excercise 30a. Include a test driver >>NOTE: I have created the test driver which is working fine. Now, I need to turn this LinkedCollection.java into SortedLinkedCollection.java which will have add() method to sort aded elements, and toString()to display sorted element. ====================================================================================*/ public class SortedLinkedCollection extends LinkedCollection { protected LLNode head; // head of the linked list protected int numElements = 0; // number of elements in this collection protected boolean found; // true if target found, else false protected LLNode location; // node containing target, if found protected LLNode previous; // node preceding location public SortedLinkedCollection() { numElements = 0; head = null; } //This add method should add all elements in collection in assending order /* @Override public boolean add(T element) // Adds element to this collection. { LLNode newNode = new LLNode(element); LLNode temp = head; while (temp != null) { } } */ @Override public String toString() { String res = "["; LLNode temp = head; while(temp != null) { if(temp.getLink() == null) res = res + temp.getInfo(); else res = res + temp.getInfo()+", "; temp = temp.getLink(); } res = res + "]"; return res; } } //LinkedCollectionDriver.java //============================================================================================================ import java.util.*; public class LinkedCollectionDriver { public static void main(String[] args) { CollectionInterface test = new LinkedCollection(); // Testing for String Collection CollectionInterface sortTest = new SortedLinkedCollection(); // Tested for Sorted String Collection boolean keepTesting; String skip; int constructor; // user's choice of constructor char operation; // user's choice of operation String addElement = ""; String targetElement = ""; int count=0; Scanner keyboard = new Scanner(System.in); // Scanner to take user input keepTesting = true; while (keepTesting) { System.out.println(" Available Collection Operation ------------------------------"); System.out.println("\t 0: add(T element)"); System.out.println("\t 1: find(T target)"); System.out.println("\t 2: size()"); System.out.println("\t 3: remove(T target)"); System.out.println("\t 4: toString()"); //this option will display one line for sorted and one line for unsorted elments System.out.println("\t x: Stop() ------------------------------"); System.out.print("Enter a TaskNumber to Execute: "); if (keyboard.hasNext()) operation = keyboard.next().charAt(0); else { System.out.println("Error: Invalid Selection!"); System.out.println("Terminating Test."); return; } skip = keyboard.nextLine(); switch (operation) { case '0': // add element System.out.println(">>>Selected Operation: 0-add(T element)"); System.out.print("Enter an element to add in the collection: "); addElement = keyboard.nextLine(); test.add(addElement); sortTest.add(addElement); System.out.println(">>>Result: \"" +addElement +"\" added to the collection."); break; case '4': //toString System.out.println(">>>Selected Operation: 4-toString()"); System.out.println(">>>Current Unsorted elements in collection: " + test.toString()); //THIS PART IS WORKING OK. System.out.println(">>>Current Sorted elements in collection: " + sortTest.toString()); //NEED HELP HERE.. break; case 'x': // stop testing keepTesting = false; break; default: System.out.println("Error: Invalid Task Selection, Terminating test!"); return; } } System.out.println("End of Stack Operation"); } } // LinkedCollection.java by Dale/Joyce/Weems Chapter 5 //================================================================================================================ public class LinkedCollection implements CollectionInterface { protected LLNode head; // head of the linked list protected int numElements = 0; // number of elements in this collection protected boolean found; // true if target found, else false protected LLNode location; // node containing target, if found protected LLNode previous; // node preceding location public LinkedCollection() { numElements = 0; head = null; } public boolean add(T element) { LLNode newNode = new LLNode(element); newNode.setLink(head); head = newNode; numElements++; return true; } protected void find(T target) { location = head; found = false; while (location != null) { if (location.getInfo().equals(target)) { found = true; return; } else{ previous = location; location = location.getLink(); } } } public int size() {return numElements;} public boolean contains (T target){ find(target); return found; } public boolean remove (T target){ find(target); if (found) { if (head == location) head = head.getLink(); // remove first node else previous.setLink(location.getLink()); // remove node at location numElements--; } return found; } public T get(T target){ find(target); if (found) return location.getInfo(); else return null; } public boolean isEmpty(){ return (numElements == 0); } public boolean isFull(){ return false; // Linked implementation is never full } @Override public String toString() { String res = "["; LLNode temp = head; while(temp != null) { if(temp.getLink() == null) res = res + temp.getInfo(); else res = res + temp.getInfo()+", "; temp = temp.getLink(); } res = res + "]"; return res; } public int count(T target) { int count = 0; LLNode temp = head; while(temp != null) { if(temp.getInfo().equals(target)) count++; temp = temp.getLink(); } return count; } public void removeAll(T target) { LLNode temp = head; while(temp != null) { if(temp.getInfo().equals(target)) //remove { if(temp == head) head = temp.getLink(); else previous.setLink(temp.getLink()); } previous = temp; temp = temp.getLink(); } } } // CollectionInterface.java by Dale/Joyce/Weems Chapter 5 //================================================================================================================ public interface CollectionInterface { boolean add(T element); T get(T target); boolean contains(T target); boolean remove (T target); boolean isFull(); boolean isEmpty(); int size(); int count(T target); void removeAll(T target); } // LLNode.java by Dale/Joyce/Weems Chapter 2 //=============================================================================================================== public class LLNode { protected LLNode link; protected T info; public LLNode(T info) { this.info = info; link = null; } public void setInfo(T info){ this.info = info; } public T getInfo(){ return info; } public void setLink(LLNode link){ this.link = link; } public LLNode getLink(){ return link; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!