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

In JAVA Language Add the following methods to the LinkedCollection class, and cr

ID: 3847960 • Letter: I

Question

In JAVA Language

Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.

String toString() creates and returns a string that correctly represents the current collection. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stored element already provides its own reasonable toString method.

int count(T target) returns a count of the number of elements e in the collection such that e.equals(target) is true.

void removeAll(T target) removes all elements e from the collection such that e.equals(target) is true.

LinkedCollection<T> combine(LinkedCollection<T> other) creates and returns a new SortedArrayCollection object that is a combination of this object and the argument object.

package ch05.collections;

import support.LLNode;

public class LinkedCollection<T> implements CollectionInterface<T>

{

  protected LLNode<T> head;        // head of the linked list

  protected int numElements = 0;   // number of elements in this collection

  // set by find method

  protected boolean found;         // true if target found, else false

  protected LLNode<T> location;    // node containing target, if found

  protected LLNode<T> previous;    // node preceding location

  public LinkedCollection()

  {

    numElements = 0;

    head = null;

  }

Array-Based

Link-Based

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();

    }

}

}

Explanation / Answer

import java.util.*; public class LinkedCollection implements Collection { protected Entry head; // Postcondition: The LinkedCollection has been initialized. public LinkedCollection() { head = null; } // constructor // Postcondition: true has been returned if the Array is empty. // Otherwise, false has been returned. public boolean isEmpty() { return head == null; } // method isEmpty // Postcondition: o has been inserted at the front of the LinkedCollection // and true has been returned. public boolean add (Object o) { Entry temp = new Entry(); temp.element = o; temp.next = head; head = temp; return true; } // method add // Postcondition: A LinkedCollectionIterator has been returned. public Iterator iterator(){ return new LinkedCollectionIterator(); } // method iterator // Postcondition: the number of elements in this LinkedCollection // has been returned. public int size() { int count = 0; for (Entry current = head; current != null; current = current.next) count++; return count; } // method size // Postcondition: true has been returned if there is at least one // element in this LinkedCollection that is equal to // o. Otherwise, false has been returned. public boolean contains (Object o) { for (Entry current = head; current != null; current = current.next) if (current.element.equals (o)) return true; return false; } // method contains public Object[] toArray() { throw new UnsupportedOperationException( ); } public Object[] toArray(Object a[]) { throw new UnsupportedOperationException( ); } public boolean remove(Object o) { throw new UnsupportedOperationException( ); } public boolean containsAll(Collection c) { throw new UnsupportedOperationException( ); } public boolean addAll(Collection c) { throw new UnsupportedOperationException( ); } public boolean removeAll(Collection c) { throw new UnsupportedOperationException( ); } public boolean retainAll(Collection c) { throw new UnsupportedOperationException( ); } public void clear() { throw new UnsupportedOperationException( ); } public boolean equals(Object o) { throw new UnsupportedOperationException( ); } public int hashCode() { throw new UnsupportedOperationException( ); } private class LinkedCollectionIterator implements Iterator { private Entry next; // Postcondition: The iterator has been initialized. LinkedCollectionIterator() { next = head; } // constructor // Precondition: this Iterator is positioned at an element in // this Linked Collection. // Postcondition: The element this Iterator was (before this // call) positioned at has been returned, and // and this Iterator has advanced. public Object next() { Object theElement = next.element; next = next.next; return theElement; } // method next // Postcondition: true has been returned if this Iterator is positioned // an element in this Collection. Otherwise, false // has been returned. public boolean hasNext() { return next != null; } // method hasNext public void remove() {} // a nominal definition } // class LinkedCollectionIterator private static class Entry { Object element; Entry next; } // class Entry } // class LinkedCollection

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