Question

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 stack is empty, then a message must be displayed indicating that there are no commands to undo. The calculator portion of the assignment must function as follows. The calculator must be implemented in a stand-alone class called ‘Calculator’. The calculator must support only the following operations: addition (+), subtraction (-), multiplication (*), and division (/). The calculator must support whole and real numbers. The calculator must support the following commands: UNDO, CLEAR, EXIT UNDO: Ignores the prior calculation and restores state as if the calculation never occurred. If there are no prior calculations to ignore, then display the message “UNDO IS NOT AVAILABLE” and request the user to enter a command or calculation request. CLEAR: Resets the calculator to the initial state, returns the Running Total to 0, and clears the undo stack. EXIT: Terminates the program without producing any results. If a command does not start with one of the preceding three commands, then the input is expected to be a calculation request in the following format: The user will supply input into the calculator in the following format: decimal-number operation decimal-number decimal-number: decimal-integer-literal real-literal real-literal: decimal-digits . decimal-digits decimal-integer-literal: decimal-digits decimal-digits: decimal-digit decimal-digits decimal-digit decimal-digit: one of 0 1 2 3 4 5 6 7 8 9 operation: one of + - * / Any input that does no conform to this input format should be rejected with an error message indicating that the input is invalid. The calculator must maintain a running total of all the operations. The running total is the result of the last operation added the running total. For example, if the calculator is presented with the following input: 10 + 5 Sum: 15; Running Total : 15 20 – 5 Difference: 15; Running Total: 30 2 * 3 Product: 6; Running Total: 36 UNDO Running Total: 30 CLEAR Running Total: 0 EXIT The Memento Design Pattern Memento design pattern is a behavioral design pattern and is used when the state of an object needs to be saved in case it needs to be acted upon in the future. The pattern consists of three objects: Originator, Memento, and Caretake Originator - the object that knows how to save itself. 1. Creates a memento containing a snapshot of its current internal state. 2. Uses the memento to restore its internal state. 3. In this program, the Calculator class is the Originator of the memento object. Caretaker - the object that knows why and when the Originator needs to save and restore itself. 1. Is responsible for the memento's safekeeping. 2. Never operates on or examines the contents of a memento. 3. In this assignment, the Caretaker is a stack data structure that will contain Memento objects provided by the Calculator class. Memento: stores internal state of the Originator object. The memento may store as much or as little of the originator's internal state as necessary at its originator's discretion. 1. Stores internal state of the Originator object. The memento may store as much or as little of the originator's internal state as necessary at its originator's discretion. 2. Protect against access by objects of other than the originator

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


import java.util.List;

import java.util.ArrayList;

import javax.swing.event.ChangeEvent;

import java.util.Scanner;


public class Calculator {


    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        System.out.println("Simple Calculator");

        System.out.println("\nHere are your options:");
        System.out.println("\n1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Division");
        System.out.println("4. Multiplication");

        System.out.print("\nWhat would you like to do?: ");
        int choice = kb.nextInt();
        System.out.println();


        if (choice == 1){
            addition();
        }
        else if (choice == 2){
            subtraction();
        }
        else if (choice == 3){
            division();
        }
        else if (choice == 4){
            multiplication();
        }

        System.out.println();
        kb.close();
    }

    public static void addition(){

        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Addition");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " + " + nTwo + " = " + (nOne + nTwo));
    }

    public static void subtraction(){
        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Subtraction");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " - " + nTwo + " = " + (nOne - nTwo));
    }

    public static void division(){
        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Division");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " / " + nTwo + " = " + (nOne / nTwo));
    }

    public static void multiplication(){
        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Multiplication");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " x " + nTwo + " = " + (nOne * nTwo));
    }
}


public class UndoRedomanager {

  


   private Node currentIndex = null;


   private Node parentNode = new Node();

  


   public UndoRedomanager (){

       currentIndex = parentNode;

   }

  

  
   public UndoRedomanager (UndoRedomanager manager){

       this();

       currentIndex = manager.currentIndex;

   }

  

  

   public void clear(){

       currentIndex = parentNode;

   }

   public void addChangeable(Changeable changeable){

       Node node = new Node(changeable);

       currentIndex.right = node;

       node.left = currentIndex;

       currentIndex = node;

   }

  

  

   public boolean canUndo(){

       return currentIndex != parentNode;

   }

  

   public boolean canRedo(){

       return currentIndex.right != null;

   }

  

  

   public void undo(){


       if ( !canUndo() ){

           throw new IllegalStateException("Cannot undo. Index is out of range.");

       }


       currentIndex.changeable.undo();


       moveLeft();

   }

  

  

   private void moveLeft(){

       if ( currentIndex.left == null ){

           throw new IllegalStateException("Internal index set to null.");

       }

       currentIndex = currentIndex.left;

   }

  

   private void moveRight(){

       if ( currentIndex.right == null ){

           throw new IllegalStateException("Internal index set to null.");

       }

       currentIndex = currentIndex.right;

   }

  

   public void redo(){


       if ( !canRedo() ){

           throw new IllegalStateException("Cannot redo. Index is out of range.");

       }


       moveRight();

      

       currentIndex.changeable.redo();

   }

  

   private class Node {

       private Node left = null;

       private Node right = null;

      

       private final Changeable changeable;

      

       public Node(Changeable c){

           changeable = c;

       }

      

       public Node(){

           changeable = null;

       }

   }

  

}

Add a comment
Know the answer?
Add Answer to:
C# ONLY C# ONLY C# ONLY In this assignment you will implement the undo feature for a simple calculator. The undo feature...
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
  • 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....

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

  • In this lab you will code a simple calculator. It need not be anything overly fancy,...

    In this lab you will code a simple calculator. It need not be anything overly fancy, but it is up to you to take it as far as you want. You will be creating this program in three small sections. You have the menu, the performance of the basic arithmetic operations (Addition, Subtraction Multiplication, and Division), and the looping. You may want to get the switch menu working first, and then fill in the code for these four arithmetic operations...

  • Program using visual basic.net You will create a very simple two numbers calculator with save options;...

    Program using visual basic.net You will create a very simple two numbers calculator with save options; here is the specifications for the application. Create a form divided vertically in two halves with right and left panels 1- In the left panel you will create the following control, the label or the value of the control will be in "" and the type of the control will in [] a- "First Number" [textbox] b- "Second number" [texbox] c- "Result" [textbox] -...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

  • The following guidelines outline the basic template for a robot vacuum cleaner game. The game must be implemented in c programming language. It mimics a robotic vacuum cleaner. The code must only use...

    The following guidelines outline the basic template for a robot vacuum cleaner game. The game must be implemented in c programming language. It mimics a robotic vacuum cleaner. The code must only use the following libraries: #include <math.h> #include <stdlib.h> #include <string.h> #include <limits.h> and any .graphics and .timers libraries. The guidelines are outlined as follows: Terminal Set-up: you may assume that the terminal will be quite large, for example, on the order of 150×50, or more. Status Display: The...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

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