Remember not to create packages. Use the default package
A
Write a simplified application to illustrate the use of an undo button for a word processor. It keeps a history of all items and allows the user to undo the last.
Write a class UndoStack
.
Implement using a Stack
of Strings. The constructor will create an empty Stack
to keep the history.
A UndoStack
has these methods:
public void add(String phrase)
adds the phase to the history of the UndoStack
public String undo()
removes the last thing added or null if there is nothing in the history to undo. Note that this method violates the rule that a method is not both a mutator and an accessor. Also trying to remove an item form an empty stack causes an exceptionpublic void undoAll()
removes all items from the history
B
In this problem you will do a better implementation of the to do list. Write a class ToDoList
that maintains a priority queue to manage the items to be done.
Write a class ToDoItem
. ToDoItem
has a description and a priority. The constructor takes these as parameters in that order.
ToDoItem
implements Comparable interface and has these methods
get
methods(accessors) for priority and descriptionequals
method. Two items are equal if and only if they have both the same priority and descriptioncompareTo
method orders by priority. If priorities are equal, order by description. toString
returns a String representation of the ToDoItem in this format:
ToDoItem[description=see friends,priority=1]
Write the class ToDoList
. Its constructor initializes an empty PriorityQueue.
ToDoList
has these methods
public void add(ToDoItem item)
Adds an item for this ToDoListpublic ToDoItem nextItem()
removes and returns the next item to do. (The one with the priority closest to 1). Note that this also violates the rule about mutators not returning valuespublic boolean hasNext()
returns true if there is at least one item left to do otherwise falsepublic ToDoItem peek()
returns the next item to do but does not remove it from the list