ArrayPriorityList



Programming Assignment: ArrayPriorityList

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 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 as . 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:

|ArrayPriorityList.java A Java Collection class that implements the methods of |[pic] |

|PriorityList | |

|ArrayPriorityListTest.java Small part of a unit test | |

|PriorityList.java The ADT stored as a Java interface | |

class ArrayPriorityList

Complete the methods in ArrayPriorityList 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 like this:

/**

* 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 The type of all elements stored in this collection

*/

public class ArrayPriorityList implements PriorityList {

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. 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 The type of all elements stored in this collection

*/

public interface PriorityList {

/**

* 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 list = new ArrayPriorityList();

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)

public void testExceptionGetElementAtZeroWhenSizeIsZero() {

ArrayPriorityList list = new ArrayPriorityList();

list.getElementAt(0);

}

}

Turn in to Web-Cat

Submit this to Project ArrayPriorityList 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

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches