Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Stack calculator Java This programming assignment will focus on stacks, queues a

ID: 3593207 • Letter: S

Question

Stack calculator Java

This programming assignment will focus on stacks, queues and dynamic memory. Your assignment is to implement both a stack and/or a queue class.

Write a simple application to manage an adding machine program, where the stack is used to push and pop numbers and results. Your application should read in floating point numbers and push them onto a stack and add them together whenever a plus sign is entered. You application should support the standard mathematical operations: addition, subtraction, multiplication and division. When the equal sign (=) is requested from the user, the result should be displayed.

Your application should also design a way for the user to cancel all entries ( which clears the stack ).

Use your queue to manage the input received. All of the data must be read one line at a time from the standard input, then enqueued for future use. The entire file must be read and stored in memory for processing. Then, retrieve it from the queue to determine what operations the user has requested.

Data Structures

The stack and/or queue abstractions must be implemented using linear linked lists of arrays. All memory within the abstractions (nodes, arrays or characters, arrays of integers) must be dynamically allocated. Module 6 Lab shows how we can implement both stacks and queues for pushing and popping abstract data.

Complete the application as defined above, implementing stacks/queues as desired but your application must contain at least one.

Application Example

Input : 45

Input : +

Input : 34

Input : =

Output: 34 + 45 = 79

Explanation / Answer



import java.util.StringTokenizer;

public class InfixFullParantEx {

    public static String evaluateInfix(String exps){
        exps = exps.replaceAll("\s+", "");
        MyGenericsStack<String> stack = new MyGenericsStack<String>(exps.length());
        StringTokenizer tokens = new StringTokenizer(exps, "{}()*/+-", true);
        while(tokens.hasMoreTokens()){
            String tkn = tokens.nextToken();
            if(tkn.equals("(")
                    || tkn.equals("{")
                    || tkn.matches("[0-9]+")
                    || tkn.equals("*")
                    || tkn.equals("/")
                    || tkn.equals("+")
                    || tkn.equals("-")){
                stack.push(tkn);
            } else if(tkn.equals("}") || tkn.equals(")")){
                try {
                    int op2 = Integer.parseInt(stack.pop());
                    String oprnd = stack.pop();
                    int op1 = Integer.parseInt(stack.pop());
                    
                    if(!stack.isStackEmpty()){
                        stack.pop();
                    }
                    int result = 0;
                    if(oprnd.equals("*")){
                        result = op1*op2;
                    } else if(oprnd.equals("/")){
                        result = op1/op2;
                    } else if(oprnd.equals("+")){
                        result = op1+op2;
                    } else if(oprnd.equals("-")){
                        result = op1-op2;
                    }
                   
                    stack.push(result+"");
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
        String finalResult = "";
        try {
            finalResult = stack.pop();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return finalResult;
    }
    
    public static void main(String a[]){
        String expr = "((2*5)+(6/2))";
        System.out.println("Expression: "+expr);
        System.out.println("Final Result: "+evaluateInfix(expr));
        expr = "(((2 * 5) - (1 * 2)) / (11 - 9))";
        System.out.println("Expression: "+expr);
        System.out.println("Final Result: "+evaluateInfix(expr));
        
    }
}


* Stack implementation
public class MyGenericsStack<T extends Object> {

    private int stackSize;
    private T[] stackArr;
    private int top;
    
    
    @SuppressWarnings("unchecked")
    public MyGenericsStack(int size) {
        this.stackSize = size;
        this.stackArr = (T[]) new Object[stackSize];
        this.top = -1;
    }

    public void push(T entry){
        if(this.isStackFull()){
            System.out.println(("Stack is full. Increasing the capacity."));
            this.increaseStackCapacity();
        }
        System.out.println("Adding: "+entry);
        this.stackArr[++top] = entry;
    }

    /**
     * This method removes an entry from the
     * top of the stack.
     * @return
     * @throws Exception
     */
    public T pop() throws Exception {
        if(this.isStackEmpty()){
            throw new Exception("Stack is empty. Can not remove element.");
        }
        T entry = this.stackArr[top--];
        System.out.println("Removed entry: "+entry);
        return entry;
    }
    
    /**
     * This method returns top of the stack
     * without removing it.
     * @return
     */
    public T peek() {
        return stackArr[top];
    }

    private void increaseStackCapacity(){
        
        @SuppressWarnings("unchecked")
        T[] newStack = (T[]) new Object[this.stackSize*2];
        for(int i=0;i<stackSize;i++){
            newStack[i] = this.stackArr[i];
        }
        this.stackArr = newStack;
        this.stackSize = this.stackSize*2;
    }
    
    /**
     * This method returns true if the stack is
     * empty
     * @return
     */
    public boolean isStackEmpty() {
        return (top == -1);
    }

    /**
     * This method returns true if the stack is full
     * @return
     */
    public boolean isStackFull() {
        return (top == stackSize - 1);
    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote