2

I'm trying to make a GUI calculator. Everything seems good so far but how can I achieve the one like the built in windows calculator, where when you operate you see for ex. "100+2+9*4/8-3" so that whenever you click another + - / * button it will get solved in sequence. Pemdas? I think. I can manage to make the calculator function like that but it will only solve it in-order sequence because I only store 2 values, so I was wondering how I can store values of all current operations and their operator(+-/*)

Akuichi
  • 23
  • 4
  • 1
    See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson May 12 '17 at 17:10
  • 1
    Ohh a built in evaluator thanks for the new info! – Akuichi May 15 '17 at 06:51
  • You're welcome. :) Of course, if the point of your programming exercise is to understand **how to** do that process yourself, the script engine will be irrelevant. But if the main point is just to evaluate the expression, that'd be my 'go to' method. Why reinvent the wheel? – Andrew Thompson May 15 '17 at 07:03

1 Answers1

1

so I was wondering how I can store values of all current operations and their operator(+-/*)

Well, you could try storing each operation in an ArrayList, and place each operation in a class called Operation which has 3 variables number1, number2 and operation which could be an enum or a String value. I'd prefer the enum approach but I'm leaving that to you.

So, you could have this class:

import java.util.ArrayList;

public class PemdasSample {
    public static void main(String args[]) {
        ArrayList <Operation> operations = new ArrayList<Operation>();
        operations.add(new Operation(5, 5, "SUM"));
        operations.add(new Operation(2, 2, "REST"));
        operations.add(new Operation(3, 4, "MULTIPLY"));

        for (Operation operation : operations) {
            switch (operation.getOperation()) {
                case "SUM":
                    System.out.println(operation.getNumber1() + operation.getNumber2());
                    break;
                case "REST":
                    System.out.println(operation.getNumber1() - operation.getNumber2());
                    break;
                case "MULTIPLY":
                    System.out.println(operation.getNumber1() * operation.getNumber2());
                    break;
                default:
                    System.out.println("Operation not found");
                    break;
            }
        }
    }

    static class Operation {
        int number1;
        int number2;
        String operation;

        public Operation(int number1, int number2, String operation) {
            this.number1 = number1;
            this.number2 = number2;
            this.operation = operation;
        }

        public int getNumber1() {
            return number1;
        }

        public void setNumber1(int number1) {
            this.number1 = number1;
        }

        public int getNumber2() {
            return number2;
        }

        public void setNumber2(int number2) {
            this.number2 = number2;
        }

        public String getOperation() {
            return operation;
        }

        public void setOperation(String operation) {
            this.operation = operation;
        }
    }
}

Which produces this output:

10
0
12

You can take the logic (and separate the Operation class to be in its own file and not as an inner class as I did, not being static, etc.) and modify your program...

You didn't provide any code, so, if this approach doesn't work for you, explain why and provide a valid Minimal, Complete and Verifiable Example which demonstrates your issue.

Disclaimer

This code sample requires Java 7 to work because of the String switch, see this answer for more information.

I also didn't add the PEMDAS logic into the program as OP didn't provide any code, just helping with an idea on how to store operations.

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Hello, I managed to solve my problem, I learned about infix to postfix notation then evaluating it XD thanks for the help bro! – Akuichi May 15 '17 at 06:50