Simple applet to add 2 integers

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

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.