Java Applet Program to Add Two Numbers
Java Programming

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

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment