Programming Assignment: ArrayPriorityList<E>

Collaboration Solo: Complete this by yourself with help from section leaders and Rick and code from our book, lectures, and section. Note: It could be difficult to not get 100% code coverage in unit tests due to a change to WebCat's code coverage tool or you forget to test a method throws an exception.

Preview: This project asks you to implement a collection class ArrayPriorityList<E> using an array instance variable. This project has the following goals:

  • Implement a collection class using an array data structure
  • Understand how generic classes store collections of the same type element safely
  • Use exceptions to handle invalid arguments

This new type will store a collection of elements as a zero-based indexed list where the element at index 0 is considered to have higher priority than the element at index 1. The element at index size()-1 has the lowest priority. An instance of this collection class will be able to store just one type of element such asString>. Begin with a starter project that has all files you will need with the build path set to find JUnit (should have no errors once imported as an existing Java project):

  • Download this file to a place you can easily find in the minute or so
  • From Eclipse, select File > Import > General > Existing Projects into Workspace > Next
  • Click the radio button to the left of Select archive file and click the Browse button to the right
  • Browse to the file ArrayPriorityListStart.zip that you just downloaded
  • Click the Finish button. You should see these three files in Eclipse:

  1. ArrayPriorityList.java A Java Collection class that implements the methods of PriorityList<E>
  2. ArrayPriorityListTest.java Small part of a unit test
  3. PriorityList.java The ADT stored as a Java interface
/

classArrayPriorityList<E>

Complete the methods in ArrayPriorityList<E> so it uses a 1D array instance variable to store elements. Since you cannot have an array of E, the type of array elements will be Object to allow this class to store any type of element, which requires a cast in the get method.It begins likethis:

/**

*Thisclass implements a generic collectiontostoreelementswhere

*indexesrepresentprioritiesandtheprioritiescanchangeinseveralways.

*

*@authorYour Name

*@param<E> Thetypeofallelementsstoredinthiscollection

*/

publicclass ArrayPriorityList<E> implements PriorityList<E> {

private Object[] data; // The data structure storing elements

privateintn; // The number of meaningful elements

// Create an empty list with zero elements

public ArrayPriorityList() {

data = new Object[20];

n = 0;

}

// ...Complete the methods not shown here to save space . . .

}

interfacePriorityList<E>

Complete the following methods, one at a time, inside ArrayPriorityList<E>. The specifications for each method are provided by the comments that precede each method heading in both the interface and the class with method stubs.

/**

* This interface describes an abstract data type to store elements where

* indexes represent priorities and the priorities can change in several ways.

*

* @author Your Name

* @param<E>The type of all elements stored in this collection

*/

publicinterface PriorityList<E> {

/**

* Return the number of elements currently in this PriorityList

*

* @return The number of elements in this PriorityList

*/

publicint size();

/**

* Return true if there are zero elements in this PriorityList *

*

* @return true if size() == 0 or false if size() > 0

*/

publicboolean isEmpty();

/**

* If possible, insert the element at the given index. If index is out of

* range, throw new IllegalArgumentException();. For example, when size is 3,

* the only possible values for index are 0, 1, 2, and 3.

*

* @param index

* The index of the element to move.

* @param el

* The element to insert

* @throws IllegalArgumentException

*/

publicvoid insertElementAt(int index, E el) throws IllegalArgumentException;

/**

* If possible, return a reference to the element at the given index. If index

* is out of range, throw new IllegalArgumentException(); When size is 3, the

* only possible values for index are 0, 1, and 2.

*

* @param index

* The index of the element to move.

* @return A reference to to element at index index.

* @throws IllegalArgumentException

*/

public E getElementAt(int index) throws IllegalArgumentException;

/**

* If possible, remove the element at the given index. If index is out of

* range, throw new IllegalArgumentException();

*

* @param index

* The index of the element to move.

* @throws IllegalArgumentException

*/

publicvoid removeElementAt(int index) throws IllegalArgumentException;

/**

* If possible, swap the element located at index with the element at index+1.

* An attempt to lower the priority of the element at index size()-1 has no

* effect. If index is out of range, throw new IllegalArgumentException();

*

* @param index

* The index of the element to move

* @throws IllegalArgumentException

*/

publicvoid lowerPriorityOf(int index) throws IllegalArgumentException;

/**

* If possible, swap the element located at index with the element at index-1.

* An attempt to raise the priority at index 0 has no effect. If index is out

* of range, throw new IllegalArgumentException();

*

* @param index

* The index of the element to move

* @throws IllegalArgumentException

*/

publicvoid raisePriorityOf(int index) throws IllegalArgumentException;

/**

* Return a copy of all elements as an array of Objects that is the size of this

* PriorityList and in the same order. Do not return the instance variable.

* Rick has a test to ensure you clone the array to prevent accidental change

* from the outside If there are no elements in this list, return new Object[0];.

* A change to the return value must not affectthis ArrayPriorityList object.

*

* @return An array of Objects where capacity == size()

*/

public Object[] toArray();

/**

* If possible, move the element at the given index to the end of this list.

* An attempt to move the last element to the last has no effect. If the index

* is out of range, throw new IllegalArgumentException();

*

* @param index

* The index of the element to move.

* @throws IllegalArgumentException

*/

publicvoid moveToLast(int index) throws IllegalArgumentException;

/**

* If possible, move the element at the given index to the front of this list.

* An attempt to move the top element to the top has no effect. If the index

* is out of range, throw new IllegalArgumentException();

*

* @param index

* The index of the element to move.

* @throws IllegalArgumentException

*/

publicvoid moveToTop(int index) throws IllegalArgumentException;

}

classArrayPriorityListTest

The following start to a unit test show two cases that students often forget about. Inserting at index 0 on a non-empty list and ensuring a method throws an exception when it is supposed to.

importstatic org.junit.Assert.*;

import org.junit.Test;

publicclass ArrayPriorityListTest {

@Test

publicvoid testInsertToLeft() {

ArrayPriorityList<String> list = new ArrayPriorityList<String>();

list.insertElementAt(0, "First");

// Must shift array elements in this case

list.insertElementAt(0, "New First");

assertEquals("New First", list.getElementAt(0));

assertEquals("First", list.getElementAt(1));

}

// Write short test methods to ensure methods throw exceptions

// when they are supposed to throw new IllegalArgumentException();

@Test(expected = IllegalArgumentException.class)

publicvoid testExceptionGetElementAtZeroWhenSizeIsZero() {

ArrayPriorityList<String> list = new ArrayPriorityList<String>();

list.getElementAt(0);

}

}

Turn in to Web-Cat

Submit this to Project ArrayPriorityList<E> and work with it until you have 100% code coverage and 100% problem coverage.

Grading Criteria (100 points max)

____ / +100 Web-Cat correctness and code coverage: To get 100% for these 100 points, you will need 100% problem coverage only, which means Rick's tests pass and you exercised all methods). The new code coverage tool on WebCat (EclEmma) may not cover all assertions, but this does not count against you. Look for red lines only in ArrayPriorityList.java