Java Program to Find Square Root 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.

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 :

advertisement
advertisement
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.

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:

>>>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.

advertisement
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

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

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.