Code:import java.awt.*;
//import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener{
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
// this array initializes the numeric buttons 0-9
static final JButton[] numbers ={new JButton("0"),new JButton("1"),new JButton("2"),new JButton("3"),
new JButton("4"),new JButton("5"),new JButton("6"),new JButton("7"),
new JButton("8"),new JButton("9")};
static final JButton dot=new JButton(".");
static final JButton unary_plus_minus=new JButton("+-");
static final JButton plus=new JButton("+");
static final JButton minus=new JButton("-");
static final JButton star=new JButton("*");
static final JButton divide=new JButton("/");
static final JButton square_root=new JButton("sqr"); // square root of a number
static final JButton modulo=new JButton("%"); // remainder of a no divided by another
static final JButton inverse=new JButton("1/x"); // reciprocal of a number
static final JButton equals=new JButton("=");
// decimal, binary, octal, clear and exit buttons
static final JButton dec=new JButton("Dec");
static final JButton bin=new JButton("Bin");//disable performing operations in dec and oct mode, switch to octal mode
// perform add and subtraction in binary
static final JButton oct=new JButton("Oct"); //disable performing operations in dec and bin mode, switch to octal mode
// perform add and subtraction in octal only
static final JButton clear=new JButton("C"); // clear the calculator screen
static final JButton close=new JButton("Exit"); // close the application
static final JButton mem_read=new JButton("MR"); // recall what is stored in memory
static final JButton mem_clear=new JButton("MC"); // cancel value in memory
static final JButton mem_store=new JButton("MS"); // store value in memory
static final JButton mem_add=new JButton("M+"); // add to what is stored in memory
static JTextField display= new JTextField(20);// calculator screen
public Calculator() {
panel1.setLayout(new FlowLayout(FlowLayout.LEFT,6,4)); // panel to store the numeric and
int i; // arithmetic operators
for (i=1;i<4;i++) panel1.add(numbers[i]); //add 1-3 number buttons to panel1
panel1.add(plus);
for (i=4;i<7;i++) panel1.add(numbers[i]); //add 4-6 number buttons to panel1
panel1.add(minus);
for (i=7;i<10;i++) panel1.add(numbers[i]); //add 7-9 number buttons to panel1
panel1.add(star);
panel1.add(numbers[0]); //add 0 button to panel1
panel1.add(unary_plus_minus);
panel1.add(dot);
panel1.add(divide);
// south
panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
panel2.add(dec);
panel2.add(bin);
panel2.add(oct);
panel2.add(clear);
panel2.add(close);
//west
panel3.setLayout(new GridLayout(4,1));
panel3.add(mem_clear);
panel3.add(mem_read);
panel3.add(mem_store);
panel3.add(mem_add);
//east
panel4.setLayout(new GridLayout(4,1));
panel4.add(square_root);
panel4.add(modulo);
panel4.add(inverse);
panel4.add(equals);
panel5.setLayout(new BorderLayout());
panel5.add(display,BorderLayout.NORTH);
panel5.add(panel3,BorderLayout.WEST);
panel5.add(panel4,BorderLayout.EAST);
panel5.add(panel1,BorderLayout.CENTER);
panel5.add(panel2,BorderLayout.SOUTH);
getContentPane().add(panel5);
// register the buttons
for (i=0;i<10;i++) numbers[i].addActionListener(this); // this registers listener for buttons 0-9
close.addActionListener(this);
clear.addActionListener(this);
dec.addActionListener(this);
bin.addActionListener(this);
oct.addActionListener(this);
//
mem_clear.addActionListener(this);
mem_read.addActionListener(this);
mem_store.addActionListener(this);
mem_add.addActionListener(this);
//
square_root.addActionListener(this);
modulo.addActionListener(this);
equals.addActionListener(this);
inverse.addActionListener(this);
//
unary_plus_minus.addActionListener(this);
plus.addActionListener(this);
minus.addActionListener(this);
star.addActionListener(this);
divide.addActionListener(this);
square_root.addActionListener(this);
// ...
// complete the rest
}
public void actionPerformed(ActionEvent e){
// write the event handlers for the remaining controls here
if(e.getSource()==close) System.exit(0);
else if(e.getSource()==clear) display.setText(" ");
//else if(e.getActionCommand()=="0") display.setText("0");
else if(e.getSource()==inverse){ // this button computes inverse
float x= Float.parseFloat(display.getText().trim()); //convert string to float
if (x!=0) x=1/x;
display.setText(Float.toString(x));
}
else display.setText(display.getText()+e.getActionCommand()); // event handler for buttons 0-9
// .. complete the rest
}
public static void main(String[] args) {
Calculator c1 = new Calculator();
c1.setSize(320, 215);
c1.setTitle("Calculator");
c1.setVisible(true);
c1.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

