You are on page 1of 8

Calculator

============================================

/* ================== API's ================= */

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

/* ============== CALCULATOR CLASS ============= */

class Calculator implements ActionListener{

//CONSTRUCTOR
public Calculator(){
f=new JFrame("Calculator");
f.setSize(350,350);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);

JMenuBar menu=new JMenuBar();


f.setJMenuBar(menu);
JMenu edit=new JMenu("Edit");
menu.add(edit);
JMenuItem copy=new JMenuItem("Copy Ctrl+C");
edit.add(copy);
JMenuItem paste=new JMenuItem("Paste Ctrl+V");
edit.add(paste);

JMenu view=new JMenu("View");


menu.add(view);
JMenuItem std=new JMenuItem("Standard");
view.add(std);
JMenuItem sci=new JMenuItem("Scientific");
view.add(sci);
JMenuItem dg=new JMenuItem("Digit Grouping");
view.add(dg);

JMenu help=new JMenu("Help");


menu.add(help);
JMenuItem ht=new JMenuItem("Help Topics");
help.add(ht);
JMenuItem cal=new JMenuItem("About Calculator");
help.add(cal);

//BORDER LAYOUT
f.setLayout(new BorderLayout(5,5));
p1=new JPanel();
bl= new BorderLayout();
p1.setLayout(bl);

//GRID LAYOUT ON WEST


p2=new JPanel();
p2.setLayout(new GridLayout(4,1));
UIManager.put("Button.background", Color.BITMASK);
UIManager.put("Button.foreground", Color.red);
JButton b[]=new JButton[27];
b1=new JButton("MC");
b2=new JButton("MR");
b3=new JButton("MS");
b4=new JButton("M+");

p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
f.add(p2,BorderLayout.WEST);

//GRID LAYOUT ON EAST


p3=new JPanel();
p3.setLayout(new GridLayout(4,1));
UIManager.put("Button.foreground", Color.blue);
b5=new JButton("sqrt");
b6=new JButton("%");
b7=new JButton("1/x");
UIManager.put("Button.foreground", Color.red);
b8=new JButton("=");
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(b8);
f.add(p3,BorderLayout.EAST);

//GRID LAYOUT ON NORTH


p4=new JPanel();
p4.setLayout(new GridLayout(1,3));
b9=new JButton("Backspace");
b10=new JButton("C");
b11=new JButton("CE");
tf1=new JTextField(26);
tf1.setHorizontalAlignment(JTextField.RIGHT);
p4.add(tf1);
p4.add(b9);
p4.add(b10);
p4.add(b11);
p1.add(p4,BorderLayout.CENTER);
p1.add(tf1,BorderLayout.NORTH);
f.add(p1,BorderLayout.NORTH);

//GRID LAYOUT ON CENTER


p5=new JPanel();
p5.setLayout(new GridLayout(4,4));
UIManager.put("Button.foreground", Color.blue);
b12=new JButton("7");
b13=new JButton("8");
b14=new JButton("9");
b16=new JButton("4");
b17=new JButton("5");
b18=new JButton("6");
b20=new JButton("1");
b21=new JButton("2");
b22=new JButton("3");
b24=new JButton("0");
b25=new JButton("+/-");
b26=new JButton(".");
UIManager.put("Button.foreground", Color.red);
b15=new JButton("/");
b27=new JButton("+");
b23=new JButton("-");
b19=new JButton("*");
p5.add(b12);
p5.add(b13);
p5.add(b14);
p5.add(b15);
p5.add(b16);
p5.add(b17);
p5.add(b18);
p5.add(b19);
p5.add(b20);
p5.add(b21);
p5.add(b22);
p5.add(b23);
p5.add(b24);
p5.add(b25);
p5.add(b26);
p5.add(b27);
f.add(p5,BorderLayout.CENTER);

/////////REGISTRATION OF EVENT//////////
b5.addActionListener(this);
b5.setActionCommand("sqrt");
b6.addActionListener(this);
b6.setActionCommand("%");
b7.addActionListener(this);
b7.setActionCommand("1/x");
b8.addActionListener(this);
b8.setActionCommand("=");
b10.addActionListener(this);
b10.setActionCommand("C");
b11.addActionListener(this);
b11.setActionCommand("CE");
b12.addActionListener(this);
b12.setActionCommand("7");
b13.addActionListener(this);
b13.setActionCommand("8");
b14.addActionListener(this);
b14.setActionCommand("9");
b15.addActionListener(this);
b15.setActionCommand("/");
b16.addActionListener(this);
b16.setActionCommand("4");
b17.addActionListener(this);
b17.setActionCommand("5");
b18.addActionListener(this);
b18.setActionCommand("6");
b19.addActionListener(this);
b19.setActionCommand("*");
b20.addActionListener(this);
b20.setActionCommand("1");
b21.addActionListener(this);
b21.setActionCommand("2");
b22.addActionListener(this);
b22.setActionCommand("3");
b23.addActionListener(this);
b23.setActionCommand("-");
b24.addActionListener(this);
b24.setActionCommand("0");
b25.addActionListener(this);
b25.setActionCommand("+/-");
b26.addActionListener(this);
b26.setActionCommand(".");
b27.addActionListener(this);
b27.setActionCommand("+");
tf1.setText("0.");

f.setVisible(true);
}
////////////EVENT LISTENER/HANDLER//////////
int num1=0,num2=0;
String Check;
StringBuilder sb=new StringBuilder();
public void actionPerformed(ActionEvent e){
int res;
String str=e.getActionCommand();

if(str.equals("0")){
sb.append("0");
tf1.setText(sb.toString());
}
else if(str.equals("1")){
sb.append("1");
tf1.setText(sb.toString());
}
else if(str.equals("2")){
sb.append("2");
tf1.setText(sb.toString());
}
else if(str.equals("3")){
sb.append("3");
tf1.setText(sb.toString());
}
else if(str.equals("4")){
sb.append("4");
tf1.setText(sb.toString());
}
else if(str.equals("5")){
sb.append("5");
tf1.setText(sb.toString());
}
else if(str.equals("6")){
sb.append("6");
tf1.setText(sb.toString());
}
else if(str.equals("7")){
sb.append("7");
tf1.setText(sb.toString());
}
else if(str.equals("8")){
sb.append("8");
tf1.setText(sb.toString());
}
else if(str.equals("9")){
sb.append("9");
tf1.setText(sb.toString());
}
else if(str.equals("+")){
num1=Integer.parseInt(tf1.getText());
Check="PLUS";
sb=new StringBuilder();
}
else if(str.equals("-")){
num1=Integer.parseInt(tf1.getText());
Check="MINUS";
sb=new StringBuilder();
}
else if(str.equals("*")){
num1=Integer.parseInt(tf1.getText());
Check="PRODUCT";
sb=new StringBuilder();
}
else if(str.equals("/")){
num1=Integer.parseInt(tf1.getText());
Check="DIVIDE";
sb=new StringBuilder();
}

else if(str.equals("=")){
num2=Integer.parseInt(tf1.getText());
if(Check.equals("PLUS")){
res=num1+num2;
tf1.setText(""+res);
sb=new StringBuilder();
}
else if(Check.equals("MINUS")){
res=num1-num2;
tf1.setText(""+res);
sb=new StringBuilder();
}
else if(Check.equals("PRODUCT")){
res=num1*num2;
tf1.setText(""+res);
sb=new StringBuilder();
}
else if(Check.equals("DIVIDE")){
res=num1/num2;
tf1.setText(""+res);
sb=new StringBuilder();
}
}
else if(str.equals("C")){
tf1.setText("0");
}
else if(str.equals("CE")){
tf1.setText("0");
}
else if(str.equals("1/x")){
float num3=Integer.parseInt(tf1.getText());
float num4=num3*num3;
float res1=num3/num4;
tf1.setText(""+res1);
sb=new StringBuilder();
}
else if(str.equals("+/-")){
num1=Integer.parseInt(tf1.getText());
res=num1*(-1);
tf1.setText(""+res);
sb=new StringBuilder();
}
else if(str.equals("sqrt")){
double num5=Integer.parseInt(tf1.getText());
double res2=Math.sqrt(num5);
tf1.setText(""+res2);
sb=new StringBuilder();
}
else if(str.equals("%")){
float num6=Integer.parseInt(tf1.getText());
float num7=Integer.parseInt(tf1.getText());
float res3=(num6/100)*num7;
tf1.setText(""+res3);
sb=new StringBuilder();
}
else if(str.equals(".")){
double num8=Integer.parseInt(tf1.getText());
double res4=num8;
tf1.setText(""+res4+".");
sb=new StringBuilder();
}
}

//PRIVATE MEMBERS
private JFrame f;
private JTextField tf1;
private BorderLayout bl;
private JPanel p1,p2,p3,p4,p5;
private JButton
b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,
b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27;
}
//////////////////////CLIENT PROGRAM///////////////////
class Main{
public static void main(String a[]){
Calculator cal=new Calculator();
}
}

http://www.ravianeducation.blogspot.com
FARHAN: 03008855006

You might also like