Question
package ch05.collections; public class ArrayCollection implements CollectionInterface { protected final int DEFCAP = 100; // default capacity protected T[] elements;// array to hold collection's elements
package ch05.collections;
public class ArrayCollection
{
protected final int DEFCAP = 100; // default capacity
protected T[] elements;// array to hold collection's elements
protected int numElements = 0;// number of elements in this collection
// set by find method
protected boolean found;// true if target found, otherwise false
protected int location;// indicates location of target if found
public ArrayCollection()
{
elements = (T[]) new Object[DEFCAP];
}
public ArrayCollection(int capacity)
{
elements = (T[]) new Object[capacity];
}
protected void find(T target)
// Searches elements for an occurrence of an element e such that
// e.equals(target). If successful, sets instance variables
// found to true and location to the array index of e. If
// not successful, sets found to false.
{
location = 0;
found = false;
while (location
{
if (elements[location].equals(target))
{
found = true;
return;
}
else
location++;
}
}
public boolean add(T element)
// Attempts to add element to this collection.
// Returns true if successful, false otherwise.
{
if (isFull())
return false;
else
{
elements[numElements] = element;
numElements++;
return true;
}
}
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)
{
elements[location] = elements[numElements - 1];
elements[numElements - 1] = null;
numElements--;
}
return found;
}
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 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 elements[location];
else
return null;
}
public boolean isFull()
// Returns true if this collection is full; otherwise, returns false.
{
return (numElements == elements.length);
}
public boolean isEmpty()
// Returns true if this collection is empty; otherwise, returns false.
{
return (numElements == 0);
}
public int size()
// Returns the number of elements in this collection.
{
return numElements;
}
}
package ch05.collections;
public interface CollectionInterface
{
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)
// and 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.
}
TO TEST THE CODE YOU CAN USE THIS METHOD:
package ch04.queues;
import java.util.*;
public class ITDArrayBoundedQueue
{
public static void main(String[] args)
{
QueueInterface
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
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 QUESTION GIVEN BELOW AND ALSO PROVIDE TEST CODE TO CHECK THE METHOD. SO IT WORKS CORRECTLY
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started