Question: 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

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

}

}

To MAKE a Test Driver you can use this code:

package ch04.queues;

import java.util.*;

public class ITDArrayBoundedQueue

{

public static void main(String[] args)

{

QueueInterface test = new ArrayBoundedQueue();

Scanner scan = new Scanner(System.in);

String skip;// skip end of line after reading an integer

boolean keepGoing; // flag for "choose operation" loop

int constructor;// indicates user's choice of constructor

int operation;// indicates user's choice of operation

String enqueueString = "", dequeueString = "";// used by operations

// Handle test name

System.out.println("What is the name of this test?");

String testName = scan.nextLine();

System.out.println(" This is test " + testName + " ");

// Handle constructor

System.out.println("Choose a constructor:");

System.out.println("1: ArrayBoundedQueue( )");

System.out.println("2: ArrayBoundedQueue(int maxSize)");

if (scan.hasNextInt())

constructor = scan.nextInt();

else

{

System.out.println("Error: you must enter an integer.");

System.out.println("Terminating test.");

return;

}

skip = scan.nextLine();

switch (constructor)

{

case 1:

test = new ArrayBoundedQueue();

break;

case 2:

System.out.println("Enter a maximum size:");

int maxSize;

if (scan.hasNextInt())

maxSize = scan.nextInt();

else

{

System.out.println("Error: you must enter an integer.");

System.out.println("Terminating test.");

return;

}

skip = scan.nextLine();

test = new ArrayBoundedQueue(maxSize);

break;

default:

System.out.println("Error in constructor choice. Terminating test.");

return;

}

// Handle test cases

keepGoing = true;

while (keepGoing)

{

System.out.println(" Choose an operation:");

System.out.println("1: enqueue(element)");

System.out.println("2: String dequeue()");

System.out.println("3: boolean isFull()");

System.out.println("4: boolean isEmpty()");

System.out.println("5: int size()");

System.out.println("6: stop Testing");

if (scan.hasNextInt())

operation = scan.nextInt();

else

{

System.out.println("Error: you must enter an integer.");

System.out.println("Terminating test.");

return;

}

skip = scan.nextLine();

switch (operation)

{

case 1:// enqueue

System.out.println("Enter string to enqueue:");

enqueueString = scan.nextLine();

System.out.println("enqueue(\"" + enqueueString + "\")");

try

{

test.enqueue(enqueueString);

}

catch (QueueOverflowException QOFException)

{

System.out.println("Overflow Exception: " + QOFException.getMessage());

}

break;

case 2:// dequeue

System.out.println("dequeue()");

try

{

dequeueString = test.dequeue();

}

catch (QueueUnderflowException QUFException)

{

System.out.println("Underflow Exception: " + QUFException.getMessage());

break;

}

System.out.println("Result: " + dequeueString + " was returned.");

break;

case 3:// isFull

System.out.println("isFull()");

System.out.println("Result: " + test.isFull());

break;

case 4:// isEmpty

System.out.println("isEmpty()");

System.out.println("Result: " + test.isEmpty());

break;

case 5:// size

System.out.println("size()");

System.out.println("Result: " + test.size());

break;

case 6:// stop testing

keepGoing = false;

break;

default:

System.out.println("Error in operation choice. Terminating test.");

return;

}

}

System.out.println("End of Interactive Test Driver");

}

}

DO THE JAVA CODE FOR GIVEN QUESTION BELOW AND ALSO CREATE A TEST DRIVER.

package ch05.collections; import support.LLNode; public class LinkedCollection implements CollectionInterface{protected LLNode head;// head

\f

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!