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

Pythagorean Test in Java

kalmik

Dark Web Navigator
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
105
Likes
24
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
The following is a basic Java GUI program allowing a user to input the lengths of a triangle's sides to find out if it is a right triangle or not.

Open notepad or any text editor, and paste in the following code:

  1. import

    javax.swing.*

    ;

    //required for GUI components
  2. import

    java.awt.*

    ;

    //required for use of the container
  3. import

    java.awt.event.*

    ;

    //contains the ActionListener interface to handle GUI events

  4. public

    class

    abc
  5. {
  6. public

    static

    void

    main(

    String

    [

    ]

    args)
  7. {
  8. double

    a, b, bc, c;
  9. String

    aStr, bStr, cStr, output;

    //declares our variables

  10. aStr =

    JOptionPane

    .showInputDialog

    (

    "Enter a length of the hypotenuse: "

    )

    ;

    //prompts user
  11. a =

    Double

    .parseDouble

    (

    aStr)

    ;

    //sets a equal to the value input

  12. bStr =

    JOptionPane

    .showInputDialog

    (

    "Enter a length of another side: "

    )

    ;

    //prompts user
  13. b =

    Double

    .parseDouble

    (

    bStr)

    ;

    //sets b equal to the value input

  14. cStr =

    JOptionPane

    .showInputDialog

    (

    "Enter a length of the last side: "

    )

    ;

    //prompts user
  15. c =

    Double

    .parseDouble

    (

    cStr)

    ;

    //sets c equal to the value input

  16. bc =

    (

    b *

    b)

    +

    (

    c *

    c)

    ;

    //calculates the length of the two shorter sides squared

  17. if

    (

    bc ==

    a *

    a)

    //conditional statement
  18. output =

    "This IS a right triangle."

    ;

    //output to user

  19. else

    //if conditional is not satisfied

  20. output =

    "This is NOT a right triangle."

    ;

    //output

  21. JOptionPane

    .showMessageDialog

    (

    null

    , output, "Calculation"

    , JOptionPane

    .INFORMATION_MESSAGE

    )

    ;

    //displays output in GUI window

  22. }
  23. }

Save it as abc.java. This is important or it will not run correctly. (The filename must match the name of the class that is delcared at the top.) You may notice there are a lot of comments. This is to help anybody who reads it know what each small section of code does. Read through this and you will get a feel for how it works step-by-step.

Open a command line or terminal, and type "cd" (without quotes) followed by the path to where you saved the abc.java file. If you saved it in Documents, type "cd Documents" and hit enter. Now, type "javac abc.java" and hit enter, then "java abc" and the program will run.

You will be asked for three lengths, starting with the hypotenuse. The program basically works by squaring the hypotenuse, and the sum of the other two sides, and seeing if both squares are equal. If they are, it is a right triangle. The user is told at the end if the sides they input make up a right triangle or not.


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

442,401

317,942

317,951

Top