Java calculator using stack

Page 1 of 1

1 Replies - 1191 Views - Last Post: 21 May 2014 - 06:54 AM Rate Topic: -----

#1 cukunit0   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-April 14

Java calculator using stack

Posted 21 May 2014 - 06:42 AM

my assignment is to design a simple GUI calculator using the stack data structure to perform additions, subtractions, multiplications and divisions.
but i having error while i press the action there.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.Stack;

public class JCalculator implements ActionListener {
JFrame jfrm;
JFrame jfrm2;
JTextField txt;
JLabel results;
String str = "";
Stack operands = new Stack();
char[] a = new char[0];
int used = 0;



JCalculator(){
    jfrm = new JFrame("JCalc");
    jfrm.getContentPane().setLayout(new GridLayout(0,1));
    jfrm.setSize(210,210);

   results = new JLabel("",SwingConstants.RIGHT);
   jfrm.getContentPane().add(results);
   jfrm.setLocation(400,300);
   jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   JButton[] calbut= new JButton[14];
   String []but = {"7","8","9","/","4","5","6","*","1","2","3","-","0","C"
   };
   JButton equal = new JButton("=");
   JButton add = new JButton("+");

   JPanel jbutton = new JPanel();
   jbutton.setLayout(new GridLayout(4,3));
   for(int i=0; i< but.length;i++){
       jbutton.add(calbut[i] = new JButton(but[i]));

       calbut[i].addActionListener(this);
   }
   jbutton.add(equal);
   jbutton.add(add);

   equal.addActionListener(this);
   add.addActionListener(this);

   jfrm.getContentPane().add(jbutton);


    jfrm.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
    char[] a = new char[0];
    int used = 0;
    if(used == a.length){
        char[] newa= new char[a.length + 1];
        for(int i = 0; i <used; i++) newa[i]= a[i];
        a= newa;
    }

    if(ae.getActionCommand().equals("1")){
        str = results.getText();
        results.setText(str + "1");
        operands.push(1);
    }
    if(ae.getActionCommand().equals("2")){
        str = results.getText();
        results.setText(str + "2");
        operands.push(2);
    }
    if(ae.getActionCommand().equals("3")){
        str = results.getText();
        results.setText(str + "3");
        operands.push(3);
    }
    if(ae.getActionCommand().equals("4")){
        str = results.getText();
        results.setText(str + "4");
        operands.push(4);
    }
    if(ae.getActionCommand().equals("5")){
        str = results.getText();
        results.setText(str + "5");
        operands.push(5);
    }
    if(ae.getActionCommand().equals("6")){
        str = results.getText();
        results.setText(str + "6");
        operands.push(6);
    }
    if(ae.getActionCommand().equals("7")){
        str = results.getText();
        results.setText(str + "7");
        operands.push(7);
    }
    if(ae.getActionCommand().equals("8")){
        str = results.getText();
        results.setText(str + "8");
        operands.push(8);
    }
    if(ae.getActionCommand().equals("9")){
        str = results.getText();
        results.setText(str + "9");
        operands.push(9);
    }
    if (ae.getActionCommand().equals("0")) {
        str = results.getText();
        results.setText(str + "0");
        operands.push(0);
    }
    if(ae.getActionCommand().equals("+")){
        str = results.getText();
        double operand = Double.parseDouble(str);
        operands.push(operand);
        results.setText("");
        a[used]= '+';
        used++;

    }
    if(ae.getActionCommand().equals("-")){
        str = results.getText();
        double operand = Double.parseDouble(str);
        operands.push(operand);
        results.setText("");
        a[used]= '+';
        used++;
    }
    if(ae.getActionCommand().equals("/")){
        str = results.getText();
        double operand = Double.parseDouble(str);
        operands.push(operand);
        results.setText("");
        a[used]= '+';
        used++;
    }
    if(ae.getActionCommand().equals("*")){
        str = results.getText();
        double operand = Double.parseDouble(str);
        operands.push(operand);
        results.setText("");
        a[used]= '*';
        used++;
    }
    if(ae.getActionCommand().equals("=")){

        for(int i = 0; i< used; i++){
           performBinaryOp(a[i]);
        }

        str = String.valueOf(operands.pop());
        results.setText(str);
    }


}
 public void performBinaryOp(char nextOperation) {
     double leftOperand, rightOperand;
    Double result = new Double(0);
    rightOperand = (Double) operands.pop();
    leftOperand = (Double) operands.pop();
    switch (nextOperation) {
        case '+':
            result = new Double(leftOperand + rightOperand);
            break;
        case '-':
            result = new Double(leftOperand - rightOperand);
            break;
        case '*':
            result = new Double(leftOperand * rightOperand);
            break;
        case '/':
            result = new Double(leftOperand / rightOperand);
            break;
    }
    operands.push(result);
}

public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new JCalculator();                
        }            
    });        
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Java calculator using stack

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15486
  • View blog
  • Posts: 62,030
  • Joined: 12-June 08

Re: Java calculator using stack

Posted 21 May 2014 - 06:54 AM

Do not create duplicate topics. Closing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1