Saturday, August 20, 2016

Program for calculator in JAVA

This is the source code for a calculater in java
import java.io.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;

public class Calculator extends JApplet implements ActionListener
{
int choice;
double a,b, result = 0;
String str;
JTextField jt;
JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
JButton btnAdd, btnSub, btnMul, btnDiv, btnEqual, btnDot, btnCl;
JPanel pn1, pn2;
Container cp;
public void init() // Initializing Applet & its Components
{
cp = getContentPane();
cp.setLayout ( new BorderLayout() );
jt = new JTextField(10);
jt.setHorizontalAlignment(4);
pn1 = new JPanel();
pn2 = new JPanel();
pn1.setLayout ( new FlowLayout() );
pn2.setLayout ( new GridLayout(4, 4) );
btnCl = new JButton ("Clear");
btn0 = new JButton ("0");
btn1 = new JButton ("1");
btn2 = new JButton ("2");
btn3 = new JButton ("3");
btn4 = new JButton ("4");
btn5 = new JButton ("5");
btn6 = new JButton ("6");
btn7 = new JButton ("7");
btn8 = new JButton ("8");
btn9 = new JButton ("9");
btnAdd = new JButton ("+");
btnSub = new JButton ("-");
btnMul = new JButton ("*");
btnDiv = new JButton ("/");
btnEqual = new JButton ("=");
btnDot = new JButton (".");
addButtons();
addListeners();
pn1.add (jt);
pn1.add (btnCl);
cp.add (pn1, BorderLayout.NORTH);
cp.add (pn2, BorderLayout.CENTER);
jt.setText("");
}

public void addListeners() // Adding Listeners to all Components
{
btn0.addActionListener ( this );
btn1.addActionListener ( this );
btn2.addActionListener ( this );
btn3.addActionListener ( this );
btn4.addActionListener ( this );
btn5.addActionListener ( this );
btn6.addActionListener ( this );
btn7.addActionListener ( this );
btn8.addActionListener ( this );
btn9.addActionListener ( this );
btnDot.addActionListener ( this );
btnCl.addActionListener ( this );
btnAdd.addActionListener ( this );
btnSub.addActionListener ( this );
btnMul.addActionListener ( this );
btnDiv.addActionListener ( this );
btnEqual.addActionListener ( this );
}

public void addButtons() // Adding all Components to JPanel
{
pn2.add ( btn7 );
pn2.add ( btn8 );
pn2.add ( btn9 );
pn2.add ( btnDiv );
pn2.add ( btn4 );
pn2.add ( btn5 );
pn2.add ( btn6 );
pn2.add ( btnMul );
pn2.add ( btn1 );
pn2.add ( btn2 );
pn2.add ( btn3 );
pn2.add ( btnSub );
pn2.add ( btn0 );
pn2.add ( btnDot );
pn2.add ( btnAdd );
pn2.add ( btnEqual );
}
public void operation(double a, double b, int op)
{
switch ( choice )
{
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 :
{ jt.setText("0"); break; }
}
}

public void actionPerformed(ActionEvent e) // Actions to be performed
{ // whenever event is triggered
try
{
str = (String)e.getActionCommand();
if ( str = = "+" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 1;
}
else if ( str = = "-" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 2;
}
else if ( str = = "*" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 3;
}
else if ( str = = "/" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 4;
}
else if ( str = = "=" )
{
b = Double.parseDouble (jt.getText());
operation(a, b, choice);
jt.setText("");
System.out.println(result);
}
else if ( str = = "Clear" )
{
jt.setText("");
choice = 0;
a = b = result = 0;
}
else
{
jt.setText( jt.getText() +str );
}
}
catch(Exception ex)
{
jt.setText("Invalid Operation");
System.out.println("Invalid Operation");
}
}
}

//<applet code="Calculator.class" width=200 height=200></applet>

Related Post:

0 comments:

Post a Comment

 
Copyright 2009 Information Blog
Powered By Blogger