Question

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

[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

public class UndoStackRunner
{

   public static void main(String[] args)
   {
      UndoStack words = new UndoStack();
      words.add("Mary had a");
      words.add("tiny lamp");
      words.undo();
      words.add("little lamb.");
      words.add("I's ");
      words.undo();
      words.add("It's");
      words.add("fleece");
      words.add("was");
      words.add("white as snow.");
      
      String removed;
      while ((removed = words.undo()) != null)
      {
         System.out.println(removed);
      }

      words.add("End of story");
      words.undoAll();
      
      if ((words.undo()) == null)
      {
         System.out.println("Can not undo. History is empty.");
      }
   }

}

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

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

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.

Add a comment
Answer #2

Answer:

Program code screen shot:

UndoStack.Java import the Stack and Scanner classes fron the util package import java.util. public class UndoStack // Declare

Sample Output:

<terminated> UndoStackRunner [Java Application] C:\Program white as snow. was Ileece Its little lamb Mary had a Can not undo

Program code to copy:

// UndoStack.java

// import the Stack and Scanner classes from the util package

import java.util.*;

public class UndoStack

{

     // Declare the Stack predefine object for Stack

     private Stack<String> stack;

     // constructor

     UndoStack()

     {

          // initialize the stack

          stack = new Stack<String>();

     }

     // add() method to push the string a value into

     // stack

     public void add(String a)

     {

          // adding element to stack

          stack.push(new String(a));

     }

     // undo() method to

     public String undo()

     {

          // condition to check whether the stack is empty or not

          // if empty, then pop the value from the stack

          if (!stack.empty())

          {

              // returns the string that has been

              // removed from the stack

              String a = stack.pop();

                            

              // return the removed stack value

              return a;

          }

         

          // otherwise return null value

          return null;

     }

     // undoAll() method is used to empty the stack

     public void undoAll()

     {

          // loop to pop all the elements from the stack

          // until the stack is empty

          while (!stack.empty())

          {

              // deleting all elements in the stack

              stack.pop();

          }

     }

}

public class UndoStackRunner

{

   public static void main(String[] args)

   {

      UndoStack words = new UndoStack();

      words.add("Mary had a");

      words.add("tiny lamp");

      words.undo();

      words.add("little lamb.");

      words.add("I's ");

      words.undo();

      words.add("It's");

      words.add("fleece");

      words.add("was");

      words.add("white as snow.");

     

      String removed;

      while ((removed = words.undo()) != null)

      {

         System.out.println(removed);

      }

      words.add("End of story");

      words.undoAll();

     

      if ((words.undo()) == null)

      {

         System.out.println("Can not undo. History is empty.");

      }

   }

}

Add a comment
Know the answer?
Add Answer to:
[Java] Please test your code in the link I provide before you post your answer. The...
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
  • [Java] Please test your code in the link I provide before you post your answer. The...

    [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/17033122188mcxvjz8n8qbk0k9fyfrd3w95 Use the following file: LinkedListUtilTester.java import java.util.LinkedList; public class LinkedListUtilTester { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); list.add("6"); list.add("7"); list.add("8"); list.add("9"); list.add("10"); list.add("11"); list.add("12"); list.add("13"); list.add("14"); list.add("15"); LinkedListUtil.shrink(list, 3); System.out.println(list); System.out.println("Expected: [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]"); System.out.println(LinkedListUtil.reverse(list)); System.out.println("Expected:...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

  • Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac...

    Can someone help me to figure that error I have put below. JAVA ----jGRASP exec: javac -g P4Program.java P4Program.java:94: error: package list does not exist Iterator i = new list.iterator(); ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. Note: Below there are two different classes that work together. Each class has it's own fuctions/methods. import java.util.*; import java.io.*; public class P4Program{ public void linkedStackFromFile(){ String content = new String(); int count = 1; File...

  • Java - I need help creating a method that removes a node at the specific index...

    Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...

  • Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of...

    Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...

  • Consider java for fixing this code please: what i need is to insert method to be...

    Consider java for fixing this code please: what i need is to insert method to be added ( please don't change the test class and any giving value in the first class ) here is the correct out put: ------------------testAddLast()---- {A} {A->B} {A->B->null} {A->B->null->C} ----------------------------- --------testSubListOfSmallerValues()---------- {} {B->B->B->A} {F->B->B->B->A->D} {F->B->B->G->B->A->M->D} ----------------------------- ------------Test lastIndexOf()----- -1 3 -1 -1 0 5 2 ----------------------------- ---------testRetainAll()--------- {} {6:Tony->6:Tony} {null->bad->null} ----------------------------- ---------------Test removeStartingAtBack--- false true {apple->null->bad->null} true {apple->null->bad} {2:Morning->3:Abby->4:Tim->5:Tom->6:Tony} ----------------------------- ---------test insertionSort()--------- {} {D} {D->E->E->F->G}...

  • how would I complete this code without calling any built-in java collection framework classes like ArrayList,...

    how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc? import java.util.Iterator; class CallStack<T> implements Iterable<T> { // You'll want some instance variables here public CallStack() { //setup what you need } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } public T pop() { //pop an item off the stack //if there are no items on the stack, return...

  • I need help with a c++ code, i am new learner on stack, please help me...

    I need help with a c++ code, i am new learner on stack, please help me write Stack.cpp according to Stack.h provided, thanks(Notes: That data structure is a singly linked list in which pushed items are placed at the tail of the linked list. Similarly, popped items will be removed from the tail of the list.) Actually,I can write a regular one, but i'm not sure how to use the tail in the question. class Stack { private: // Desc:...

  • In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These...

    In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...

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