-
Notifications
You must be signed in to change notification settings - Fork 1
/
Triangle.java
56 lines (46 loc) · 1.45 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
package com.yurii.salimov.lesson04.task02;
/**
* @author Yuriy Salimov ([email protected])
* @version 1.0
*/
public final class Triangle extends Figure {
private static final String NAME = "Triangle";
private final double sideA;
private final double sideB;
private final double sideC;
public Triangle(final double sideA, final double sideB, final 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 double getSideB() {
return this.sideB;
}
public double getSideC() {
return this.sideC;
}
public static Triangle combine(final Triangle first, final Triangle second) {
final double sideA = first.getSideA() + second.getSideA();
final double sideB = first.getSideB() + second.getSideB();
final double sideC = first.getSideC() + second.getSideC();
return new Triangle(sideA, sideB, sideC);
}
}