The below program is similar to the SquareInt Applet. This one however also takes in 2 values and adds them together using a method. It then uses another method to work out if the result of the added numbers is odd or even.
//SquareInt2.java
//a programmer-defined square method
import java.awt.*;
import javax.swing.*;
public class SquareInt2 extends JApplet{
double sum =0;
public void init(){
String output = “”;
JTextArea outputArea = new JTextArea(100,200);
//get the applet’s HUI component display area
Container c = getContentPane();
//attach outputArea to Container c
c.add(outputArea);
int result;
for(int x = 1; x <= 10; x++){
result = square(x);
output += “The square of “+ x + ” is ” + result + “n”;
}
String firstNumber = JOptionPane.showInputDialog(”Please enter first value”);
String secondNumber = JOptionPane.showInputDialog(”Please enter second value”);
double fNumber;
double sNumber;
fNumber = Double.parseDouble(firstNumber);
sNumber = Double.parseDouble(secondNumber);
double answer = addNums(fNumber,sNumber);
output = output + “nn The answer to ” + fNumber + ” + ” + sNumber + ” is = ” + answer;
boolean even = is_even((int)answer);
if(even){
output = output + “n” + answer + ” is an even number”;
}
else{
output = output + “n” + answer + ” is an odd number”;
}
outputArea.setText(output);
}
//square method definition
public int square(int y){
return y * y;
}
//even odd method definition
public boolean is_even(int num){
if(num % 2 == 0){
return true;
}
return false;
}
//adding method definition
public double addNums(double a, double b){
double answer = a + b;
return answer;
}
}
Below is the resulting screenshot

SquareInt Applet
//SquareInt.java
// a programmer-defined square method
import java.awt.Container;
import javax.swing.*;
public class SquareInt extends JApplet{
public void init ()
{
String output=””;
JTextArea outputArea = new JTextArea(10,20);
//get the applet’s HUI component display area
Container c = getContentPane();
//attach outputArea to Container c
c.add(outputArea);
int result;
for(int x=1; x<=10; x++){
result=square(x);
output+=”The square of ” +x+ ” is ” + result + “\n”;
}
outputArea.setText(output);
}
//square method definition
public int square (int y)
{
return y*y;
}
}
The above code generates number 1 to 10 square as below.

Addition Applet
Today the first applet we created by one that takes in two numbers by the user and then returns the sum of those two numbers.
Below is the following code:
//AdditionApplet.java
//Adding two floating-point numbers
import java.awt.Graphics; //import class Graphics3 Graphics
import javax.swing.*; //import package javax.swing
public class AdditionApplet extends JApplet{
double sum; //sum of the vaules entered by the user
public void init()
{
String firstNumber, //first string entered by user
secondNumber; //second string entered by user
double number1, //first number to add
number2; //second number to add
//read the second number from user
firstNumber = JOptionPane.showInputDialog(”Enter first floating-point value”);
//read the second number from user
secondNumber = JOptionPane.showInputDialog(”Enter second floating-point value”);
//covert numbers from type String to type double
number1 = Double.parseDouble(firstNumber);
number2 = Double.parseDouble(secondNumber);
//add the numbers
sum = number1 + number2;
}
public void paint(Graphics g)
{
//draw the results with g.drawString
g.drawRect(15,10,270,20);
g.drawString(”The sum is ” + sum,25,25);
}
}
This code then generated the following with the Applet from the html file.
First prompting the user to enter in the first value. Then the second.

The results are shown as soon as the 2nd value are entered.
