kalmik
Dark Web Navigator
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:
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
Open notepad or any text editor, and paste in the following code:
- import
javax.swing.*
;
//required for GUI components
- import
java.awt.*
;
//required for use of the container
- import
java.awt.event.*
;
//contains the ActionListener interface to handle GUI events
- public
class
abc
- {
- public
static
void
main(
String
[
]
args)
- {
- double
a, b, bc, c;
- String
aStr, bStr, cStr, output;
//declares our variables
- aStr =
JOptionPane
.showInputDialog
(
"Enter a length of the hypotenuse: "
)
;
//prompts user
- a =
Double
.parseDouble
(
aStr)
;
//sets a equal to the value input
- bStr =
JOptionPane
.showInputDialog
(
"Enter a length of another side: "
)
;
//prompts user
- b =
Double
.parseDouble
(
bStr)
;
//sets b equal to the value input
- cStr =
JOptionPane
.showInputDialog
(
"Enter a length of the last side: "
)
;
//prompts user
- c =
Double
.parseDouble
(
cStr)
;
//sets c equal to the value input
- bc =
(
b *
b)
+
(
c *
c)
;
//calculates the length of the two shorter sides squared
- if
(
bc ==
a *
a)
//conditional statement
- output =
"This IS a right triangle."
;
//output to user
- else
//if conditional is not satisfied
- output =
"This is NOT a right triangle."
;
//output
- JOptionPane
.showMessageDialog
(
null
, output, "Calculation"
, JOptionPane
.INFORMATION_MESSAGE
)
;
//displays output in GUI window
- }
- }
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.