Java Program to Find Square of a Given Number Using Applet

«
»

This is a Java Program to Find Square of a Given Number Using Applet

Problem Description

We have to write a program in Java such that it accepts a number and displays the square of the number using applet.

Expected Input and Output

For finding square of a number using applet, we can have the following different sets of input and output.

advertisement

1. To find the square of a number :

When the number 12 is entered,
it is expected that the square of 12 that is 144 is displayed in the frame.

2. To find the square of a floating-point number :

When the number 10.5 is entered,
it is expected that the square of 10.5 that is 110.25 is displayed in the frame.
Problem Solution

1. Create an input text field to accept a number from user.
2. Create a button to confirm the number and calculate its square.
3. When the button is clicked, display its square in a text field.

advertisement
Program/Source Code

Here is source code of the Java Program to find square of number using applet. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /*Java Applet to Find Square of Number using Applet*/
  2. import java.awt.*;
  3. import java.applet.*;
  4. import java.awt.event.*;
  5. public class Square_Num extends Applet implements ActionListener
  6. {
  7.     TextField num,out;
  8.     //Initialize with required input fields
  9.     public void init()
  10.     {
  11. 	Label label = new Label("Enter a number :");
  12. 	this.add(label);
  13. 	num = new TextField(5);
  14. 	this.add(num);
  15. 	Button calc = new Button("Calculate");
  16. 	calc.addActionListener(this);
  17. 	this.add(calc);
  18.         out = new TextField();
  19.     }
  20.     @Override
  21.     //Function to display the square of the number
  22.     public void actionPerformed(ActionEvent e)
  23.     {
  24. 	double n = Double.valueOf(num.getText());
  25. 	double sq=n*n;
  26. 	out.setText("Square of "+n+" is "+sq);
  27. 	this.add(out);
  28. 	revalidate();
  29.     }
  30. }
  31. /*
  32. <applet code = Square_Num.class width=500 height=500>
  33. </applet>
  34. */

To execute the applet use the following commands:

advertisement
>>>javac Square_Num.java
>>>appletviewer Square_Num.java
Program Explanation

1. Create a text field using TextField class of the java.awt package.
2. Create a button using Button class of the java.awt package.
2. Create a label using Label class of the java.awt package.

Runtime Test Cases

Here’s the run time test cases to Display Square of Number using Applet.

Test case 1 – To Display Square of a Number.
java-program-square-num

advertisement

Test case 2 – To Display Square of a floating-point number.
java-applet-square-number-dec

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn