Simple Calculator in Java

Here's the screenshot below:
It can only perform one operation at a time.

import java.lang.*;
import java.awt.*;
import java.awt.event.*;
 
public class calculator extends 
 Frame implements ActionListener { 
        
    Button btn1 = new Button("1");
    Button btn2 = new Button("2");
    Button btn3 = new Button("3");
    Button plus = new Button("+");
    
    Button btn4 = new Button("4");
    Button btn5 = new Button("5");
    Button btn6 = new Button("6");
    Button minus = new Button("-");
    
    Button btn7 = new Button("7");
    Button btn8 = new Button("8");
    Button btn9 = new Button("9");
    Button times = new Button("*");
   
    Button btn_clr = new Button("C"); 
    Button btn0 = new Button("0");
    Button divide = new Button("/");
    Button ekwals = new Button("=");

    TextField txt = new TextField(20);

    
    String str_number = "";
    int operation = 0;
    double number1 = 0;
    double number2 = 0;
    double result = 0;
    String[] str = null;
    
    public calculator() {
          
        Panel base = new Panel();
        Panel NumberPanel = new Panel();
        
        base.setLayout(new BorderLayout());
        NumberPanel.setLayout(new GridLayout(4,4));
        
        NumberPanel.add(btn1);
        btn1.addActionListener(this);
        NumberPanel.add(btn2);
        btn2.addActionListener(this);
        NumberPanel.add(btn3);
        btn3.addActionListener(this);
        NumberPanel.add(plus);
        plus.addActionListener(this);
        NumberPanel.add(btn4);
        btn4.addActionListener(this);
        NumberPanel.add(btn5);
        btn5.addActionListener(this);
        NumberPanel.add(btn6);
        btn6.addActionListener(this);
        NumberPanel.add(minus);
        minus.addActionListener(this);
        NumberPanel.add(btn7);
        btn7.addActionListener(this);
        NumberPanel.add(btn8);
        btn8.addActionListener(this);
        NumberPanel.add(btn9);
        btn9.addActionListener(this);
        NumberPanel.add(times);
        times.addActionListener(this);
        NumberPanel.add(btn0);
        btn0.addActionListener(this);
        NumberPanel.add(btn_clr);
        btn_clr.addActionListener(this);
        NumberPanel.add(ekwals);
        ekwals.addActionListener(this);
        NumberPanel.add(divide);
        divide.addActionListener(this);    
        
        txt.setEditable(false);             
        txt.setBackground(Color.black);
        txt.setForeground(Color.green);
        base.add(NumberPanel,BorderLayout.CENTER);
        base.add(txt,BorderLayout.NORTH);
        add(base,BorderLayout.CENTER);        
    }
    
    public void actionPerformed(ActionEvent e) {
        
    if(e.getSource()==btn1) {
           txt.setText("1");
           str_number+=txt.getText();
           txt.setText(str_number); }
    else if(e.getSource()==btn2) {
           txt.setText("2");
           str_number+=txt.getText();
           txt.setText(str_number); }
    else if(e.getSource()==btn3) {
           txt.setText("3");
           str_number+=txt.getText();
           txt.setText(str_number); }
    else if(e.getSource()==btn4) {
           txt.setText("4");
           str_number+=txt.getText();
           txt.setText(str_number); }
    else if(e.getSource()==btn5) {
           txt.setText("5");
           str_number+=txt.getText();
           txt.setText(str_number); }
    else if(e.getSource()==btn6) {
           txt.setText("6");
           str_number+=txt.getText();
           txt.setText(str_number); }
    else if(e.getSource()==btn7) {
           txt.setText("7");
           str_number+=txt.getText();
           txt.setText(str_number); }
    else if(e.getSource()==btn8) {
           txt.setText("8");
           str_number+=txt.getText();
           txt.setText(str_number); }
    else if(e.getSource()==btn9) {
           txt.setText("9");
           str_number+=txt.getText();
           txt.setText(str_number); }
    else if(e.getSource()==btn0) {
           txt.setText("0");
           str_number+=txt.getText();
           txt.setText(str_number); }
     else if(e.getSource()==plus) {
            if(operation==0 & str_number!="") {
            number1=Integer.parseInt(str_number);
            txt.setText("+");
            str_number+=txt.getText();
            txt.setText(str_number);
            operation=1;
            }
            else {txt.setText(str_number);}
            }
     else if(e.getSource()==minus) {
         if(operation==0 & str_number!="") {
            number1=Integer.parseInt(str_number);
             txt.setText("-");
             str_number+=txt.getText();
             txt.setText(str_number);
             operation=2;
             }
             else { txt.setText(str_number); }
             }
     else if(e.getSource()==times) {
         if(operation==0 & str_number!="") {
            number1=Integer.parseInt(str_number);
             txt.setText("*");
             str_number+=txt.getText();
             txt.setText(str_number);
             operation=3;
             }
             else { txt.setText(str_number); }
             }
     else if(e.getSource()==divide) {
         if(operation==0 & str_number!="") {
            number1=Integer.parseInt(str_number);
             txt.setText("/");
             str_number+=txt.getText();
             txt.setText(str_number);
             operation=4;
             }
             else { txt.setText(str_number); }
             }
     else if(e.getSource()==ekwals) {
         if(operation!=0 & str_number!="") {
             txt.setText("=");
               str_number+=txt.getText();
               txt.setText(str_number);
     
     switch(operation) {
             case 1: {
   str = str_number.split("\\+");
                 number2=Integer.parseInt(str[1].replace("=",""));
                 result=number1+number2;
                 txt.setText(Double.toString(result));
                 break;
             }
             case 2: {
                 str = str_number.split("\\-");
                 number2=Integer.parseInt(str[1].replace("=",""));
                 result=number1-number2;
                 txt.setText(Double.toString(result));
                 break;
             }
             case 3: {
                 str = str_number.split("\\*");
                 number2=Integer.parseInt(str[1].replace("=",""));
                 result=number1*number2;
                 txt.setText(Double.toString(result));
                 break;
             }
             case 4: {     
                 str = str_number.split("\\/");
                 number2=Integer.parseInt(str[1].replace("=",""));
                 result=number1/number2;
                 txt.setText(Double.toString(result));
                 break;
             }
             }
             btn0.setEnabled(false);
             btn1.setEnabled(false);
             btn2.setEnabled(false);
             btn3.setEnabled(false);
             btn4.setEnabled(false);
             btn5.setEnabled(false);
             btn6.setEnabled(false);
             btn7.setEnabled(false);
             btn8.setEnabled(false);
             btn9.setEnabled(false);
             plus.setEnabled(false);
             minus.setEnabled(false);
             times.setEnabled(false);
             divide.setEnabled(false);
             ekwals.setEnabled(false);
         }
         else { txt.setText(""); }
     }
     else if(e.getSource()==btn_clr) {
         txt.setText("");
         str_number = "";
         operation = 0;
         number1 = 0;
         number2 = 0;
         result = 0;
        btn0.setEnabled(true);
        btn1.setEnabled(true);
        btn2.setEnabled(true);
        btn3.setEnabled(true);
        btn4.setEnabled(true);
        btn5.setEnabled(true);
        btn6.setEnabled(true);
        btn7.setEnabled(true);
        btn8.setEnabled(true);
        btn9.setEnabled(true);
        plus.setEnabled(true);
        minus.setEnabled(true);
        times.setEnabled(true);
        divide.setEnabled(true);
        ekwals.setEnabled(true);
     }
}
    
    public static void main(String[] args) {        
        calculator calc = new calculator();
        calc.setVisible(true);
        calc.setSize(200,200);
        calc.setResizable(false);
        calc.setTitle("Calculator");        
    }
}

If you have any question about the code, just comment below. :)

Popular posts from this blog

Set table width to 100% on PDF export in Datatables

Add/Subtract Single Digit Number in Assembly Language