0% found this document useful (0 votes)
18 views5 pages

Java Undo/Redo Feature Simulation

The document presents a Java program that simulates an undo/redo feature for a text editor using two stacks. Users can type text, undo their last action, and redo an undone action through a simple menu interface. The program maintains the current text state and allows for navigating through the text history effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Java Undo/Redo Feature Simulation

The document presents a Java program that simulates an undo/redo feature for a text editor using two stacks. Users can type text, undo their last action, and redo an undone action through a simple menu interface. The program maintains the current text state and allows for navigating through the text history effectively.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

💻 Java Program: Undo / Redo Feature Simulation

import [Link];

import [Link];

public class UndoRedoEditor {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

Stack<String> undoStack = new Stack<>();

Stack<String> redoStack = new Stack<>();

String text = ""; // current text content

int choice;

do {

[Link]("\nCurrent Text: " + text);

[Link]("\n--- MENU ---");

[Link]("1. Type text");

[Link]("2. Undo");

[Link]("3. Redo");

[Link]("4. Exit");

[Link]("Choose an option: ");

choice = [Link]();

[Link](); // consume newline

switch (choice) {

case 1:

[Link]("Enter text to add: ");


String newText = [Link]();

[Link](text); // Save current state before typing

text += newText;

[Link](); // Clear redo history after new typing

break;

case 2:

if (![Link]()) {

[Link](text); // save current before undo

text = [Link](); // restore previous text

[Link]("Undo successful!");

} else {

[Link]("Nothing to undo!");

break;

case 3:

if (![Link]()) {

[Link](text); // save current before redo

text = [Link](); // restore redone text

[Link]("Redo successful!");

} else {

[Link]("Nothing to redo!");

break;

case 4:

[Link]("Exiting program...");

break;
default:

[Link]("Invalid choice! Please try again.");

} while (choice != 4);

[Link]();

}
Sample Output

Current Text:

--- MENU ---

1. Type text

2. Undo

3. Redo

4. Exit

Choose an option: 1

Enter text to add: Hello

Current Text: Hello

--- MENU ---

1. Type text

2. Undo

3. Redo

4. Exit

Choose an option: 1

Enter text to add: World

Current Text: HelloWorld

Choose an option: 2

Undo successful!

Current Text: Hello

Choose an option: 3
Redo successful!

Current Text: HelloWorld

📦 Analogy
Think of Undo and Redo as moving backward and forward through your
document’s history timeline:
Past <--- [UNDO STACK] <--- Current ---> [REDO STACK] ---> Future

Common questions

Powered by AI

No, the program cannot perform a redo operation when both stacks are empty. If the 'redoStack' is empty, which implies no steps to redo, the program outputs the message "Nothing to redo!" and the text remains unchanged .

The 'redoStack.clear();' statement is crucial when new text is added because it eliminates irrelevant redo actions that would no longer apply after the text content has changed with new typing. This ensures the integrity of redo operations by resetting the redo history, which prevents illogical attempts to reapply an outdated state to the new text content .

During an undo operation, the current text is pushed onto the 'redoStack' to save its state before the undoing. This allows the text to be redone later if necessary. Then, the last entry from the 'undoStack' is popped to revert the text to its prior state. This mechanism ensures that the application can backtrack and restore previous versions of the text efficiently .

The program handles invalid user selections through the use of a default case in the switch statement. If the user enters an option that does not correspond to any of the preset commands (1 through 4), the default case executes, displaying the message "Invalid choice! Please try again." This prompt encourages the user to provide a valid input .

The program maintains the 'undoStack' by pushing the current text into the 'redoStack' before executing an undo operation. This action saves the current text state, allowing it to be pushed back onto the 'undoStack' if a redo action later restores it. Thus, the 'undoStack' is dynamically updated to reflect all reversible changes to ensure state retrievability .

The loop in the Java program allows for continuous user interaction, enabling multiple command inputs until the user decides to exit. This loop facilitates the ongoing application of typing, undoing, and redoing actions on the text string without needing to restart the program, thus enhancing user experience and program efficiency .

The Undo/Redo feature is implemented using two stacks: 'undoStack' and 'redoStack'. When a user types new text, the current text is pushed onto the 'undoStack' before updating it, allowing the last state to be retrievable. If an undo command is issued, the current text is pushed to the 'redoStack' to preserve the state before undoing, and the top element of 'undoStack' is set as the new text, effectively undoing the last change. When a redo command is issued, the current text is pushed to the 'undoStack' and the top element of the 'redoStack' is set as the new text, redoing the undone change .

If the user attempts to undo with an empty 'undoStack', the program will output the message "Nothing to undo!" because there are no prior actions recorded that can be reverted. The text remains unchanged in this scenario .

User input is managed through a Scanner object, capturing the choice of action and processing it via a switch-case structure. Each choice corresponds to a specific operation: typing, undoing, redoing, or exiting. Input for typing requires reading an additional line for the new text to be appended. A loop continues to prompt user choices until the exit command is chosen, ensuring continuous interaction and operation of the Undo/Redo feature .

The Java program clears the 'redoStack' whenever new text is typed. This is because typing new text represents a deviation in the text's historical timeline, making previous redo actions irrelevant. By clearing the 'redoStack', the program ensures that redo actions cannot occur upon a change of direction after new text entry .

You might also like