Question

You are to write a program that emulates an “undo” operation in programs like Word processors....

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. It will need a help from another data type, called CareTaker.
6. Any time you make an edit, e.g. add a line or delete a line, DocumentBuffer will ask, i.e. use the methods of, the CareTaker to store its current state. DocumentBuffer state is defined as its lineBuffer list that you add or delete lines.
7. DocumentBuffer will expose a public method called undo() that will restore its state to the last known state before making any changes. It will basically restore its state to the last known state prior to making any edits. It will always undo the last operation and you can keep calling undo to restore prior states, until there are no more undo to execute. This inidcates that you have reached the initial state.
8. The following scenario demonstrate how this works. The lines I am adding or deleting are simple strings like ‘line 1’, ‘line 2’, etc. Lines like ‘inserting …’, ‘removing…’, ‘undo…’ are just println() statements from the test driver to show what I am doing and they are not part of the buffer. Also, the prefix like ‘1>, 2>’, etc. are line numbers associated with line number. Lines with “***********” is just a separator between each state, again printed from the test driver.
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
1. DocumentBuffer class—a simple edit manager that stores lines (just a list of strings), exposes edit methods, undo opertaions and provides methods to store and restore its internal state.
1. Data members:
▪ private List<Line> lineBuffer;
▪ private final CareTaker ct;
2. Methods:
▪ public DocumentBuffer(CareTaker ct){}—constructor that requires CareTaker object and initializes its ct data mamber. You also need to initialize lineBuffer and use ct to store its initial state (more on CareTaker specs below).
▪ public boolean addLine(Line line){}—adds a line to the line buffer. Just add the line to the end of the buffer. Must save state before adding a new a line. Return true if all goes well, false otherwise.
▪ public int removeLine(int lineno){}—removes a line based on its line number. Must save state before removing a new a line. Return line number that was removed.
▪ public String toString(){}—this method will dump all lines in the buffer in the manner I showed in the sample output above.
▪ private Memento saveStateToMemento(){}—this method will save the current state using a Memento object (specs to follow). Retun the Memeto object just created.
▪ private void restoreStateFromMemento(Memento mem){}—this method will restore the state from a Memento object passed to it.
▪ public boolean undo(){}—this the public method that you will use to undo last operation. It will need the help of CareTaker to retrieve last Memento object stored and then calls restoreStateFromMemento() passing it the Memnto object just retreived from the CareTaker.
2. Line class—a class that represents the edit lines that DocumentBuffer manages.
1. Data members:
▪ private String text;
▪ private int lineno;
2. Methods:
▪ public Line(String text, int lineno){}—initializer contructor
▪ public String toString(){}—returns a string representation in the manner shown above in the sample output.
3. Memento class—a class that stores/restore the states of DocumentBuffer. Think of it as object that remmebers what you asked it to remember, hence the name Memento.
1. Data members:
▪ private List<Line> state;
2. Methods:
▪ public Memento(List<Line> state){}—stores state passed to it.
▪ public List<Line> getState(){}—get state stored in this object.
4. CareTaker class—is stack that stores and retrieves Memento objects.
1. Data members:
▪ private Deque<Memento> mStack; // use Deque<> as a stack
2. Methods:
▪ public void add(Memento state){}—push Memnto object passed onto a stack
▪ public Memento get(){}—pops last Memento object from the stack

PA3 class—is the test driver program below to test your program.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pa3;

/**
*
*/
public class PA3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CareTaker ct = new CareTaker();
DocumentBuffer buffer = new DocumentBuffer(ct);

System.out.println("inserting new line...");
buffer.addLine(new Line("line 1", 1));
System.out.println(buffer);
System.out.println("***************");

// add line
System.out.println("inserting new line...");
buffer.addLine(new Line("line 2", 2));
System.out.println(buffer);
System.out.println("***************");

// add line
System.out.println("inserting new line...");
buffer.addLine(new Line("line 3", 3));

System.out.println(buffer);
System.out.println("***************");

