rgs LinkedCollection fruits = new LinkedCollection veggies = new LinkedCollection all = fruits.combine(veggies); System.out.println("Combine all" +all.toString()); +veggies.toString());
Explain why the code below does not run when using the test driver shown in screen shot. i also included 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<>(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() {
//creates and returns a string that correctly represents the current collection
String result = "";
LLNode<T> cursor= head;
while (cursor!= null) {
result += cursor.getInfo() + "\n";
cursor = cursor.getLink();}
return result;
}
public int count(T target){
//return a count of the number of elements e in the collection such that e.equals(target) is true
int count = 0;
LLNode<T> temp = head;
while(temp != null){
if(temp.getInfo().equals(target))
count++;
}
return count;
}
public void removeAll(T target){
//remove all elements e from the collection such that e.equal(T target) is true
LLNode<T> temp = previous;
while(temp != null){
if(temp.getInfo().equals(target))
temp.setLink(temp.getLink());
else
temp = temp.getLink();
remove(target);
}
}
LinkedCollection<T>combine(LinkedCollection<T> other) {
//creates and returns a new LinkedCollection object that is a combination this object and argument object
int index1 = 0, index2 = 0, index3 = 0;
int size1 = size(), size2 = other.size();
LinkedCollection<T> col = new LinkedCollection<>(size1 + size2);
while(index1 < size1 && index2 < size2) {
if(((Comparable)(elements[index1])).compareTo(other.elements[index2]) < 0)
col.add(elements[index1++]);
else
col.add(other.elements[index2++]);
}
while(index1 < size1)
col.add(elements[index1++]);
while(index2 < size2)
col.add(other.elements[index2++]);
return col;
}
}
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;
}
}
![```java
public class test {
public static void main(String[] args) {
LinkedCollection<String> fruits = new LinkedCollection<String>();
LinkedCollection<String> veggies = new LinkedCollection<String>();
fruits.add("grapes");
fruits.add("pineapple");
fruits.add("apple");
System.out.println("The fruits collection is " + fruits.toString());
System.out.println("count is" + count([]));
veggies.add("tomato");
veggies.add("pepper");
veggies.add("broccoli");
veggies.add("potato");
fruits.removeAll("grape");
veggies.removeAll("tomato");
System.out.println("After removeAll fruits " + fruits.toString() + "\n");
System.out.println("veggies " + veggies.toString());
LinkedCollection<String> all = fruits.combine(veggies);
System.out.println("Combine all " + all.toString());
}
}
```
### Explanation:
This Java code defines a `test` class with a `main` method that demonstrates the use of the custom `LinkedCollection` class to manage collections of fruits and vegetables.
- **Collections Creation**:
- Two `LinkedCollection<String>` instances named `fruits` and `veggies` are created.
- **Adding Items**:
- Fruits added to the collection include "grapes", "pineapple", and "apple".
- Vegetables added include "tomato", "pepper", "broccoli", and "potato".
- **Displaying Collections**:
- Prints the contents of the `fruits` collection.
- Attempts to print a count using `count([])`, but this seems to be an error as `count` isn't defined and the syntax is incorrect.
- **Removing Items**:
- Attempts to remove "grape" from `fruits` and "tomato" from `veggies`.
- **Post-removal Print**:
- Displays the contents of the `fruits` and `veggies` collections after removal actions.
- **Combining Collections**:
- Combines the `fruits` and `veggies` collections and prints the combined result.
Note: The code snippet has syntax errors. Specifically, `count([])` appears to be incorrect. Additionally, "grape" and "grapes" inconsistency, and missing class `LinkedCollection` definition or](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F1b2c1e36-9974-49eb-8a0c-fe4d3c635f06%2Ff118be13-662d-4d3e-9420-21afa885f150%2F5mye7cf_processed.png&w=3840&q=75)


Trending now
This is a popular solution!
Step by step
Solved in 2 steps









