Add the following methods to the linkedcollection class. String toString() creates and returns a string that correctly represents the current collection Int count (T target) return a count of the number of elements e in the collection such that e.equals(target) is true void removeAll(T target) remove all elements e from the collection such that e.equal(T target) remove

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 18RQ
icon
Related questions
Question

Add the following methods to the linkedcollection class.

String toString() creates and returns a string that correctly represents the current collection

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

void removeAll(T target) remove all elements e from the collection such that e.equal(T target) remove

LinkedCollection<T> combine (LinkedCollection<T> other ) creates and returns a new Linkedcollection object that is a combination this object and argument object

Then write a test driver that will call each method listed above to show that they are implentanted correctly screenshot shown the LLNode class and Collectioninterface interface

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<T>(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

}

}

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)
// 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.
}
Transcribed Image Text: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) // 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. }
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()I
return link;
}
}
Transcribed Image Text: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()I return link; } }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

You failed to include the count and toString methods.

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Map
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT