From 90be640aafedba0fcc9c1d949380f03d2da46995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz> Date: Mon, 8 Mar 2021 10:37:15 +0100 Subject: [PATCH] Solution --- src/main/java/koz01/java2/lab05/Point.java | 35 ++++++++++- .../java/koz01/java2/lab05/ExampleTest.java | 11 ++++ .../java/koz01/java2/lab05/TestPoint.java | 61 +++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/test/java/koz01/java2/lab05/ExampleTest.java create mode 100644 src/test/java/koz01/java2/lab05/TestPoint.java diff --git a/src/main/java/koz01/java2/lab05/Point.java b/src/main/java/koz01/java2/lab05/Point.java index 978b02e..4a1f244 100644 --- a/src/main/java/koz01/java2/lab05/Point.java +++ b/src/main/java/koz01/java2/lab05/Point.java @@ -1,7 +1,22 @@ package koz01.java2.lab05; +import lombok.ToString; + +@ToString public class Point { + + public static Point getRandomPointWithDistanceFromOrigin(double distance) { + if (distance < 0) { + throw new IllegalArgumentException("distance = " + distance + + " is negative"); + } + // x = distance * cos(phi) + // y = distance * sin(phi) + double phi = Math.random() * 2 * Math.PI; + return new Point(distance * Math.cos(phi), distance * Math.sin(phi)); + } + private double x; private double y; @@ -23,5 +38,23 @@ public class Point { protected boolean canEqual(Point point) { return true; } - + + /** + * Naimplementujte hashCode pomocĂ vzorce resultHashCode := resultHashCode*31 + * + hashCode(attribute). + */ + @Override + public int hashCode() { + int resultHashCode = 1; + resultHashCode = resultHashCode * 31 + Double.hashCode(x); + resultHashCode = resultHashCode * 31 + Double.hashCode(y); + return resultHashCode; + } + + public double distance(Point p) { + double dY = p.y - this.y; + double dX = p.x - this.x; + return Math.sqrt(dY * dY + dX * dX); + } + } diff --git a/src/test/java/koz01/java2/lab05/ExampleTest.java b/src/test/java/koz01/java2/lab05/ExampleTest.java new file mode 100644 index 0000000..bddfac2 --- /dev/null +++ b/src/test/java/koz01/java2/lab05/ExampleTest.java @@ -0,0 +1,11 @@ +package koz01.java2.lab05; + +import org.junit.jupiter.api.Test; + +public class ExampleTest { + + @Test + public void testFoo() { + + } +} diff --git a/src/test/java/koz01/java2/lab05/TestPoint.java b/src/test/java/koz01/java2/lab05/TestPoint.java new file mode 100644 index 0000000..fc708a3 --- /dev/null +++ b/src/test/java/koz01/java2/lab05/TestPoint.java @@ -0,0 +1,61 @@ +package koz01.java2.lab05; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.jupiter.api.Test; + +import lombok.extern.log4j.Log4j2; + +@Log4j2 +public class TestPoint { + + @Test + public void testHashCode() { + Set<Point> testingSet = new HashSet<>(); + Point p1 = new Point(10, 12); + testingSet.add(p1); + + log.info("contains = {} ", testingSet.contains(p1)); + log.info("p1.hashCode = {} ", p1.hashCode()); + assertTrue(testingSet.contains(p1)); + + Point p2 = new Point(10, 12); + assertEquals(p1, p2); + log.info("p2.hashCode = {} ", p2.hashCode()); + assertTrue(testingSet.contains(p2)); + } + + @Test + public void testPerformance() { + long start = System.currentTimeMillis(); + Set<Point> points = new HashSet<>(); + for (int i = 0; i < 100000; i++) { + points.add(new Point(i, i)); + } + + + + points.contains(new Point(0, 0)); + long end = System.currentTimeMillis(); + log.info("duration = {} ms", end - start); + } + + @Test + public void testNegativeDistance() { + assertThrows(IllegalArgumentException.class, () -> { + Point.getRandomPointWithDistanceFromOrigin(-10); + }); + } + + @Test + public void testGenerate() { + Point p = Point.getRandomPointWithDistanceFromOrigin(1000.); + assertEquals(1000., p.distance(new Point(0, 0))); + log.info("point = {}", p); + } +} -- GitLab