Programming Assignment: Arrayprioritylist<E>
Total Page:16
File Type:pdf, Size:1020Kb

Programming Assignment: ArrayPriorityList
1. ArrayPriorityList.java A Java Collection class that implements the methods of PriorityList
Complete the methods in ArrayPriorityList
/** * This class implements a generic collection to store elements where * indexes represent priorities and the priorities can change in several ways. * * @author Your Name * @param
private Object[] data; // The data structure storing elements private int n; // 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 . . . } interface PriorityList
Complete the following methods, one at a time, inside ArrayPriorityList
/** * 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
/** * Return the number of elements currently in this PriorityList * * @return The number of elements in this PriorityList */ public int size();
/** * Return true if there are zero elements in this PriorityList * * * @return true if size() == 0 or false if size() > 0 */ public boolean 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 */ public void 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 */ public void 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 */ public void 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 */ public void 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 affect this 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 */ public void 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 */ public void moveToTop(int index) throws IllegalArgumentException; } class ArrayPriorityListTest
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. import static org.junit.Assert.*; import org.junit.Test; public class ArrayPriorityListTest {
@Test public void testInsertToLeft() { ArrayPriorityList
// Write short test methods to ensure methods throw exceptions // when they are supposed to throw new IllegalArgumentException(); @Test(expected = IllegalArgumentException.class) public void testExceptionGetElementAtZeroWhenSizeIsZero() { ArrayPriorityList
Turn in to Web-Cat Submit this to Project ArrayPriorityList
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