Java Applet Program to Add Two Numbers

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

Addition of two numbers in Java user input(run time using text boxes)

An Applet program to find the sum of two numbers

import java.awt.*;    //abstract window toolkit(for button,labels etc.)

import java.applet.*;   //for applet

import java.awt.event.*;     //for event(action perform)

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)//bcalculate button code for calculation)

{

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)//bclear button code

{

tfirstNo.setText(” “);

tsecondNo.setText(” “);

tResult.setText(” “);

}}}

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

Save : summ.java

Run….

javac summ.java

appletviewer summ.java

an applet program to add 2 numbers

An Applet Program to show the use of PARAM Tag

//Addition by using the passing of parameters to an applet(<param>)

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  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);

}

}

/*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>

*/

Save ……..

ParameterAddExample.java

Run……

javac ParameterAddExample.java

appletviewer ParameterAddExample.java

PARAM Tag in Java Applet