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;

  }

}

 

 

 

5 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 "
System.out.println("count is"+çount());
veggies.add ("tomato");
veggies.add ("pepper");
veggies.add ("broccoli");
veggies.add ("potato");
+ fruits.toString());
fruits.removeAll("grape");
veggies.removeAll("tomato");
System.out.println("After removeAll fruits " +fruits.toString()+ "\n");
System.out.println("veggies
LinkedCollection<String> all = fruits.combine(veggies);
System.out.println("Combine all" +all.toString());
+veggies.toString());
}
}
Transcribed Image Text:5 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 " System.out.println("count is"+çount()); veggies.add ("tomato"); veggies.add ("pepper"); veggies.add ("broccoli"); veggies.add ("potato"); + fruits.toString()); fruits.removeAll("grape"); veggies.removeAll("tomato"); System.out.println("After removeAll fruits " +fruits.toString()+ "\n"); System.out.println("veggies LinkedCollection<String> all = fruits.combine(veggies); System.out.println("Combine all" +all.toString()); +veggies.toString()); } }
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.
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