Saturday, 30 June 2012

program that performed various operation +,-,*,/ (calculator type) using applet.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/* <applet code = "appcalc" width=100 height=200 >
   </applet> */

public class appcalc extends Applet implements ActionListener
  {
   TextField no1,no2,op;
   int ino1,ino2;
   float result=0.0f;





   public void init()
    {
     Label lno1=new Label ("no1 :",Label.RIGHT);
     Label lno2=new Label ("no2 :",Label.RIGHT);
     Label lop=new Label ("op :",Label.RIGHT);
     no1=new TextField(3);
     no2=new TextField(3);
     op=new TextField(1);

     add(lno1);
    
     add(no1);
     add(lno2);
     add(no2);
     add(lop);
     add(op);

     no1.addActionListener (this );
     no2.addActionListener (this );
     op.addActionListener (this );

   }
   public void actionPerformed(ActionEvent ae)
     {
      repaint();
     }
   public void paint(Graphics g)
    {
     g.drawString("no1: "+no1.getText(),6,60);
     g.drawString("no2: "+no2.getText(),6,80);
     g.drawString("op: "+op.getText(),6,100);

     ino1 = Integer.parseInt(no1.getText());
     ino2 = Integer.parseInt(no2.getText());
     if ( op.getText().equals("+"))
      {
          result =(float) ( ino1 + ino2);
          g.drawString("result: "+result,6,120);

      }
     if ( op.getText().equals("-"))
      {
          result =(float) ( ino1 - ino2);
          g.drawString("result: "+result,6,120);

      }

     if ( op.getText().equals("*"))
      {
          result =(float) ( ino1 * ino2);
          g.drawString("result: "+result,6,120);

      }
     if ( op.getText().equals("/"))
      {
          result =(float) ( ino1 / ino2);
          g.drawString("result: "+result,6,120);

      }


    }
  }