package ADTs;
public interface CollectionADT<T> {
public boolean isEmpty();
public int size();
@Override
public String toString();
}
**********************************************************
package ADTs;
import Exceptions.*;
/**
* An interface for an ordered (NOT SORTED) List
* Elements stay in the order they are put in to the list
* For use in Data Structures & Algorithms
*
*
* @author
*/
public interface ListADT<T> extends CollectionADT<T> {
/**
* Adds the specified element to the list at the front
*
* @param element: the element to be added
*
*/
public void addFirst(T element);
/**
* Adds the specified element to the end of the list
*
* @param element: the element to be added
*/
public void addLast(T element);
/**
* Adds the specified element to the list after the existing element
*
* @param existing: the element that is in the list already
* @param element: the element to be added
* @throws ElementNotFoundException if existing isn't in the list
*/
public void addAfter(T existing, T element) throws ElementNotFoundException,
EmptyCollectionException;
/**
* Removes and returns the specified element
*
* @return the element specified
* @throws EmptyCollectionException
* @throws ElementNotFoundException
*/
public T remove(T element) throws EmptyCollectionException, ElementNotFoundException;
/**
* Removes and returns the first element
*
* @return the first element in the list
* @throws EmptyCollectionException
*/
public T removeFirst() throws EmptyCollectionException;
/**
* Removes and returns the last element
*
* @return the last element in the list
* @throws EmptyCollectionException
*/
public T removeLast() throws EmptyCollectionException;
/**
* Returns (without removing) the first element in the list
*
* @return element at the beginning of the list
* @throws EmptyCollectionException
*/
public T first() throws EmptyCollectionException;
/**
* Returns (without removing) the last element in the list
*
* @return element at the end of the list
* @throws EmptyCollectionException
*/
public T last() throws EmptyCollectionException;
/**
* Return whether the list contains the given element.
*
* @param element
* @return
* @throws EmptyCollectionException
*/
public boolean contains(T element) throws EmptyCollectionException;
/**
* Returns the index of the given element.
*
* @param element
* @return the index of the element, or -1 if not found
*/
public int indexOf(T element);
/**
* Return the element at the given index of a list.
*
* @param element
* @return
* @throws EmptyCollectionException
*/
public T get(int index) throws EmptyCollectionException, InvalidArgumentException;
/**
* Set the at the given index of a list.
*
* @param element
* @return
* @throws EmptyCollectionException
*/
public void set(int index, T element) throws EmptyCollectionException, InvalidArgumentException;
}
**********************************************
package ADTs;
import Exceptions.EmptyCollectionException;
import Exceptions.QueueOverflowException;
public interface QueueADT<T> extends CollectionADT<T> {
public void enqueue(T element) throws QueueOverflowException;
public T dequeue() throws EmptyCollectionException;
public T peek() throws EmptyCollectionException;
}
***** Work below please******
package DataStructures;
import ADTs.*;
import Exceptions.*;
public class ArrayQueue {
}
***********************************************************
package DataStructures;
import Exceptions.*;
import org.junit.Test;
import static org.junit.Assert.*;
public class ArrayQueueTest {
ArrayQueue<Integer> list = new ArrayQueue<Integer>(10);
@Test
public void testArrayQueue() {
// LinkedList
assertEquals(0, list.size());
list = new ArrayQueue<Integer>();
assertEquals(0, list.size());
}
@Test
public void testIsEmpty() {
assertTrue(list.isEmpty());
}
@Test
public void testSize() {
assertEquals(0, list.size());
try {
list.enqueue(1);
assertEquals(1, list.size());
list.enqueue(2);
assertEquals(2, list.size());
} catch (Exception e) {
fail("Shoud not throw exception here: " + e.toString());
}
try {
list.dequeue();
assertEquals(1, list.size());
list.dequeue();
assertEquals(0, list.size());
} catch (EmptyCollectionException e) {
fail("EmptyCollectionException should not be thrown here.");
}
}
@Test
public void testEnqueue() {
try {
list.enqueue(1);
} catch (Exception e) {
fail("Exception should not be thrown here: " + e.toString());
}
assertEquals(1, list.size());
try {
assertEquals(1, list.peek().intValue());
} catch (Exception e) {
fail("Exception should not be thrown here.");
}
try {
list.enqueue(2);
} catch (Exception e) {
fail("Exception should not be thrown here: " + e.toString());
}
assertEquals(2, list.size());
try {
assertEquals(1, list.peek().intValue());
} catch (Exception e) {
fail("Exception should not be thrown here: " + e.toString());
}
}
@Test
public void testDequeue() {
try {
list.enqueue(1);
list.enqueue(2);
list.enqueue(3);
} catch (Exception e) {
fail("Exception should not be thrown here: " + e.toString());
}
try {
assertEquals(3, list.size());
Integer out = list.dequeue();
assertEquals(1, out.intValue());
assertEquals(2, list.size());
} catch (Exception e) {
fail("Exception should not be thrown here.");
}
try {
assertEquals(2, list.size());
Integer out = list.dequeue();
assertEquals(2, out.intValue());
assertEquals(1, list.size());
} catch (Exception e) {
fail("Exception should not be thrown here.");
}
try {
Integer out = list.dequeue();
assertEquals(3, out.intValue());
assertEquals(0, list.size());
} catch (Exception e) {
fail("Exception should not be thrown here.");
}
try {
list.dequeue();
} catch (Exception e) {
assertTrue("EmptyCollectionException should be thrown here.", e instanceof
EmptyCollectionException);
}
}
@Test
public void testPeek() {
try {
list.enqueue(1);
} catch (Exception e) {
fail("Exception should not be thrown here: " + e.toString());
}
try {
assertEquals(1, list.size());
assertEquals(1, list.peek().intValue());
} catch (Exception e) {
fail("Exception should not be thrown here.");
}
try {
list.enqueue(2);
} catch (Exception e) {
fail("Exception should not be thrown here: " + e.toString());
}
try {
assertEquals(2, list.size());
assertEquals(1, list.peek().intValue());
list.dequeue();
assertEquals(2, list.peek().intValue());
} catch (Exception e) {
fail("Exception should not be thrown here.");
}
try {
list.dequeue();
} catch (Exception e) {
fail("Exception should not be thrown here: " + e.getMessage());
}
try {
list.peek();
} catch (Exception e) {
assertTrue("EmptyCollectionException should be thrown here.", e instanceof
EmptyCollectionException);
} }}
These are the files in the project -

