Skip to the content.

CSA Calculator Enactment

Stuff for calc enactment

Homework (hw)

  • Queue Task: Modify the CalculatorQueue to support more complex operations, such as multiplication and division.
  • Stack Task: Modify the CalculatorStack to reverse the order of addition and handle operations in the LIFO sequence.
  • Add a method to both the stack and queue programs to handle invalid operations and display an error message.
  • Create a method for both programs to display all operations before processing them, and track the result after each operation.

Advanced Task: Implement a calculator that supports parentheses using a stack to ensure proper operation precedence.

  • Postfix evaulation using an array (this allows numbers to be more than 0-9)
import java.util.LinkedList;
import java.util.Queue;

public class CalculatorQueue {
    public static void main(String[] args) {
        // Create a queue to store operations
        Queue<String> operations = new LinkedList<>();
        
        // Enqueue tasks
        operations.add("3 + 5");
        operations.add("2 - 1");
        operations.add("4 * 2");
        operations.add("8 / 2");
        operations.add("8 / 0"); // Example of division by zero

        // Process tasks in order
        while (!operations.isEmpty()) {
            String operation = operations.poll(); // Dequeue
            System.out.println("Processing: " + operation);

            // Split the operation into parts
            String[] parts = operation.split(" ");
            System.out.println("Current operation parts: " + String.join(" ", parts)); // Print the operation parts

            if (parts.length != 3) {
                System.out.println("Error: Invalid operation format");
                continue;
            }

            String operator = parts[1];
            try {
                int operand1 = Integer.parseInt(parts[0]);
                int operand2 = Integer.parseInt(parts[2]);

                switch (operator) {
                    case "+":
                        // Perform addition
                        int sum = operand1 + operand2;
                        System.out.println("Result: " + sum);
                        break;
                    case "-":
                        // Perform subtraction
                        int difference = operand1 - operand2;
                        System.out.println("Result: " + difference);
                        break;
                    case "*":
                        // Perform multiplication
                        int product = operand1 * operand2;
                        System.out.println("Result: " + product);
                        break;
                    case "/":
                        // Perform division
                        if (operand2 != 0) {
                            int quotient = operand1 / operand2;
                            System.out.println("Result: " + quotient);
                        } else {
                            System.out.println("Error: Division by zero");
                        }
                        break;
                    default:
                        System.out.println("Error: Unknown operation");
                        break;
                }
            } catch (NumberFormatException e) {
                System.out.println("Error: Invalid number format");
            }
        }
    }
}

// Call main function
CalculatorQueue.main(null);
Failed to start the Kernel. 


Jupyter Server crashed. Unable to connect. 


Error code from Jupyter: 1


Traceback (most recent call last):


  File "/Library/Frameworks/Python.framework/Versions/3.11/bin/jupyter-notebook", line 5, in <module>


    from notebook.app import main


  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/notebook/app.py", line 12, in <module>


    from jupyter_server.base.handlers import JupyterHandler


  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/jupyter_server/base/handlers.py", line 23, in <module>


    from jupyter_events import EventLogger


  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/jupyter_events/__init__.py", line 3, in <module>


    from .logger import EVENTS_METADATA_VERSION, EventLogger


  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/jupyter_events/logger.py", line 14, in <module>


    from jsonschema import ValidationError


  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/jsonschema/__init__.py", line 13, in <module>


    from jsonschema._format import FormatChecker


  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/jsonschema/_format.py", line 11, in <module>


    from jsonschema.exceptions import FormatError


  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/jsonschema/exceptions.py", line 15, in <module>


    from referencing.exceptions import Unresolvable as _Unresolvable


  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/referencing/__init__.py", line 4, in <module>


    from referencing._core import Anchor, Registry, Resource, Specification


  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/referencing/_core.py", line 9, in <module>


    from rpds import HashTrieMap, HashTrieSet, List


  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/rpds/__init__.py", line 1, in <module>


    from .rpds import *


ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/rpds/rpds.cpython-311-darwin.so, 0x0002): tried: '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/rpds/rpds.cpython-311-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/rpds/rpds.cpython-311-darwin.so' (no such file), '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/rpds/rpds.cpython-311-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64')). 


View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.

Stack task

  • Stack Task: Modify the CalculatorStack to reverse the order of addition and handle operations in the LIFO sequence.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class CalculatorQueue {
    public static void main(String[] args) {
        // Create a queue to store operations
        Queue<String> operations = new LinkedList<>();
        
        // Enqueue tasks
        operations.add("3 + 5");
        operations.add("2 - 1");
        
        // Process tasks in order
        while (!operations.isEmpty()) {
            String operation = operations.poll(); // Dequeue
            System.out.println("Processing: " + operation);
            
            // Stack to reverse the order of addition and other operations in the LIFO sequence
            Stack<String> stack = new Stack<>();
            stack.push(operation);
            
            while (!stack.isEmpty()) {
                String op = stack.pop();
                
                // Print the operation before performing any action
                System.out.println("Current operation: " + op);
                
                // Split the operation into parts
                String[] parts = op.split(" ");
                if (parts.length != 3) {
                    System.out.println("Error: Invalid operation format");
                    continue;
                }
                
                String operator = parts[1];
                try {
                    int operand1 = Integer.parseInt(parts[0]);
                    int operand2 = Integer.parseInt(parts[2]);
                    
                    switch (operator) {
                        case "+":
                            // Perform addition
                            int sum = operand1 + operand2;
                            System.out.println("Result: " + sum);
                            break;
                        case "-":
                            // Perform subtraction
                            int difference = operand1 - operand2;
                            System.out.println("Result: " + difference);
                            break;
                        case "*":
                            // Perform multiplication
                            int product = operand1 * operand2;
                            System.out.println("Result: " + product);
                            break;
                        case "/":
                            // Perform division
                            if (operand2 != 0) {
                                int quotient = operand1 / operand2;
                                System.out.println("Result: " + quotient);
                            } else {
                                System.out.println("Error: Division by zero");
                            }
                            break;
                        default:
                            System.out.println("Error: Unknown operation");
                            break;
                    }
                } catch (NumberFormatException e) {
                    System.out.println("Error: Invalid number format");
                }
            }
        }
    }
}

CalculatorQueue.main(null);
Processing: 3 + 5
Current operation: 3 + 5
Result: 8
Processing: 2 - 1
Current operation: 2 - 1
Result: 1