Write a Java program to illustrate basic calculator using grid layout manager.

 //calculator.html


/*<html >

<applet code="calculator" width =1000 height =1000></applet>

</html>*/

 

//calculator.java


import java.awt.*;

import java.awt.event.*;

public class calculator implements ActionListener

{

    int c,n;

    String s1,s2,s3,s4,s5;

    Frame f;

    Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,add,sub,mul,div,mod,eq,clear;

    Panel p;

    TextField tf;

    GridLayout g;

    public calculator1()

    {

        f = new Frame("My calculator");

        p = new Panel();

        f.setLayout(new FlowLayout());

g = new GridLayout(4,4,10,20);

        p.setLayout(g);

//creating components

  tf = new TextField(20);

          b0=new Button("0");

  b1=new Button("1");

  b2=new Button("2");

  b3=new Button("3");

  b4=new Button("4");

  b5=new Button("5");

  b6=new Button("6");

  b7=new Button("7");

  b8=new Button("8");

  b9=new Button("9");

  add=new Button("+");

  sub=new Button("-");

  mul=new Button("*");

  div=new Button("/");

  mod = new Button("%");

  eq=new Button("=");

  clear=new Button("C");

//adding components into panel 

        p.add(b0);p.add(b1);p.add(b2);p.add(b3);p.add(b4);p.add(b5);p.add(b6);p.add(b7);p.add(b8);

        p.add(b9);p.add(add);p.add(sub);p.add(mul);p.add(div);p.add(mod);p.add(eq);p.add(clear);

//adding components into frame 

        f.add(tf);

        f.add(p);

        f.setSize(300,300);

        f.setVisible(true);

    //adding actions

        b0.addActionListener(this);

        b1.addActionListener(this);

        b2.addActionListener(this);

        b3.addActionListener(this);

        b4.addActionListener(this);

        b5.addActionListener(this);

        b6.addActionListener(this);

        b7.addActionListener(this); 

        b8.addActionListener(this);

        b9.addActionListener(this);

        add.addActionListener(this);

        sub.addActionListener(this);

        mul.addActionListener(this);

        div.addActionListener(this);

        mod.addActionListener(this);

        eq.addActionListener(this); 

        clear.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e)

    {

        if(e.getSource()==b0)

        {

            s3 = tf.getText();

            s4 = "0";

            s5 = s3+s4;

            tf.setText(s5);

        }

        if(e.getSource()==b1)

        {

            s3 = tf.getText();

            s4 = "1";

            s5 = s3+s4;

            tf.setText(s5);

        }

        if(e.getSource()==b2)

        {

            s3 = tf.getText();

            s4 = "2";

            s5 = s3+s4;

            tf.setText(s5);

        }if(e.getSource()==b3)

        {

            s3 = tf.getText();

            s4 = "3";

            s5 = s3+s4;

            tf.setText(s5);

        }

        if(e.getSource()==b4)

        {

            s3 = tf.getText();

            s4 = "4";

            s5 = s3+s4;

            tf.setText(s5);

        }

        if(e.getSource()==b5)

        {

            s3 = tf.getText();

            s4 = "5";

            s5 = s3+s4;

            tf.setText(s5);

        }

        if(e.getSource()==b6)

        {

            s3 = tf.getText();

            s4 = "6";

            s5 = s3+s4;

            tf.setText(s5);

        }

        if(e.getSource()==b7)

        {

            s3 = tf.getText();

            s4 = "7";

            s5 = s3+s4;

            tf.setText(s5);

        }

        if(e.getSource()==b8)

        {

            s3 = tf.getText();

            s4 = "8";

            s5 = s3+s4;

            tf.setText(s5);

        }

        if(e.getSource()==b9)

        {

            s3 = tf.getText();

            s4 = "9";

            s5 = s3+s4;

            tf.setText(s5);

        }

        if(e.getSource()==add)

        {

            s1 = tf.getText();

            tf.setText("");

            c=1;

 

        }

        if(e.getSource()==sub)

        {

            s1 = tf.getText();

            tf.setText("");

            c=2;

 

        }

        if(e.getSource()==mul)

        {

            s1 = tf.getText();

            tf.setText("");

            c=3;

 

        }

        if(e.getSource()==div)

        {

            s1 = tf.getText();

            tf.setText("");

            c=4;

 

        }

        if(e.getSource()==mod)

        {

            s1 = tf.getText();

            tf.setText("");

            c=5;

 

        }

        if(e.getSource()==eq)

        {

            s2 = tf.getText();

            if(c==1)

            {

                n = Integer.parseInt(s1)+Integer.parseInt(s2);

                tf.setText(String.valueOf(n));

            }

            else

            if(c==2)

            {

                n = Integer.parseInt(s1)-Integer.parseInt(s2);

                tf.setText(String.valueOf(n));

            }

            else

            if(c==3)

            {

                n = Integer.parseInt(s1)*Integer.parseInt(s2);

                tf.setText(String.valueOf(n));

            }

            if(c==4)

            {

                try

                {

                    int p=Integer.parseInt(s2);

                    if(p!=0)

                    {

                                        n = Integer.parseInt(s1)/Integer.parseInt(s2);

                    tf.setText(String.valueOf(n));

                     }

                     else

                        tf.setText("infinite");

 

                }

                catch(Exception i){}

            }

            if(c==5)

            {

                n = Integer.parseInt(s1)%Integer.parseInt(s2);

                tf.setText(String.valueOf(n));

            }

        }

        if(e.getSource()==clear)

        {

            tf.setText("");

        }

    }

 

    public static void main(String[] abc)

    {

        calculator v = new calculator();

    }

}

 

Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Abstract classes and Abstract methods in Java with Examples rcub.

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.