diff --git a/src/main/java/koz01/java2/lab05/Point.java b/src/main/java/koz01/java2/lab05/Point.java
index 978b02e053e63dda831deb09942f45919bcf7a51..4a1f244244e9644d07fc2a180621868434dd980c 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 0000000000000000000000000000000000000000..bddfac257605d38f07f71b927409b3b1e22a6aa7
--- /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 0000000000000000000000000000000000000000..fc708a3ee770d2b1af35753f1853b501cce12400
--- /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);
+ }
+}