// add line
System.out.println("inserting new line...");
buffer.addLine(new Line("line 4", 4));

System.out.println(buffer);
System.out.println("***************");

// remove line 2
System.out.println("removing a line...");
buffer.removeLine(2);
System.out.println(buffer);
System.out.println("***************");

// undo last insert
System.out.println("undo last operation...");
boolean rc = buffer.undo();
System.out.println(buffer);
System.out.println("***************");

// undo last insert
System.out.println("undo last operation...");
rc = buffer.undo();
System.out.println(buffer);
System.out.println("***************");

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Ans:

void InputHandler::handleInput()
{
  if (isPressed(BUTTON_X)) jump();
  else if (isPressed(BUTTON_Y)) fireGun();
  else if (isPressed(BUTTON_A)) swapWeapon();
  else if (isPressed(BUTTON_B)) lurchIneffectively();
}
class Command
{
public:
  virtual ~Command() {}
  virtual void execute() = 0;
};
class JumpCommand : public Command
{
public:
  virtual void execute() { jump(); }
};

class FireCommand : public Command
{
public:
  virtual void execute() { fireGun(); }
};
class InputHandler
{
public:
  void handleInput();

  // Methods to bind commands...

private:
  Command* buttonX_;
  Command* buttonY_;
  Command* buttonA_;
  Command* buttonB_;
};
void InputHandler::handleInput()
{
  if (isPressed(BUTTON_X)) buttonX_->execute();
  else if (isPressed(BUTTON_Y)) buttonY_->execute();
  else if (isPressed(BUTTON_A)) buttonA_->execute();
  else if (isPressed(BUTTON_B)) buttonB_->execute();
}
Add a comment
Know the answer?
Add Answer to:
You are to write a program that emulates an “undo” operation in programs like Word processors....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C# ONLY C# ONLY C# ONLY In this assignment you will implement the undo feature for a simple calculator. The undo feature...

    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...

  • Modify the following given Java program. You are expected to modify the supplied Exercise1.java to print...

    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" +...

  • In the processLineOfData, write the code to handle case "H" of the switch statement such that:...

    In the processLineOfData, write the code to handle case "H" of the switch statement such that: An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows:               Double.parseDouble(rate) Call the parsePaychecks method in this class passing the HourlyEmployee object created in the previous step and the checks variable. Call the findDepartment method...

  • Write a java class definition for a circle object. The object should be capable of setting...

    Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { }    //get method (Accessor Methods ) public double getRadius (...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • Hello, Can you please error check my javascript? It should do the following: Write a program...

    Hello, Can you please error check my javascript? It should do the following: Write a program that uses a class for storing student data. Build the class using the given information. The data should include the student’s ID number; grades on exams 1, 2, and 3; and average grade. Use appropriate assessor and mutator methods for the grades (new grades should be passed as parameters). Use a mutator method for changing the student ID. Use another method to calculate the...

  • PLEASE EDIT THE LAST 3 METHODS WHERE I PUT STARS AND DO WHAT THE COMMENTS SAY....

    PLEASE EDIT THE LAST 3 METHODS WHERE I PUT STARS AND DO WHAT THE COMMENTS SAY. THE CODE IS IN JAVA PROGRAMMING LANGUAGE. import java.util.AbstractList; import java.util.List; import java.util.RandomAccess; import java.lang.RuntimeException; import java.util.Arrays; public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess {    protected Object[] data; protected int size; public int size() {     return size; }    private void rangeCheck(int index) {     if (index < 0 || index >= size) throw new IndexOutOfBoundsException(""); }    @SuppressWarnings("unchecked") private E...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

  • What if you had to write a program that would keep track of a list of...

    What if you had to write a program that would keep track of a list of rectangles? This might be for a house painter to use in calculating square footage of walls that need paint, or for an advertising agency to keep track of the space available on billboards. The first step would be to define a class where one object represents one rectangle's length and width. Once we have class Rectangle, then we can make as many objects of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT