Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[Java] Please test your code in the link I provide before you post your answer.

ID: 3839679 • Letter: #

Question

[Java] Please test your code in the link I provide before you post your answer.

The output should be looked like exact same as the tester.

http://www.codecheck.it/files/17050415451csldwjahxt2kwn73ahd6vukt

Thank you.

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 exception

public void undoAll() removes all items from the history.

Use the following file:

**********************************************

UndoStackRunner.java

**********************************************

Explanation / Answer

import java.util.*;
public class UndoStack {
private Stack st;
UndoStack(){
st = new Stack(); //Creating empty stack
}
public void add( String a) {
st.push(new String(a)); //adding element to stack

}
public String undo() {
if(!st.empty()) {
String a = (String) st.pop(); // returns the string that has been removed or undo
return a;
}
return null;

}

public void undoAll(){
while(!st.empty())
st.pop(); //deleting all elements in the stack
}
}

In this we have used data structure Stack. Because in undo we follow LIFO approach. In this program also we are doing same and Stack inbuild method pop and push helps us to make it more convinient.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote