ProjectSortableList<E>
Collaboration: Complete by yourself with help from section leaders and Rick
Preview: This project asks you to implement a collection class SortableList<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
·Implement and understand the insertion sort algorithm plus some other array processing algorithms
Begin a new Java project in Eclipse and get these three files into the src folder. There should be no compile-time errors and the test methods should not pass:
- SortableList.java
- SortableListTest.java
SortableList<E>
/**
* @author [YOUR NAME]
*
* @param<E>
* The generic argument that states what type of data this SortableList
* will hold.
*/
publicclass SortableList<E extends Comparable<E> {
private Object[] elements;
privatebooleanisSorted;
privateintsize;
/**
* Create a new Object array of capacity 20 (MUST BE 20), set the size to
* zero, and set isSorted to false;
*/
public SortableList() {
}
/**
* Adds the passed element at index size (at the end of the list) to the
* array. Will grow the array if necessary.
*
* If adding the next value ruins the sort the array, set isSorted to false
*
* @param data
* The element being added at the 'end' of this SortableList, at
* index size()
*/
publicvoid add(E data) {
}
/**
* Returns the data at the given index.
*
* @param indexThe location of the element.
* @return The element at the given index or null if there isn't data at the
* given index
* Precondition: 0 <= index <= size()
*/
public E get(int index) {
returnnull;
}
/**
* Removes the first occurrence of the passed data from this Sortable List and
* returns true. If the passed data isn't in this Sortable List, return false.
*
* All data after the removed element should be shifted to the left. For
* example: [1, 2, 3, 4, 5, 6, 7, 8] will become this after
* removing 4: [1, 2, 3, 5, 6, 7, 8]
*
* @param data
* The element that equals data will be removed if ound
* @return true if the data was removed, false otherwise.
*/
publicboolean remove(E data) {
returnfalse;
}
/**
* Return the number of elements currently contained in this Sortable List.
*
* @return the number of elements that have been added so far
*/
publicint size() {
return 0;
}
/**
* Uses the Insertion Sort algorithm to put all data in ascending order
* (smallest to largest)
*
* Only sort the data if it's not currently sorted.
*/
publicvoid sort() {
return;
}
/**
* Reverses the elements in the array. The first element becomes last, the second
* element becomes second last, and so on.
*/
publicvoid reverse() {
return;
}
/**
* returns a String that starts with a bracket, then has elements separated by
* commas and spaces. It should also end with a closing bracket.
*
* For example, after adding 10, 54, 7 this SortableList toString() should return:[10, 54, 7]
* If we added "Lane", "Tyson", "Rick", and "Cody" it should return[Lane, Tyson, Rick, Cody]
* An empty list should return "[]"
*/
public String toString() {
return"toString will actually be tested this time!";
}
}
SortableListTest
The following start to a unit test SortableListTest.javashows how to instantiate generic collections and use at least three of the required methods. You must add many tests to get 100% code coverage and a correct implantation.
importstatic org.junit.Assert.assertEquals;
import org.junit.Test;
publicclass SortableListTest {
@Test
publicvoid testAddSizeAndGet() {
SortableList<Integer> ints = new SortableList<Integer>();
SortableList<String> names = new SortableList<String>();
// Add a few elements to ints
assertEquals(0, ints.size());
ints.add(79);
ints.add(92);
ints.add(87);
assertEquals(3, ints.size());
// The casts is needed for JUnit's assertEquals method
assertEquals(79, (int) ints.get(0));
assertEquals(92, (int) ints.get(1));
assertEquals(87, (int) ints.get(2));
// Add a few elements to name
assertEquals(0, names.size());
names.add("Cody");
names.add("Tyson");
names.add("Lane");
assertEquals(3, names.size());
assertEquals("Cody", names.get(0));
assertEquals("Tyson", names.get(1));
assertEquals("Lane", names.get(2));
}
// Add MANY more tests as you implement one method at a time.
}
Submit to Web-Cat
Submit this to Project SortableList and work with it until you have 100% code coverage and 100% problem coverage.
Grading Criteria (100 points, subject to change)
____ / +100 Web-Cat correctness and code coverage: To get 100% for these 80 points, you will need 100% code coverage and 100% problem coverage (Rick's tests pass and you exercised all methods). These 100 points are derived from Web-Cat. You may submit as many times as you wish until you have 100% on both. Notice that a multiplication feature is employed that means 90% code coverage and 90% problem coverage results in 0.9 * 0.9 * 100 81/100 points. This is only 81% rather than 90%.
_____/ -80 If you did not use a one-dimensional array instance variable in SortableList<E>. -80 for any other scenario such as using an existing Java collection class or a linked structure as your data structure.
____/ -20 If you did not implement the Insertion Sort algorithm.
____/ -20 If you did not implement your own reverse algorithm.