A counter changes over time, but a user may want to mark several points and undo back through them. Each saved point should be an object representing the counter's state, while the history only stores and returns those objects.
The starter code provides UndoableCounter, the coordinator used by the tests. Implement only its three Memento participants:
CounterMemento, the memento that stores one saved value.Counter, the originator that owns the live value and creates and restores mementos.CounterHistory, the caretaker that stores mementos without reading their contents.The provided coordinator behaves as follows:
UndoableCounter(int start) creates a counter with the supplied starting value and empty history.int add(int amount) adds the amount, which may be negative, and returns the new value.int save() records the current value as a memento and returns the new history size.boolean undo() removes the newest memento, restores it, and returns true. Empty history changes nothing and returns false.int value() returns the current counter value.int historySize() returns how many saved points remain.int clearHistory() removes every saved point and returns how many were removed. It does not change the current counter.Do not rewrite UndoableCounter. Make the three participants satisfy the API it already uses.
Input:
Output:
Explanation: The saved memento holds 10. Both additions change only the live counter, and undo restores 10 before removing that memento from history.
Input:
Output:
Explanation: Each save records a separate point. Undo restores the newest one first, then the older one; after both are consumed, another undo has nothing to restore.
-100000 <= start <= 100000-100000 <= amount <= 100000100 calls are made across all methods.UndoableCounter is complete in every language below. Implement only CounterMemento, Counter, and CounterHistory.
Full marks when `Counter` creates `CounterMemento` and is the only participant that uses the stored value to restore state. Lose points heavily when `CounterHistory` stores raw integers instead of memento objects.
Full marks when CounterHistory stores CounterMemento objects, returns the newest one without inspecting it, and clearHistory only removes saved objects. Lose points when the caretaker performs counter calculations or restoration.
Full marks when `CounterHistory` pops the newest memento, returns no memento when empty, reports its size, and clears all saved objects while returning the number removed, while `Counter` adds positive or negative amounts correctly. Lose points for FIFO history or printing to stdout.
Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.
| Call | Returns |
|---|---|
| new UndoableCounter(10) | null |
| save() | 1 |
| add(5) | 15 |
| add(-3) | 12 |
| value() | 12 |
| undo() | true |
| value() | 10 |
| historySize() | 0 |
The saved memento holds 10. Both additions change only the live counter, and undo restores 10 before removing that memento from history.
Run checks these cases. Submit also runs a larger hidden set.

