-
-
Save mbakhoff/9f8aa032656c6acf7696a84cc749b02e to your computer and use it in GitHub Desktop.
SortedUniquePersonList template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ----------Person.java--------------- | |
public class Person { | |
private final int idCode; | |
private final String firstName; | |
private final String lastName; | |
public Person(int idCode, String firstName, String lastName) { | |
this.idCode = idCode; | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
public int getIdCode() { | |
return idCode; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
@Override | |
public String toString() { | |
return idCode + "-" + firstName + "-" + lastName; | |
} | |
} | |
// --------SortedUniquePersonList.java------------- | |
/** | |
* Collection for holding Person objects.<br> | |
* Provides the following guarantees:<br> | |
* 1) Elements are guaranteed to be in ascending order, sorted by their ID code value.<br> | |
* 2) Elements are guaranteed to have unique ID code values.<br> | |
* | |
* Uses an underlying array for storing the elements. <br> | |
* The object guarantees to not use more than twice the required array size.<br> | |
* For example, if currently 10 persons are stored, then the underlying array size might range from 10 to 20, but will not be larger. | |
*/ | |
public class SortedUniquePersonList { | |
/** | |
* Returns reference to object at the given index. Checks that the given index is in bounds of the underlying array, returns null if it isn't. | |
* @param index Index at which the object is searched. | |
* @return Person object at the given index, or null if the index is out of bounds. | |
*/ | |
public Person getElementAt(int index) { | |
} | |
/** | |
* Returns the index of the object with the given ID code. If an object with the given ID code is not present, returns -1. | |
* @param idCode ID code that is searched. | |
* @return Index at which the Person object with the given ID code can be found, or -1 if no such ID code is present. | |
*/ | |
public int indexOf(int idCode) { | |
} | |
/** | |
* Attempts to add the person to the collection, but only if no person with the same ID code is already present.<br> | |
* If an element is added, it is inserted to the correct position according to their ID code. Also, the index of all subsequent elements is then increased.<br> | |
* If a Person object with the same ID code is already present, does nothing. | |
* @param person Person object to be added. | |
* @return true if person was added to the collection, false otherwise. | |
*/ | |
public boolean add(Person person) { | |
} | |
/** | |
* Attempts to remove the person with the given ID code from the collection. Does nothing if no Person object with the given ID code is present.<br> | |
* In the case of a successful removal of an object, decreases the index of all subsequent elements. | |
* @param idCode ID code that is searched. | |
* @return true if the person with the given ID code was removed, false otherwise. | |
*/ | |
public boolean removeElement(int idCode) { | |
} | |
/** | |
* Calculates and returns the size of the collection. | |
* @return Number of elements in the collection. | |
*/ | |
public int size() { | |
} | |
} | |
// --------Test.java------------- | |
public class Test { | |
public static void main(String[] args) throws Exception { | |
Person p1 = new Person(1, "first1", "last1"); | |
Person p2 = new Person(2, "first2", "last2"); | |
Person p3 = new Person(3, "first3", "last3"); | |
Person pX = new Person(Integer.MAX_VALUE, "firstX", "lastX"); | |
SortedUniquePersonList supl = new SortedUniquePersonList(); | |
SortedUniquePersonList supl2 = new SortedUniquePersonList(); | |
assertThat("new list should have size 0", supl.size() == 0); | |
assertThat("new list should have size 0", supl2.size() == 0); | |
supl.add(pX); | |
supl.add(p3); | |
supl.add(p1); | |
supl.add(p2); | |
assertThat("size must be correct after add (supl)", supl.size() == 4); | |
assertThat("size must be correct after add (supl2)", supl2.size() == 0); | |
assertThat("elements must be sorted (p1)", supl.getElementAt(0) == p1); | |
assertThat("elements must be sorted (p2)", supl.getElementAt(1) == p2); | |
assertThat("elements must be sorted (p3)", supl.getElementAt(2) == p3); | |
assertThat("elements must be sorted (pX)", supl.getElementAt(3) == pX); | |
assertThat("getElementAt must be null (1000)", supl.getElementAt(1000) == null); | |
assertThat("getElementAt must be null (-1000)", supl.getElementAt(-1000) == null); | |
assertThat("indexOf must be -1 (1000)", supl.indexOf(1000) == -1); | |
assertThat("indexOf must be -1 (-1000)", supl.indexOf(-1000) == -1); | |
assertThat("indexOf must be correct (p1)", supl.indexOf(1) == 0); | |
assertThat("indexOf must be correct (pX)", supl.indexOf(Integer.MAX_VALUE) == 3); | |
supl.removeElement(Integer.MAX_VALUE); | |
supl.removeElement(1); | |
assertThat("size must be correct after remove", supl.size() == 2); | |
assertThat("elements must be sorted (p2)", supl.getElementAt(0) == p2); | |
assertThat("elements must be sorted (p3)", supl.getElementAt(1) == p3); | |
assertThat("indexOf must be correct (p2)", supl.indexOf(2) == 0); | |
assertThat("indexOf must be correct (p3)", supl.indexOf(3) == 1); | |
supl.removeElement(2); | |
supl.removeElement(3); | |
assertThat("size must be correct after remove all", supl.size() == 0); | |
supl.add(p1); | |
assertThat("size must be correct after re-add", supl.size() == 1); | |
System.out.println("tests passed"); | |
} | |
static void assertThat(String description, boolean isCorrect) throws Exception { | |
if (!isCorrect) | |
throw new Exception(description); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment