forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExercise06_19.java
73 lines (63 loc) · 2.12 KB
/
Exercise06_19.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
72
73
package ch_06;
/**
* *6.19 (The MyTriangle class) Create a class named MyTriangle that contains the
* following two methods:
* /** Return true if the sum of any two sides is
* * greater than the third side.
* public static boolean isValid(
* double side1,double side2,double side3)
* /** Return the area of the triangle.
* public static double area(
* double side1,double side2,double side3)
* <p>
* <p>
* Write a test program that reads three sides for a triangle and computes the area if
* the input is valid. Otherwise,it displays that the input is invalid.The formula for
* computing the area of a triangle is given in Programming Exercise2.19.
*
* @author Harry D.
*/
public class Exercise06_19 {
public static void main(String[] args) {
double x1 = -1.356;
double y1 = 1.743;
double x2 = -1.377;
double y2 = 1.929;
double x3 = -1.432;
double y3 = 1.711;
double s1 = findSide(x1, y1, x2, y2);
double s2 = findSide(x2, y2, x3, y3);
double s3 = findSide(x1, y1, x3, y3);
if (isValid(s1, s2, s3)) {
System.out.println("The area of the triangle is: " + area(s1, s2, s3));
} else {
System.out.println("The input for sides, is invalid");
}
}
/**
* Return the area of the triangle.
*/
public static double area(double side1, double side2, double side3) {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt((s * (s - side1) * (s - side2) * (s - side3)));
}
/**
* Return true if the sum of any two sides is
* greater than the third side.
*/
public static boolean isValid(
double side1, double side2, double side3) {
if ((side1 + side2) > side3) {
return false;
} else if ((side1 + side3) > side2) {
return false;
} else if ((side2 + side3) > side1) {
return false;
} else {
return true;
}
}
public static double findSide(double x, double y, double w, double z) {
return Math.pow(Math.pow(x - w, 2) + Math.pow(y - z, 2), 0.5);
}
}