Skip to content
Snippets Groups Projects
Commit 90be640a authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Solution

parent 924a86a7
No related merge requests found
Pipeline #120 failed with stages
in 0 seconds
package koz01.java2.lab05; package koz01.java2.lab05;
import lombok.ToString;
@ToString
public class Point { 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 x;
private double y; private double y;
...@@ -23,5 +38,23 @@ public class Point { ...@@ -23,5 +38,23 @@ public class Point {
protected boolean canEqual(Point point) { protected boolean canEqual(Point point) {
return true; 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);
}
} }
package koz01.java2.lab05;
import org.junit.jupiter.api.Test;
public class ExampleTest {
@Test
public void testFoo() {
}
}
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);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment