-
Notifications
You must be signed in to change notification settings - Fork 1
/
Triangle.java
69 lines (56 loc) · 1.6 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.yurii.salimov.lesson03.task05;
/**
* @author Yuriy Salimov (yuriy.alex.salimov@gmail.com)
* @version 1.0
*/
public class Triangle extends Figure {
private static final String NAME = "Triangle";
private double sideA;
private double sideB;
private double sideC;
public Triangle(double sideA, double sideB, double sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
// Heron's formula
@Override
public double getArea() {
final double semiperimeter = (this.sideA + this.sideB + this.sideC) / 2;
return Math.sqrt(
semiperimeter *
(semiperimeter - this.sideA) *
(semiperimeter - this.sideB) *
(semiperimeter - this.sideC)
);
}
@Override
public String getName() {
return NAME;
}
public double getSideA() {
return this.sideA;
}
public void setSideA(double sideA) {
this.sideA = sideA;
}
public double getSideB() {
return this.sideB;
}
public void setSideB(double sideB) {
this.sideB = sideB;
}
public double getSideC() {
return this.sideC;
}
public void setSideC(double sideC) {
this.sideC = sideC;
}
public static Triangle combine(Triangle triangle1, Triangle triangle2) {
return new Triangle(
triangle1.getSideA() + triangle2.getSideA(),
triangle1.getSideB() + triangle2.getSideB(),
triangle1.getSideC() + triangle2.getSideC()
);
}
}