Addition of Two Numbers Using Applet

To build an applet, first of all we need to create an applet code(.java file). After successful compilation, its corresponding .class file is created. Then a web page also known as HTML document is to be developed. Once the HTML document is created, the applet is to be inserted in it. Finally the resultant HTML document will be executed to produce the desired output.

 

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class summ extends Applet implements ActionListener

{

  Label lfirstNo,lsecondNo,lResult;

  TextField tfirstNo,tsecondNo,tResult;

  Button bcalculate,bclear;

public void init()

 {

  setBackground(Color.gray);

  lfirstNo=new Label("enter no 1");

  lsecondNo=new Label("enter n0 2");

  lResult=new Label("result");

  tfirstNo=new TextField(20);

  tsecondNo=new TextField(20);

  tResult=new TextField(20);

  bcalculate=new Button("add");

  bclear=new Button("clear");

  add(lfirstNo);add(tfirstNo);

  add(lsecondNo);add(tsecondNo);

  add(lResult);add(tResult);

  add(bcalculate);add(bclear);

  bcalculate.addActionListener(this);

  bclear.addActionListener(this);

 }

public void actionPerformed(ActionEvent actionev)

{

  if(actionev.getSource()==bcalculate)

  {

   int first_No,sec_No,third_No;

   first_No=Integer.parseInt(tfirstNo.getText());

   sec_No=Integer.parseInt(tsecondNo.getText());

   third_No=first_No+sec_No;

   tResult.setText(String.valueOf(third_No));

}

if(actionev.getSource()==bclear)

 {

   tfirstNo.setText(" ");

   tsecondNo.setText(" ");

   tResult.setText(" ");

  }

 }

}

//<applet code="summ.class" height=500 width=500></applet>

 sum of two integer using Applet

 

//Addition by using passing of parameters to an applet

import java.awt.*;

import java.applet.*;

public class ParameterAddExample extends Applet

{

  int ist_No,iind_No,summ;

public void init()

{

/*init() in the applet retrieves user defined values of the parameters defined in the <PARAM> tags by using the getParameter().*/

  setBackground(Color.gray);

  ist_No=Integer.parseInt(getParameter("FirstNo"));

  iind_No=Integer.parseInt(getParameter("SecondNo"));

  summ=ist_No+iind_No; //calculation by using two variables

 }

public void paint(Graphics graphic)

{

  Font fon=new Font("Arial",Font.BOLD,15);//font setting

  graphic.setFont(fon);

  graphic.drawString("First Number is=" +ist_No,40,40);

  graphic.drawString("Second Number is=" +iind_No,40,60);

  graphic.drawString("Result is=" +summ,40,80);

 }

}

/*<PARAM>tag has a NAME attribute which defines the name of the parameter and a VALUE attribute specifies the value of the parameter.*/

/*

<applet code="ParameterAddExample.class" height=500 width=500>

<PARAM NAME=FirstNo VALUE=15>

<PARAM NAME=SecondNo VALUE=25>

</applet>

*/

sum of two integer