JAVA Program using Applet to Display Calculator

JAVA Program using Applet to Display Calculator:

/*

<APPLET
 CODE = calculator.class
 WIDTH=600
 HEIGHT=400>
 </APPLET>
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class calculator extends Applet
                         implements ActionListener

{
   TextField t1,t2,t3;
   Button b1,b2,b3,b4;

   public void init()
   {

    t1 = new TextField(8);
    t2 = new TextField(8);
    t3 = new TextField(8);
    add(t1);
    add(t2);
    add(t3);
    t1.setText("0");
    t2.setText("0");
    t3.setText("");
    b1= new Button("Addition");
    add(b1);
    b1.addActionListener(this);

    b2= new Button("Substract");
    add(b2);
    b2.addActionListener(this);

    b3= new Button("Multiply");
    add(b3);
    b3.addActionListener(this);

    b4= new Button("Division");
    add(b4);
    b4.addActionListener(this);
   }


    public void actionPerformed(ActionEvent e)
    {

        int x=0,y=0,z=0;
        String s1,s2,s;
         try
           {
            s1=t1.getText();
            x= Integer.parseInt(s1);
            s2=t2.getText();
            y= Integer.parseInt(s2);
           }
            catch(Exception ex) { }
           s="";
           z=0;

      if(e.getSource()==b1)
      {
       z=x+y;
      }

      if(e.getSource()==b2)
      {
       z=x-y;
      }

      if(e.getSource()==b3)
      {
        z=x*y;
      }
      if(e.getSource()==b4)
      {
        z=x/y;
      }
      s=String.valueOf(z);
      t3.setText(s);

    }
}

OUTPUT:

JAVA Program using Applet to Display Calculator

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.