rgs LinkedCollection fruits = new LinkedCollection veggies = new LinkedCollection all = fruits.combine(veggies); System.out.println("Combine all" +all.toString()); +veggies.toString());

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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
Transcribed Image Text:```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
```java
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.
}
```

### Explanation

This Java code defines a generic interface named `CollectionInterface<T>`. It outlines the structure for collections of objects of type `T`, where `T` is a placeholder for any data type.

- **`boolean add(T element);`**: Attempts to add an element to the collection. Returns `true` if successful, otherwise `false`.

- **`T get(T target);`**: Retrieves an element from the collection that equals the target. Returns `null` if no such element is found.

- **`boolean contains(T target);`**: Checks if the collection contains an element equivalent to the target. Returns `true` if found, else `false`.

- **`boolean remove(T target);`**: Removes an element matching the target from the collection, returning `true` if successful, `false` otherwise.

- **`boolean isFull();`**: Determines if the collection is full, returning `true` if it is, `false` otherwise.

- **`boolean isEmpty();`**: Checks if the collection is empty, returning `true` if it is, `false` otherwise.

- **`int size();`**: Returns the number of elements currently in the collection.

This interface serves as a blueprint that specifies the methods that any class implementing this interface should have, ensuring consistent behavior across different types of collections.
Transcribed Image Text:```java 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. } ``` ### Explanation This Java code defines a generic interface named `CollectionInterface<T>`. It outlines the structure for collections of objects of type `T`, where `T` is a placeholder for any data type. - **`boolean add(T element);`**: Attempts to add an element to the collection. Returns `true` if successful, otherwise `false`. - **`T get(T target);`**: Retrieves an element from the collection that equals the target. Returns `null` if no such element is found. - **`boolean contains(T target);`**: Checks if the collection contains an element equivalent to the target. Returns `true` if found, else `false`. - **`boolean remove(T target);`**: Removes an element matching the target from the collection, returning `true` if successful, `false` otherwise. - **`boolean isFull();`**: Determines if the collection is full, returning `true` if it is, `false` otherwise. - **`boolean isEmpty();`**: Checks if the collection is empty, returning `true` if it is, `false` otherwise. - **`int size();`**: Returns the number of elements currently in the collection. This interface serves as a blueprint that specifies the methods that any class implementing this interface should have, ensuring consistent behavior across different types of collections.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education