Complete the code to declare the Command interface method for undo functionality.
interface Command {
void execute();
void [1]();
}The Command interface must declare an undo method to support undo operations.
Complete the code to add a command to the undo stack after execution.
public class Invoker { private Stack<Command> undoStack = new Stack<>(); public void executeCommand(Command cmd) { cmd.execute(); undoStack.[1](cmd); } }
After executing a command, it should be pushed onto the undo stack to enable undoing later.
Fix the error in the undo method to correctly undo the last command.
public void undo() {
if (!undoStack.isEmpty()) {
Command cmd = undoStack.[1]();
cmd.undo();
}
}To undo the last command, it must be popped from the undo stack before calling undo.
Fill both blanks to implement redo functionality using a redo stack.
public class Invoker { private Stack<Command> undoStack = new Stack<>(); private Stack<Command> redoStack = new Stack<>(); public void undo() { if (!undoStack.isEmpty()) { Command cmd = undoStack.[1](); cmd.undo(); redoStack.[2](cmd); } } }
Undo removes the last command from undoStack using pop, then adds it to redoStack using push for redo support.
Fill all three blanks to implement redo method that re-executes and updates stacks correctly.
public void redo() {
if (!redoStack.isEmpty()) {
Command cmd = redoStack.[1]();
cmd.[2]();
undoStack.[3](cmd);
}
}Redo removes the last command from redoStack with pop, executes it with execute, then pushes it back to undoStack with push.
