Space for google add

Write a java program to implement a simple arithmetic calculator. Perform appropriate validations.

 


Write a java program to implement a simple arithmetic calculator. Perform appropriate validations.


calr.java 

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

class Calr extends JFrame implements ActionListener
{
static double a=0,b=0,result=0;
static int operator=0;
JTextField t1;
JPanel p1,p2;// p1=textbox,button
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,bclr;
Calr()
{
t1=new JTextField(17);
p1=new JPanel();
p2=new JPanel();
p2.setLayout(new GridLayout(4,4));
b0=new JButton("1");
b1=new JButton("2");
b2=new JButton("3");
b3=new JButton("+");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("-");
b8=new JButton("7");
b9=new JButton("8");
b10=new JButton("9");
b11=new JButton("*");
b12=new JButton("0");
b13=new JButton(".");
b14=new JButton("=");
b15=new JButton("/");
bclr=new JButton("Clear");

p2.add(b0);
b0.addActionListener(this);
p2.add(b1);
b1.addActionListener(this);
p2.add(b2);
b2.addActionListener(this);
p2.add(b3);
b3.addActionListener(this);
p2.add(b4);
b4.addActionListener(this);
p2.add(b5);
b5.addActionListener(this);
p2.add(b6);
b6.addActionListener(this);
p2.add(b7);
b7.addActionListener(this);
p2.add(b8);
b8.addActionListener(this);
p2.add(b9);
b9.addActionListener(this);
p2.add(b10);
b10.addActionListener(this);
p2.add(b11);
b11.addActionListener(this);
p2.add(b12);
b12.addActionListener(this);
p2.add(b13);
b13.addActionListener(this);
p2.add(b14);
b14.addActionListener(this);
p2.add(b15);
b15.addActionListener(this);
bclr.addActionListener(this);

setLayout(new BorderLayout());
p1.add(t1);p1.add(bclr);
add(p1,BorderLayout.NORTH);
add(p2);
setSize(300,400);
                setVisible(true);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b0)
t1.setText(t1.getText().concat("1"));
if(e.getSource()==b1)
t1.setText(t1.getText().concat("2"));
if(e.getSource()==b2)
t1.setText(t1.getText().concat("3"));
if(e.getSource()==b4)
t1.setText(t1.getText().concat("4"));
if(e.getSource()==b5)
t1.setText(t1.getText().concat("5"));
if(e.getSource()==b6)
t1.setText(t1.getText().concat("6"));
if(e.getSource()==b8)
t1.setText(t1.getText().concat("7"));
if(e.getSource()==b9)
t1.setText(t1.getText().concat("8"));
if(e.getSource()==b10)
t1.setText(t1.getText().concat("9"));
if(e.getSource()==b12)
t1.setText(t1.getText().concat("0"));
if(e.getSource()==b13)
t1.setText(t1.getText().concat("."));
if(e.getSource()==b3)
{
a=Double.parseDouble(t1.getText());
operator=1;
t1.setText("");
if(e.getSource()==b7)
{
a=Double.parseDouble(t1.getText());
operator=2;
t1.setText("");
}
if(e.getSource()==b11)
{
a=Double.parseDouble(t1.getText());
operator=3;
t1.setText("");
}
if(e.getSource()==b15)
{
a=Double.parseDouble(t1.getText());
operator=4;
t1.setText("");
}
if(e.getSource()==b14)
{
b=Double.parseDouble(t1.getText());
switch(operator)
{
case 1: result=a+b;
break;
case 2: result=a-b;
break;
case 3: result=a*b;
break;
case 4: result=a/b;
break;
default: result=0;
}
t1.setText(""+result);
}
if(e.getSource()==bclr)
t1.setText("");
}
public static void main(String arg[]) 
        {
            new Calr();     
        }
}


Post a Comment

0 Comments