-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculateTriangle.java
71 lines (60 loc) · 2.43 KB
/
CalculateTriangle.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Programmer: Dan Hopp
* Date: 23-OCT-2019
* Description: Prompt the user to enter three sides of a triangle. If the three
* sides make a valid triangle, compute its area formatted to 2 decimal places.
* If it's not a valid triangle, display that it is an invalid triangle.
* Triangle area formula:
* s = (side1 + side2 + side3) / 2
* area = sqrt(s * (s - side1) * (s - side2) * (s - side3))
*
*
* Use two methods.
* #1 - Check if three sides will make a valid triangle:
* public static boolean isValidTriangle(double side1, double side2,
* double side3)
* #2 - If valid, a second method will compute the area of a triangle:
* public static double area(double side1, double side2, double side3)
*/
package lab6;
import java.util.Scanner;
public class Problem2 {
public static void main(String[] arg) {
//Prompt the user to enter three sides of a triangle
Scanner input = new Scanner(System.in);
System.out.print("Enter three sides of the triangle as "
+ "double values: ");
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
//Check for the valid triangle
//if valid, give user the triangle's area
if(isValidTriangle(side1, side2, side3)){
//round to 2 decimal places
System.out.printf("The area of the triangle is %4.2f\n",
area(side1, side2, side3));
}
//if invalid, let the user know
else {
System.out.println("The sides do not make a vaild triangle.");
}
}
//return true if the sum of any two sides is greater than the third side
public static boolean isValidTriangle(double side1, double side2,
double side3) {
//check all "2 sides > third side" combos
if (side1 + side2 > side3
&& side2 + side3 > side1
&& side1 + side3 > side2) {
return true;
}
else {
return false;
}
}
//return the area of a triangle
public static double area(double side1, double side2, double side3) {
double s = (side1 + side2 + side3) / 2;
return Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);
}
}