Java program to display sum of two input numbers in applet

Sum of two input numbers in applet

This program illustrate the concept of Sum of two input numbers in applet.

  • Textfields are used to take input in applet.To know more about textfields click Display textfields
  • x=Integer.parseInt(s1); is to convert string into integer.
  • g.drawString(s3,10,70); will display the output in applet.


public boolean action(Event w,Object o)
{
repaint();
return true;
}

  • It will return a boolean value either true of false.
  • Event e argumnet is used in applet to determine the event occur.

Scanfcode.java


import java.awt.*;
import java.applet.*;
public class scanfcode extends Applet
{
	TextField t1,t2;
	public void init()
	{
		t1=new TextField(10);
		add(t1);
		t2=new TextField(10);
		add(t2);
		
		
	}
  public void paint(Graphics g)
  {
	  int x=0,y=0,z=0;
	  String s1,s2,s3;
	  g.drawString("Enter Two numbers and press enter",10,50);
	  s1=t1.getText();
	  x=Integer.parseInt(s1);
	  s2=t2.getText();
	  y=Integer.parseInt(s2);
	  z=x+y;
	  s3="Sum of two input numbers is "+String.valueOf(z);
	  g.drawString(s3,10,70);
	  
	  
  }
  public boolean action(Event w,Object o)
  {
	  repaint();
	  return true;
  }
}

Scanfcode.html

<html>
<head>
<title>
<p>First applet program</p>
</title>
</head>
<body>
<applet code="scanfcode.class" width="350" height="350">
</applet>
</body>
</html>

Coammand

Sum of two input numbers in applet

OUTPUT

Sum of two input numbers in applet


Comment below if you are facing any problem.


Also read

Java applet program to display sum of two numbers without textfield.
Java applet program to display textfields


Summary
Java applet program to display sum of two input numbers
Article Name
Java applet program to display sum of two input numbers
Description
This program illustrate the concept of Sum of two input numbers in applet.Textfields are used to take input in applet.
Author
Publisher Name
Scanfcode
Publisher Logo

Leave a Reply

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