You have been asked to extend the ArrayIntList
class that we have been studying. Recall that it includes the following public constructors and methods:
Method/Constructor | Description |
public ArrayIntList() | constructs an empty list with default capacity |
public ArrayIntList(int capacity) | constructs an empty list with the given capacity |
public void add(int value) | adds given value to end of list |
public void add(int index, int value) | adds given value at given index |
public boolean contains(int value) | returns whether value occurs anywhere in the list |
public int get(int index) | returns value at given index |
public int indexOf(int value) | index of first occurrence of value; -1 if not found |
public boolean isEmpty() | returns whether list contains no elements |
public void remove(int index) | removes the value at the given index |
public void set(int index, int value) | sets element at given index to store given value |
public int size() | returns number of elements in list |
public String toString() | returns comma-separated string version of list |
You are to define a new class called HistoryList
that extends this class through inheritance. It should behave like an ArrayIntList
except that it should keep track of the history of modifications to the list. This history is stored as a sequence of String values indicating the list's state at each point. For example, if the operations below at left are performed, then the HistoryList
object should keep track of the sequence of strings shown below at right:
Operations | History Entries |
HistoryList list = new HistoryList();
list.add(18);
list.add(27);
list.add(0, 45);
list.remove(1);
list.set(0, -15);
list.add(9);
|
"[]"
"[18]"
"[18, 27]"
"[45, 18, 27]"
"[45, 27]"
"[-15, 27]"
"[-15, 27, 9]"
|
When a HistoryList
is constructed, the first string in the list above should be added to its history. Each time any of methods add, remove, or set are called, a new entry is added to the history showing the state of the list after the call. You must exactly reproduce the format shown above.
These strings that are part of the history will be accessed by clients using the following new methods:
Method/Constructor | Description |
public int historySize() | returns number of strings in history |
public String getHistory(int index) | returns given history string (0=first, 1=second, etc.) |
In the example above, after executing the sample code, the history size will be 6 and the six different history strings can be accessed by calls on getHistory
passing indexes between 0 and 5. For example:
for (int i = 0; i < list.historySize(); i++) {
System.out.println(list.history(i));
}
You must also make HistoryList
objects comparable to each other using the Comparable
interface. A HistoryList
with more strings in its history is considered to be "greater than" one with fewer history entries. If two HistoryList
s have the same number of history entries, the one that contains more elements (the one with the greater size) is considered to be greater. If the two lists have the same number of history entries and the same size, they are considered to be "equal" for this problem.