Java Program for calculator

Program by Avinash Kumar Pandey

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
public class PopupCalculatorApplet extends JApplet implements ActionListener {
  public void init() {
    Button calcButton = new Button("Calculator");
    calcButton.addActionListener(this);
    Container contentPane = getContentPane();
    contentPane.add(calcButton);
  }
 
  public void actionPerformed(ActionEvent evt) {
    if (calc.isVisible())
      calc.setVisible(false);
    else
      calc.show();
  }
 
  private JFrame calc = new CalculatorFrame();
}
 
class CalculatorPanel extends JPanel implements ActionListener {
  public CalculatorPanel() {
    setLayout(new BorderLayout());
 
    display = new JTextField("0");
    display.setEditable(false);
    add(display, "North");
 
    JPanel p = new JPanel();
    p.setLayout(new GridLayout(4, 4));
    String buttons = "789/456*123-0.=+";
    for (int i = 0; i < buttons.length(); i++)
      addButton(p, buttons.substring(i, i + 1));
    add(p, "Center");
  }
 
  private void addButton(Container c, String s) {
    JButton b = new JButton(s);
    c.add(b);
    b.addActionListener(this);
  }
 
  public void actionPerformed(ActionEvent evt) {
    String s = evt.getActionCommand();
    if ('0' <= s.charAt(0) && s.charAt(0) <= '9' || s.equals(".")) {
      if (start)
        display.setText(s);
      else
        display.setText(display.getText() + s);
      start = false;
    } else {
      if (start) {
        if (s.equals("-")) {
          display.setText(s);
          start = false;
        } else
          op = s;
      } else {
        calculate(Double.parseDouble(display.getText()));
        op = s;
        start = true;
      }
    }
  }
 
  public void calculate(double n) {
    if (op.equals("+"))
      arg += n;
    else if (op.equals("-"))
      arg -= n;
    else if (op.equals("*"))
      arg *= n;
    else if (op.equals("/"))
      arg /= n;
    else if (op.equals("="))
      arg = n;
    display.setText("" + arg);
  }
 
  private JTextField display;
 
  private double arg = 0;
 
  private String op = "=";
 
  private boolean start = true;
}
 
class CalculatorFrame extends JFrame {
  public CalculatorFrame() {
    setTitle("Calculator");
    setSize(200, 200);
 
    Container contentPane = getContentPane();
    contentPane.add(new CalculatorPanel());
  }
}

4 Responses to “Java Program for calculator”

  1. Preeti Gupta

    not up to the mark answer
    can u give me a code which include only awt code without the use of swing

    Reply
  2. Buongiorno, mi sono licenziata dopo 21anni in un posto (contratto inet)erminatodper vari motivi naturalmente no gravi, per poi andare a fare una sostituzione di maternità che è durata dal 06/03/12 al 04/12/12 quindi con contratto determinato. A questo punto chiedo ho diritto alla disoccupazione?? Posso giá chiederla?? Mille grazie.

    Reply

Leave a Reply