package ADTs public interface CollectionADTltTgt .pdf

  • 1.
    package ADTs; public interfaceCollectionADT<T> { public boolean isEmpty(); public int size(); @Override public String toString(); } ********************************************************** package ADTs; import Exceptions.*; /** * An interface for an ordered (NOT SORTED) List * Elements stay in the order they are put in to the list * For use in Data Structures & Algorithms * * * @author */ public interface ListADT<T> extends CollectionADT<T> { /** * Adds the specified element to the list at the front * * @param element: the element to be added * */ public void addFirst(T element); /** * Adds the specified element to the end of the list * * @param element: the element to be added */ public void addLast(T element); /** * Adds the specified element to the list after the existing element * * @param existing: the element that is in the list already * @param element: the element to be added * @throws ElementNotFoundException if existing isn't in the list */ public void addAfter(T existing, T element) throws ElementNotFoundException, EmptyCollectionException; /**
  • 2.
    * Removes andreturns the specified element * * @return the element specified * @throws EmptyCollectionException * @throws ElementNotFoundException */ public T remove(T element) throws EmptyCollectionException, ElementNotFoundException; /** * Removes and returns the first element * * @return the first element in the list * @throws EmptyCollectionException */ public T removeFirst() throws EmptyCollectionException; /** * Removes and returns the last element * * @return the last element in the list * @throws EmptyCollectionException */ public T removeLast() throws EmptyCollectionException; /** * Returns (without removing) the first element in the list * * @return element at the beginning of the list * @throws EmptyCollectionException */ public T first() throws EmptyCollectionException; /** * Returns (without removing) the last element in the list * * @return element at the end of the list * @throws EmptyCollectionException */ public T last() throws EmptyCollectionException; /** * Return whether the list contains the given element. * * @param element * @return * @throws EmptyCollectionException */
  • 3.
    public boolean contains(Telement) throws EmptyCollectionException; /** * Returns the index of the given element. * * @param element * @return the index of the element, or -1 if not found */ public int indexOf(T element); /** * Return the element at the given index of a list. * * @param element * @return * @throws EmptyCollectionException */ public T get(int index) throws EmptyCollectionException, InvalidArgumentException; /** * Set the at the given index of a list. * * @param element * @return * @throws EmptyCollectionException */ public void set(int index, T element) throws EmptyCollectionException, InvalidArgumentException; } ********************************************** package ADTs; import Exceptions.EmptyCollectionException; import Exceptions.QueueOverflowException; public interface QueueADT<T> extends CollectionADT<T> { public void enqueue(T element) throws QueueOverflowException; public T dequeue() throws EmptyCollectionException; public T peek() throws EmptyCollectionException; } ***** Work below please****** package DataStructures; import ADTs.*; import Exceptions.*; public class ArrayQueue { } *********************************************************** package DataStructures;
  • 4.
    import Exceptions.*; import org.junit.Test; importstatic org.junit.Assert.*; public class ArrayQueueTest { ArrayQueue<Integer> list = new ArrayQueue<Integer>(10); @Test public void testArrayQueue() { // LinkedList assertEquals(0, list.size()); list = new ArrayQueue<Integer>(); assertEquals(0, list.size()); } @Test public void testIsEmpty() { assertTrue(list.isEmpty()); } @Test public void testSize() { assertEquals(0, list.size()); try { list.enqueue(1); assertEquals(1, list.size()); list.enqueue(2); assertEquals(2, list.size()); } catch (Exception e) { fail("Shoud not throw exception here: " + e.toString()); } try { list.dequeue(); assertEquals(1, list.size()); list.dequeue(); assertEquals(0, list.size()); } catch (EmptyCollectionException e) { fail("EmptyCollectionException should not be thrown here."); } } @Test public void testEnqueue() { try { list.enqueue(1); } catch (Exception e) { fail("Exception should not be thrown here: " + e.toString());
  • 5.
    } assertEquals(1, list.size()); try { assertEquals(1,list.peek().intValue()); } catch (Exception e) { fail("Exception should not be thrown here."); } try { list.enqueue(2); } catch (Exception e) { fail("Exception should not be thrown here: " + e.toString()); } assertEquals(2, list.size()); try { assertEquals(1, list.peek().intValue()); } catch (Exception e) { fail("Exception should not be thrown here: " + e.toString()); } } @Test public void testDequeue() { try { list.enqueue(1); list.enqueue(2); list.enqueue(3); } catch (Exception e) { fail("Exception should not be thrown here: " + e.toString()); } try { assertEquals(3, list.size()); Integer out = list.dequeue(); assertEquals(1, out.intValue()); assertEquals(2, list.size()); } catch (Exception e) { fail("Exception should not be thrown here."); } try { assertEquals(2, list.size()); Integer out = list.dequeue(); assertEquals(2, out.intValue()); assertEquals(1, list.size()); } catch (Exception e) {
  • 6.
    fail("Exception should notbe thrown here."); } try { Integer out = list.dequeue(); assertEquals(3, out.intValue()); assertEquals(0, list.size()); } catch (Exception e) { fail("Exception should not be thrown here."); } try { list.dequeue(); } catch (Exception e) { assertTrue("EmptyCollectionException should be thrown here.", e instanceof EmptyCollectionException); } } @Test public void testPeek() { try { list.enqueue(1); } catch (Exception e) { fail("Exception should not be thrown here: " + e.toString()); } try { assertEquals(1, list.size()); assertEquals(1, list.peek().intValue()); } catch (Exception e) { fail("Exception should not be thrown here."); } try { list.enqueue(2); } catch (Exception e) { fail("Exception should not be thrown here: " + e.toString()); } try { assertEquals(2, list.size()); assertEquals(1, list.peek().intValue()); list.dequeue(); assertEquals(2, list.peek().intValue()); } catch (Exception e) { fail("Exception should not be thrown here."); }
  • 7.
    try { list.dequeue(); } catch(Exception e) { fail("Exception should not be thrown here: " + e.getMessage()); } try { list.peek(); } catch (Exception e) { assertTrue("EmptyCollectionException should be thrown here.", e instanceof EmptyCollectionException); } }} These are the files in the project -