Calculator in java
Calculator
Details:
Sample Calculation:
617 + 912 = 1529
50.2 + 0.2 = 50.4
900 – 700 =200
11 * 2 = 22
2 0/ 2 = 10
Solution:
Part-1
import java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
private JTextField tf;
private JButton off,ac,pow;
private JButton mp,mm,mc,mr,add,sub,malti,div;
private JButton one,two,three,four,five, six,seven,eight,nine,zero,point,equ;
private JPanel panel;
private double number1=0,number2=0,res=0;
private int status=0;
public Calculator()
{
super("Calculator");
this.setSize(440,470);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.black);
tf = new JTextField();
tf.setBounds(15,25,370,45);
tf.setEditable(false);
tf.setBackground(Color.cyan);
panel.add(tf);
off = new JButton("OFF");
off.setBounds(15,79,120,45);
off.setBackground(Color.red);
off.addActionListener(this);
panel.add(off);
ac = new JButton("AC");
ac.setBounds(156,79,120,45);
ac.setBackground(Color.white);
ac.addActionListener(this);
panel.add(ac);
pow = new JButton("<");
pow.setBounds(300,79,83,45);
pow.setBackground(Color.white);
pow.addActionListener(this);
panel.add(pow);
mp = new JButton("M+");
mp.setBounds(15,130,55,45);
mp.setBackground(Color.blue);
panel.add(mp);
seven = new JButton("7");
seven.setBounds(78,130,55,45);
seven.setBackground(Color.white);
seven.addActionListener(this);
panel.add(seven);
eight = new JButton("8");
eight.setBounds(156,130,55,45);
eight.setBackground(Color.white);
eight.addActionListener(this);
panel.add(eight);
nine = new JButton("9");
nine.setBounds(216,130,55,45);
nine.setBackground(Color.white);
nine.addActionListener(this);
panel.add(nine);
div = new JButton("/");
div.setBounds(300,130,83,45);
div.setBackground(Color.yellow);
div.addActionListener(this);
panel.add(div);
mm = new JButton("M-");
mm.setBounds(15,180,55,45);
mm.setBackground(Color.blue);
panel.add(mm);
four = new JButton("4");
four.setBounds(78,180,55,45);
four.setBackground(Color.white);
four.addActionListener(this);
panel.add(four);
five = new JButton("5");
five.setBounds(156,180,55,45);
five.setBackground(Color.white);
five.addActionListener(this);
panel.add(five);
six= new JButton("6");
six.setBounds(216,180,55,45);
six.setBackground(Color.white);
six.addActionListener(this);
panel.add(six);
malti = new JButton("*");
malti.setBounds(300,180,83,45);
malti.setBackground(Color.yellow);
malti.addActionListener(this);
panel.add(malti);
mc = new JButton("MC");
mc.setBounds(15,230,55,45);
mc.setBackground(Color.blue);
panel.add(mc);
one = new JButton("1");
one.setBounds(78,230,55,45);
one.setBackground(Color.white);
one.addActionListener(this);
panel.add(one);
two = new JButton("2");
two.setBounds(156,230,55,45);
two.setBackground(Color.white);
two.addActionListener(this);
panel.add(two);
three= new JButton("3");
three.setBounds(216,230,55,45);
three.setBackground(Color.white);
three.addActionListener(this);
panel.add(three);
sub = new JButton("-");
sub.setBounds(300,230,83,45);
sub.setBackground(Color.yellow);
sub.addActionListener(this);
panel.add(sub);
//
mr = new JButton("MR");
mr.setBounds(15,280,55,45);
mr.setBackground(Color.blue);
mr.addActionListener(this);
panel.add(mr);
zero = new JButton("0");
zero.setBounds(78,280,55,45);
zero.setBackground(Color.white);
zero.addActionListener(this);
panel.add(zero);
point = new JButton(".");
point.setBounds(156,280,55,45);
point.setBackground(Color.white);
point.addActionListener(this);
panel.add(point);
equ= new JButton("=");
equ.setBounds(216,280,55,45);
equ.setBackground(Color.white);
equ.addActionListener(this);
panel.add(equ);
add = new JButton("+");
add.setBounds(300,280,83,45);
add.setBackground(Color.yellow);
add.addActionListener(this);
panel.add(add);
this.add(panel);
}
/*
public void mouseClicked(MouseEvent me){}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public void mouseExited(MouseEvent me) {}*/
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==zero)
{
tf.setText(tf.getText()+"0");
}
if(ae.getSource()==one)
{
tf.setText(tf.getText()+("1"));
}
if(ae.getSource()==two)
{
tf.setText(tf.getText()+("2"));
}
if(ae.getSource()==three)
{
tf.setText(tf.getText()+("3"));
}
if(ae.getSource()==four)
{
tf.setText(tf.getText()+("4"));
}
if(ae.getSource()==five)
{
tf.setText(tf.getText()+("5"));
}
if(ae.getSource()==six)
{
tf.setText(tf.getText()+("6"));
}
if(ae.getSource()==seven)
{
tf.setText(tf.getText()+("7"));
}
if(ae.getSource()==eight)
{
tf.setText(tf.getText()+("8"));
}
if(ae.getSource()==nine)
{
tf.setText(tf.getText()+("9"));
}
if(ae.getSource()==point)
{
tf.setText(tf.getText()+("."));
}
if(ae.getSource()==off)
{
System.exit(0);
}
if(ae.getSource()==ac)
{
tf.setText("");
}
if(ae.getSource()==pow)
{
String s=tf.getText();
tf.setText("");
for(int i=0;i<s.length()-1;i++)
{
tf.setText(tf.getText()+s.charAt(i));
}
}
if(ae.getSource()==add)
{
number2=Double.parseDouble(tf.getText());
status=1;
tf.setText("");
}
if(ae.getSource()==sub)
{
number2=Double.parseDouble(tf.getText());
status=2;
tf.setText("");
}
if(ae.getSource()==malti)
{
number2=Double.parseDouble(tf.getText());
status=3;
tf.setText("");
}
if(ae.getSource()==div)
{
number2=Double.parseDouble(tf.getText());
status=4;
tf.setText("");
}
if(ae.getSource()==equ)
{
number1=Double.parseDouble(tf.getText());
if(status == 1)
{
res = number2+number1;
}
else if(status == 2)
{
res = number1-number2;
}
else if(status ==3)
{
res = number1*number2;
}
else if(status == 4)
{
res = number1/number2;
}
tf.setText(""+res);
}
}
}
Pimport java.lang.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
private JTextField tf;
private JButton off,ac,pow;
private JButton mp,mm,mc,mr,add,sub,malti,div;
private JButton one,two,three,four,five, six,seven,eight,nine,zero,point,equ;
private JPanel panel;
private double number1=0,number2=0,res=0;
private int status=0;
public Calculator()
{
super("Calculator");
this.setSize(440,470);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.black);
tf = new JTextField();
tf.setBounds(15,25,370,45);
tf.setEditable(false);
tf.setBackground(Color.cyan);
panel.add(tf);
off = new JButton("OFF");
off.setBounds(15,79,120,45);
off.setBackground(Color.red);
off.addActionListener(this);
panel.add(off);
ac = new JButton("AC");
ac.setBounds(156,79,120,45);
ac.setBackground(Color.white);
ac.addActionListener(this);
panel.add(ac);
pow = new JButton("<");
pow.setBounds(300,79,83,45);
pow.setBackground(Color.white);
pow.addActionListener(this);
panel.add(pow);
mp = new JButton("M+");
mp.setBounds(15,130,55,45);
mp.setBackground(Color.blue);
panel.add(mp);
seven = new JButton("7");
seven.setBounds(78,130,55,45);
seven.setBackground(Color.white);
seven.addActionListener(this);
panel.add(seven);
eight = new JButton("8");
eight.setBounds(156,130,55,45);
eight.setBackground(Color.white);
eight.addActionListener(this);
panel.add(eight);
nine = new JButton("9");
nine.setBounds(216,130,55,45);
nine.setBackground(Color.white);
nine.addActionListener(this);
panel.add(nine);
div = new JButton("/");
div.setBounds(300,130,83,45);
div.setBackground(Color.yellow);
div.addActionListener(this);
panel.add(div);
mm = new JButton("M-");
mm.setBounds(15,180,55,45);
mm.setBackground(Color.blue);
panel.add(mm);
four = new JButton("4");
four.setBounds(78,180,55,45);
four.setBackground(Color.white);
four.addActionListener(this);
panel.add(four);
five = new JButton("5");
five.setBounds(156,180,55,45);
five.setBackground(Color.white);
five.addActionListener(this);
panel.add(five);
six= new JButton("6");
six.setBounds(216,180,55,45);
six.setBackground(Color.white);
six.addActionListener(this);
panel.add(six);
malti = new JButton("*");
malti.setBounds(300,180,83,45);
malti.setBackground(Color.yellow);
malti.addActionListener(this);
panel.add(malti);
mc = new JButton("MC");
mc.setBounds(15,230,55,45);
mc.setBackground(Color.blue);
panel.add(mc);
one = new JButton("1");
one.setBounds(78,230,55,45);
one.setBackground(Color.white);
one.addActionListener(this);
panel.add(one);
two = new JButton("2");
two.setBounds(156,230,55,45);
two.setBackground(Color.white);
two.addActionListener(this);
panel.add(two);
three= new JButton("3");
three.setBounds(216,230,55,45);
three.setBackground(Color.white);
three.addActionListener(this);
panel.add(three);
sub = new JButton("-");
sub.setBounds(300,230,83,45);
sub.setBackground(Color.yellow);
sub.addActionListener(this);
panel.add(sub);
//
mr = new JButton("MR");
mr.setBounds(15,280,55,45);
mr.setBackground(Color.blue);
mr.addActionListener(this);
panel.add(mr);
zero = new JButton("0");
zero.setBounds(78,280,55,45);
zero.setBackground(Color.white);
zero.addActionListener(this);
panel.add(zero);
point = new JButton(".");
point.setBounds(156,280,55,45);
point.setBackground(Color.white);
point.addActionListener(this);
panel.add(point);
equ= new JButton("=");
equ.setBounds(216,280,55,45);
equ.setBackground(Color.white);
equ.addActionListener(this);
panel.add(equ);
add = new JButton("+");
add.setBounds(300,280,83,45);
add.setBackground(Color.yellow);
add.addActionListener(this);
panel.add(add);
this.add(panel);
}
/*
public void mouseClicked(MouseEvent me){}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
public void mouseExited(MouseEvent me) {}*/
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==zero)
{
tf.setText(tf.getText()+"0");
}
if(ae.getSource()==one)
{
tf.setText(tf.getText()+("1"));
}
if(ae.getSource()==two)
{
tf.setText(tf.getText()+("2"));
}
if(ae.getSource()==three)
{
tf.setText(tf.getText()+("3"));
}
if(ae.getSource()==four)
{
tf.setText(tf.getText()+("4"));
}
if(ae.getSource()==five)
{
tf.setText(tf.getText()+("5"));
}
if(ae.getSource()==six)
{
tf.setText(tf.getText()+("6"));
}
if(ae.getSource()==seven)
{
tf.setText(tf.getText()+("7"));
}
if(ae.getSource()==eight)
{
tf.setText(tf.getText()+("8"));
}
if(ae.getSource()==nine)
{
tf.setText(tf.getText()+("9"));
}
if(ae.getSource()==point)
{
tf.setText(tf.getText()+("."));
}
if(ae.getSource()==off)
{
System.exit(0);
}
if(ae.getSource()==ac)
{
tf.setText("");
}
if(ae.getSource()==pow)
{
String s=tf.getText();
tf.setText("");
for(int i=0;i<s.length()-1;i++)
{
tf.setText(tf.getText()+s.charAt(i));
}
}
if(ae.getSource()==add)
{
number2=Double.parseDouble(tf.getText());
status=1;
tf.setText("");
}
if(ae.getSource()==sub)
{
number2=Double.parseDouble(tf.getText());
status=2;
tf.setText("");
}
if(ae.getSource()==malti)
{
number2=Double.parseDouble(tf.getText());
status=3;
tf.setText("");
}
if(ae.getSource()==div)
{
number2=Double.parseDouble(tf.getText());
status=4;
tf.setText("");
}
if(ae.getSource()==equ)
{
number1=Double.parseDouble(tf.getText());
if(status == 1)
{
res = number2+number1;
}
else if(status == 2)
{
res = number1-number2;
}
else if(status ==3)
{
res = number1*number2;
}
else if(status == 4)
{
res = number1/number2;
}
tf.setText(""+res);
}
}
}
Part-2
import java.lang.*;
public class Start
{
public static void main(String args[])
{
Calculator c =new Calculator();
c.setVisible(true);
}
}
Get this Calculator
No comments