-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangle.java
52 lines (44 loc) · 1.02 KB
/
Triangle.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
// Triangle class
public class Triangle
extends GeometricObject {
private double side1;
private double side2;
private double side3;
/** Construct default Triangle object */
Triangle() {
side1 = side2 = side3 = 1.0;
}
/** Parameterized constructor */
Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
/** Get side1 */
public double getSide1() {
return side1;
}
/** Get side2 */
public double getSide2() {
return side2;
}
/** Get side3 */
public double getSide3() {
return side3;
}
/** Get area of this triangle */
public double getArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
/** Get perimeter of this triangle */
public double getPerimeter() {
return side1 + side2 + side3;
}
/** String description of the triangle */
public String toString() {
return "Triangle output:\n" +
"side1 = " + side1 + " side2 = " + side2 +
" side3 = " + side3;
}
}