-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHypotenuse.java
More file actions
42 lines (34 loc) · 1.42 KB
/
Hypotenuse.java
File metadata and controls
42 lines (34 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.text.DecimalFormat;
import java.util.Random;
/**
* This class randomly models a right triangle and calculates its hypotenuse.
* It shows the usage of random numbers, Math methods and number formatting.
*
* @author Rajin Gajadhar
* @version 1.0
*/
public class Hypotenuse {
/**
* A constant holding the value 2.0 for calculations.
*/
public static final double TWO = 2.0;
/**
* The main method where the program starts.
* @param args Command line arguments (not used).
*/
public static void main(String[] args) {
// Create a Random object to generate random numbers
Random random = new Random();
// Generate random numbers for base and height
double base = random.nextDouble() * 6;
double height = random.nextDouble() * 6;
// Compute the hypotenuse using Pythagorean theorem
double hypotenuse = Math.sqrt(Math.pow(base, TWO) + Math.pow(height, TWO));
// Create a DecimalFormat object to format the output
DecimalFormat formatter = new DecimalFormat("#.##");
// Display the results
System.out.println("Base of the right triangle: " + formatter.format(base));
System.out.println("Height of the right triangle: " + formatter.format(height));
System.out.println("Hypotenuse of the right triangle: " + formatter.format(hypotenuse));
}
}