inserting new line...
1> line 1
***************
inserting new line...
1> line 1
2> line 2
***************
inserting new line...
1> line 1
2> line 2
3> line 3
***************
inserting new line...
1> line 1
2> line 2
3> line 3
4> line 4
***************
removing a line...
1> line 1
3> line 3
4> line 4
***************
undo last operation...
1> line 1
2> line 2
3> line 3
4> line 4
***************
undo last operation...
1> line 1
2> line 2
3> line 3
Program Specifications
There is no screenshot to attach as the driver program was not part of the specification.
please rate if you find the answer satisfactory and do comment if you face any issues.
DocumentBuffer
package undo;
import java.util.ArrayList;
import java.util.List;
public class DocumentBuffer {
private List<Line> lineBuffer;
private final CareTaker ct;
public DocumentBuffer (CareTaker ct) {
this.ct = ct;
this.lineBuffer = new
ArrayList<Line>();
ct.add(new
Memento(this.lineBuffer));
}
public boolean addLine(Line line) {
try {
saveStateToMemento();
this.lineBuffer.add(line);
System.out.println("Adding Line...");
return
true;
} catch (Exception e) {
return
false;
}
}
public int removeLine(int lineno) {
try {
saveStateToMemento();
this.lineBuffer.remove(lineno);
System.out.println("Removing Line...");
return
lineno;
} catch (Exception e) {
return -1;
}
}
public String toString() {
StringBuilder sb = new
StringBuilder();
for (Line l : this.lineBuffer)
{
sb.append(l.toString() + "\n");
}
return null;
}
private Memento saveStateToMemento() {
Memento mem = new
Memento(this.lineBuffer);
ct.add(mem);
return mem;
}
private void restoreStateFromMemento(Memento mem) {
this.lineBuffer =
mem.getState();
}
public boolean undo() {
try {
restoreStateFromMemento(this.ct.get());
return
true;
} catch (Exception e) {
return
false;
}
}
}
Line
package undo;
public class Line {
private String text;
private int lineno;
public Line (String text, int lineno) {
this.text = text;
this.lineno = lineno;
}
public String toString() {
return this.lineno + "> " +
this.text;
}
}
CareTaker
package undo;
import java.util.Deque;
import java.util.LinkedList;
public class CareTaker {
private Deque mStack; // use Deque<> as a stack
public CareTaker () {
super();
mStack = new
LinkedList<Memento>() {};
}
public void add(Memento state) {
mStack.add(state);
}
public Memento get() {
return (Memento)
mStack.pop();
}
}
Memento
package undo;
import java.util.ArrayList;
import java.util.List;
public class Memento {
private List state;
public Memento () {
this.state = new
ArrayList<ArrayList<Line>>();
}
public Memento (List state) {
this.state = new
ArrayList<ArrayList<Line>>(state);
}
public List getState() {
return (List)
this.state.get(this.state.size() - 1);
}
}
You are to write a Java program that emulates an “undo” operation in programs like Word...
You are to write a program that emulates an “undo” operation in programs like Word processors. 2. You will need to use the proper data stuctures as you see fit. 3. Specifically, you will create a data type called, DocumentBuffer, that acts as edit buffer & keeps a list of Line data type. 4. To keep it simple, DocumentBuffer will have these editing operations: add a Line and remove a Line. 5. DocumentBuffer will store its state during any edits....
C# ONLY C# ONLY C# ONLY In this assignment you will implement the undo feature for a simple calculator. The undo feature must be implemented using a generic or template-based Stack data structure. You may utilize language supplied libraries (e.g. java.util.stack) or a stack implementation of your own design. No third-party downloads or software installs are allowed. The undo feature must allow for an unlimited number of undo operations. If the user attempts to perform an undo operation and the...
JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...
Java.
Must not use Java API java.util.Stack
/**
A class of stacks whose entries are stored in an array.
Implement all methods in ArrayStack class using resizable
array strategy, i.e. usedoubleArray()
Must throw StackException during exception events in methods:
peek(), pop(), ArrayStack(int initialCapacity)
Do not change or add data fields
Do not add new methods
*/
import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...
Java.
Must not use Java API java.util.Stack
/**
A class of stacks whose entries are stored in an array.
Implement all methods in ArrayStack class using resizable
array strategy, i.e. usedoubleArray()
Must throw StackException during exception events in methods:
peek(), pop(), ArrayStack(int initialCapacity)
Do not change or add data fields
Do not add new methods
*/
import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...
22.7 Lab: Word ladder Write a word ladder program. Read this wikipedia article that describes what a word ladder is: Word Ladder Your program must use a list to store the dictionary of words and then use stacks of strings and a queue of these stacks to solve for and output the word ladder. You are required to use the Standard Template Library (STL) list, stack, and queue. You must implement the WordLadder class. The class is declared in WordLadder.h....
I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array. //Array implementation of stacks. import java.util.Arrays; public class ArrayStack implements Stack { //Declare a class constant called DEFAULT_STACK_SIZE with the value 10. private static final int DEFAULT_STACK_SIZE = 10; /* Declare two instance variables: 1. An integer called...
Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...
solve this Q in java languege Write a method that return DoublyLinkedList as reversed string For example: If the elements of a list is 1, 2, 3, 4, 5, 6 the reverse string should be 6, 5, 4, 3, 2, 1 implement reverse method you have two steps: 1- you should start traversing from the last element of DoublyLinkedList (the previous of the trailer) 2- you should add the element inside each node to string don't forget the space in...
Modify the following given Java program. You are expected to modify the supplied Exercise1.java to print just the last lines of each paragraph in HTML format. Copy and paste the Sample input on the console then print the Expected output. import java.util.Scanner; public class Exercise1 { /** A simple static string for HTML & table header. */ private static final String HTMLHeader = "<!DOCTYPE html>\n" + " <html>\n" + " <head>\n" + " <title>CSE: Exercise 1</title>\n" + " </head>\n" +...