I created a calculator using Java swing.Frame creation and button arrangements were all set.I used ActionListener interface.And the program were execute successfully,when i performed a single operation along with two operands(eg:5+2). Working is success so far.But when I try to do multiple operations at same time with more than two operands(eg:10+2-5),it gives me 0.0 as output everytime.But I need my calculator to perform multiple operations at the same time .And should also follow the BODMAS rule.
I declared a float array named result and a char array named operation,as global variables.
public Calculator implements ActionListener{
float[] result=new float[10];
char[] operation=new char[10];
int i=0;
isOperationClicked=false;
Here is one example of operation button(like +,-,/,*) ,defined inside the actionPerformed function along with else if ladder.
else if(e.getSource()==plusbutton) {
isOperationClicked=true;
String oldValue=displaylabel.getText();
result=new float[10];
result[i]=Float.parseFloat(oldValue);
operation=new char[10];
operation[i]= '+';
i++;
}
Also equaltobotton is defined inside the actionPerformed function.Checking each operation symbols with ‘if’ on a ‘while loop’.Here i put one example.
else if(e.getSource()==equaltobutton) {
int m=0;
String finalvalue=displaylabel.getText();
float finalvalueF=Float.parseFloat(finalvalue);
result[i]=finalvalueF;
while(m<i) {
if(operation[m]=='/') {
float res=result[m]/result[m+1];
result=removeArrayF(result,m);
result[m]=res;
operation=removeArrayC(operation,m);
i--;m--;
}
m++;
}
For display the result
displaylabel.setText(result[0]+"");
This setText function is also include on the last line of equaltobutton's action
The removearrayF and the removearrayC are two methods defined to remove one element from the array. This is what i do .what are the changes i need to do for the proper output for multiple operations at same time?