simpleapplettoadd2integers.java
// Applet class that does addition of two integer numbers
// import class graphics and javax.swing package
import java.awt.Graphics;
import javax.swing.*;
public class AddApplet extends JApplet
{
int sum;
int x;
int y;
public void init()
{
String num1;
String num2;
// read first number from the keyboard
num1 = JOptionPane.showInputDialog("Enter a whole number");
// read seond number from the ketboard
num2 = JOptionPane.showInputDialog("Enter a whole number");
x= Integer.parseInt(num1); // comvert the string to an integer
y= Integer.parseInt(num2); // convert the string to an integer
sum = x + y;
}
public void paint(Graphics g)
{
//writes the output in the applet starting at position 30, 30
g.drawString("the sum ot x (="+x+")+y(="+y+")="+sum, 30,30);
}
}
Return to : Java Programming Hints
and Tips