Add the following methods to the LinkedCollection class, and create a test drive
ID: 3831113 • Letter: A
Question
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.
1 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.
2 int count(T target) returns a count of the number of elements e in the collection such that e.equals(target) is true.
3 void removeAll(T target) removes all elements e from the collection such that e.equals(target) is true.
4 LinkedCollection combine(LinkedCollection 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 implements CollectionInterface
{
protected LLNode 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 location; // node containing target, if found
protected LLNode previous; // node preceding location
public LinkedCollection()
{
numElements = 0;
head = null;
}
public boolean add(T element)
// Adds element to this collection.
{
LLNode newNode = new LLNode(element);
newNode.setLink(head);
head = newNode;
numElements++;
return true;
}
protected void find(T target)
// Searches the collection for an occurence of an element e such that
// e.equals(target). If successful, sets instance variables
// found to true, location to node containing e, and previous
// to the node that links to location. If not successful, sets
// found to false.
{
location = head;
found = false;
while (location != null)
{
if (location.getInfo().equals(target)) // if they match
{
found = true;
return;
}
else
{
previous = location;
location = location.getLink();
}
}
}
public int size()
// Returns the number of elements on this collection.
{
return numElements;
}
public boolean contains (T target)
// Returns true if this collection contains an element e such that
// e.equals(target); otherwise, returns false.
{
find(target);
return found;
}
public boolean remove (T target)
// Removes an element e from this collection such that e.equals(target)
// and returns true; if no such element exists, returns false.
{
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)
// Returns an element e from this collection such that e.equals(target);
// if no such element exists, returns null.
{
find(target);
if (found)
return location.getInfo();
else
return null;
}
public boolean isEmpty()
// Returns true if this collection is empty; otherwise, returns false.
{
return (numElements == 0);
}
public boolean isFull()
// Returns true if this collection is full; otherwise, returns false.
{
return false; // Linked implementation is never full
}
}
--------------------------------------------------------------
package support;
public class LLNode<T>
{
protected LLNode<T> 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<T> link){this.link = link;}
public LLNode<T> getLink(){ return link;}
}
------------------------------------------------------------------------
CollectionInterface
package ch05.collections;
public interface CollectionInterface<T>
{
boolean add(T element);
// Attempts to add element to this collection.
// Returns true if successful, false otherwise.
T get(T target);
// Returns an element e from this collection such that e.equals(target).
// If no such e exists, returns null.
boolean contains(T target);
// Returns true if this collection contains an element e such that
// e.equals(target); otherwise returns false.
boolean remove (T target);
// Removes an element e from this collection such that e.equals(target)
// and returns true. If no such e exists, returns false.
boolean isFull();
// Returns true if this collection is full; otherwise, returns false.
boolean isEmpty();
// Returns true if this collection is empty; otherwise, returns false.
int size();
// Returns the number of elements in this collection.
}
Explanation / Answer
Hi, I have implemented first three methods.
Q4 is about returning SortedArrayCollection object. Since you have not posted SortedArrayCollection, so I can not complete it
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;
}
public boolean add(T element)
// Adds element to this collection.
{
LLNode<T> newNode = new LLNode<>(element);
newNode.setLink(head);
head = newNode;
numElements++;
return true;
}
protected void find(T target)
// Searches the collection for an occurence of an element e such that
// e.equals(target). If successful, sets instance variables
// found to true, location to node containing e, and previous
// to the node that links to location. If not successful, sets
// found to false.
{
location = head;
found = false;
while (location != null)
{
if (location.getInfo().equals(target)) // if they match
{
found = true;
return;
}
else
{
previous = location;
location = location.getLink();
}
}
}
public int size()
// Returns the number of elements on this collection.
{
return numElements;
}
public boolean contains (T target)
// Returns true if this collection contains an element e such that
// e.equals(target); otherwise, returns false.
{
find(target);
return found;
}
public boolean remove (T target)
// Removes an element e from this collection such that e.equals(target)
// and returns true; if no such element exists, returns false.
{
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)
// Returns an element e from this collection such that e.equals(target);
// if no such element exists, returns null.
{
find(target);
if (found)
return location.getInfo();
else
return null;
}
public boolean isEmpty()
// Returns true if this collection is empty; otherwise, returns false.
{
return (numElements == 0);
}
public boolean isFull()
// Returns true if this collection is full; otherwise, returns false.
{
return false; // Linked implementation is never full
}
@Override
public String toString() {
String res = "[";
LLNode<T> temp = head;
while(temp != null){
if(temp.getLink() == null)
res = res + temp.getInfo();
else
res = res + temp.getInfo()+", ";
}
res = res + "]";
return res;
}
public int count(T target){
int count = 0;
LLNode<T> temp = head;
while(temp != null){
if(temp.getInfo().equals(target))
count++;
}
return count;
}
public void removeAll(T target){
LLNode<T> temp = head;
while(temp != null){
if(temp.getInfo().equals(target))
temp.setLink(temp.getLink());
else
temp = temp.getLink();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.