-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2.1. Рефакторинг - Площадь треугольника.
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ru.j4j.oop; | ||
|
||
import static java.lang.Math.sqrt; | ||
import static java.lang.Math.pow; | ||
|
||
public class Point { | ||
private final int x; | ||
private final int y; | ||
|
||
public Point(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
public double distance(Point that) { | ||
return sqrt(pow(this.x - that.x, 2) + pow(this.y - that.y, 2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ru.j4j.oop; | ||
|
||
import static java.lang.Math.*; | ||
|
||
public class Triangle { | ||
private Point ab; | ||
private Point bc; | ||
private Point ca; | ||
|
||
public Triangle(Point ab, Point bc, Point ca) { | ||
this.ab = ab; | ||
this.bc = bc; | ||
this.ca = ca; | ||
} | ||
|
||
public double semiPerimeter(double a, double b, double c) { | ||
return (a + b + c) / 2; | ||
} | ||
|
||
public boolean exist(double ab, double ac, double bc) { | ||
return ab + ac > bc && ac + bc > ab && ab + bc > ac; | ||
} | ||
|
||
public double area() { | ||
double ab = this.ab.distance(this.bc); | ||
double bc = this.ab.distance(this.ca); | ||
double ca = this.bc.distance(this.ca); | ||
if (exist(ab, bc, ca)) { | ||
double p = semiPerimeter(ab, bc, ca); | ||
return sqrt(p * (p - ab) * (p - bc) * (p - ca)); | ||
} | ||
return -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ru.j4j.oop; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class TriangleTest { | ||
@Test | ||
public void when00and40and04Then8() { | ||
Point a = new Point(0, 0); | ||
Point b = new Point(4, 0); | ||
Point c = new Point(0, 4); | ||
Triangle triangle = new Triangle(a, b, c); | ||
double rsl = triangle.area(); | ||
double expected = 8; | ||
assertThat(rsl).isCloseTo(expected, offset(0.001)); | ||
} | ||
|
||
@Test | ||
public void when00and00and04Then1() { | ||
Point a = new Point(0, 0); | ||
Point b = new Point(0, 0); | ||
Point c = new Point(0, 4); | ||
Triangle triangle = new Triangle(a, b, c); | ||
double rsl = triangle.area(); | ||
double expected = -1; | ||
assertThat(rsl).isCloseTo(expected, offset(0.001)); | ||
} | ||
|
||
@Test | ||
public void negativeTest() { | ||
Point a = new Point(0, 0); | ||
Point b = new Point(0, 0); | ||
Point c = new Point(0, 4); | ||
Triangle triangle = new Triangle(a, b, c); | ||
double rsl = triangle.area(); | ||
double expected = -1; | ||
assertThat(rsl).isEqualTo(expected); | ||
} | ||
} |