Programming Assignment: Arrayprioritylist<E>

Programming Assignment: Arrayprioritylist<E>

<p>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 as <String>. 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 http://www.cs.arizona.edu/~mercer/Projects/ArrayPriorityListStart.zip  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:</p><p>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 class ArrayPriorityList<E> </p><p>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 like this:</p><p>/** * 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 <E> The type of all elements stored in this collection */ public class ArrayPriorityList<E> implements PriorityList<E> {</p><p> private Object[] data; // The data structure storing elements private int n; // The number of meaningful elements </p><p>// Create an empty list with zero elements public ArrayPriorityList() { data = new Object[20]; n = 0; }</p><p>// . . . Complete the methods not shown here to save space . . . } interface PriorityList<E></p><p>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. </p><p>/** * 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 */ public interface PriorityList<E> {</p><p>/** * Return the number of elements currently in this PriorityList * * @return The number of elements in this PriorityList */ public int size();</p><p>/** * Return true if there are zero elements in this PriorityList * * * @return true if size() == 0 or false if size() > 0 */ public boolean isEmpty();</p><p>/** * 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;</p><p>/** * 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;</p><p>/** * 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;</p><p>/** * 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;</p><p>/** * 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();</p><p>/** * 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;</p><p>/** * 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 </p><p>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 {</p><p>@Test public void 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)); }</p><p>// 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<String> list = new ArrayPriorityList<String>(); list.getElementAt(0); } }</p><p>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.</p><p>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</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us