From 3c4f80404dd80cc99b66f903d77ec78efa29a92e Mon Sep 17 00:00:00 2001 From: "d.kapustin" Date: Tue, 31 Oct 2023 13:21:55 +0300 Subject: [PATCH] =?UTF-8?q?2.1.=20=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=BD=D0=B3=20-=20=D0=9F=D0=BB=D0=BE=D1=89?= =?UTF-8?q?=D0=B0=D0=B4=D1=8C=20=D1=82=D1=80=D0=B5=D1=83=D0=B3=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D0=B8=D0=BA=D0=B0.=20https://job4j.ru/profile/exer?= =?UTF-8?q?cise/20/task-view/142?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ru/j4j/oop/Point.java | 18 ++++++++++ src/main/java/ru/j4j/oop/Triangle.java | 34 ++++++++++++++++++ src/test/java/ru/j4j/oop/TriangleTest.java | 40 ++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/main/java/ru/j4j/oop/Point.java create mode 100644 src/main/java/ru/j4j/oop/Triangle.java create mode 100644 src/test/java/ru/j4j/oop/TriangleTest.java diff --git a/src/main/java/ru/j4j/oop/Point.java b/src/main/java/ru/j4j/oop/Point.java new file mode 100644 index 0000000..7c5bb57 --- /dev/null +++ b/src/main/java/ru/j4j/oop/Point.java @@ -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)); + } +} diff --git a/src/main/java/ru/j4j/oop/Triangle.java b/src/main/java/ru/j4j/oop/Triangle.java new file mode 100644 index 0000000..4cfa1b6 --- /dev/null +++ b/src/main/java/ru/j4j/oop/Triangle.java @@ -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; + } +} diff --git a/src/test/java/ru/j4j/oop/TriangleTest.java b/src/test/java/ru/j4j/oop/TriangleTest.java new file mode 100644 index 0000000..7dd41a4 --- /dev/null +++ b/src/test/java/ru/j4j/oop/TriangleTest.java @@ -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); + } +} \ No newline at end of file