• We just launched and are currently in beta. Join us as we build and grow the community.

Compute Square Root of a Number in Java GUI

bagwis

On-Chain Analyst
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
158
Likes
200
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, you will learn how to compute a square root of a number using Java GUI using Math.sqrt function. We all know that square root are number that when multiplied by itself gives a given number.

Now let's start this tutorial!


1. Open JCreator or NetBeans and make a java program with a file name of sqrRoot.java.

2. Import javax.swing package. Hence we will use a GUI (Graphical User Interface) here like the inputting the number.
  1. import

    javax.swing.*

    ;

3. Initialize your variable in your Main, variable numInput as string and make it as an input and variable squareRoot as double that will hold the output value of our output when we inputted on the inputBox.
  1. double

    squareRoot;
  2. String

    numInput;

  3. numInput =

    JOptionPane

    .showInputDialog

    (

    null

    , "Enter a number:"

    )

    ;
  4. squareRoot =

    Double

    .parseDouble

    (

    numInput)

    ;

4. Have now your computation on how to get the square root using Math.sqrt function. Put this code below.

  1. squareRoot =

    Math

    .sqrt

    (

    squareRoot)

    ;

5. Lastly, display the output of the program using JOptionPane.showMessageDialog.

  1. JOptionPane

    .showMessageDialog

    (

    null

    , "Square Root is: "

    +

    squareRoot)

    ;


Output:

squareroot.png


Here's the full code of this tutorial:

  1. import

    javax.swing.*

    ;

  2. public

    class

    sqrRoot {


  3. public

    static

    void

    main(

    String

    [

    ]

    args)

    {


  4. double

    squareRoot;
  5. String

    numInput;

  6. numInput =

    JOptionPane

    .showInputDialog

    (

    null

    , "Enter a number:"

    )

    ;
  7. squareRoot =

    Double

    .parseDouble

    (

    numInput)

    ;

  8. squareRoot =

    Math

    .sqrt

    (

    squareRoot)

    ;
  9. JOptionPane

    .showMessageDialog

    (

    null

    , "Square Root is: "

    +

    squareRoot)

    ;

  10. }


  11. }

Hope this helps! :)

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,491

323,080

323,089

Top