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

Solution

parent 924a86a7
No related merge requests found
Pipeline #114 failed with stages
in 0 seconds
......@@ -6,22 +6,51 @@ public class Point {
private double y;
public static Point getRandomPointWithDistanceFromOrigin(double distance) {
if (distance < 0.) {
// vyhod vyjimku IllegalArgumentException
throw new IllegalArgumentException("Distance " + distance +
" is negative");
}
// x = dist*cos(alpha)
// y = dist*sin(alpha)
double alfa = Math.random() * 2 * Math.PI;
return new Point(distance * Math.cos(alfa), distance * Math.sin(alfa));
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double distance(Point p) {
double dX = this.x - p.x;
double dY = this.y - p.y;
return Math.sqrt(dX * dX + dY * dY);
}
/**
* 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;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point point = (Point) obj;
return point.canEqual(this) && point.x == x && point.y == y;
@Override
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point point = (Point) obj;
return point.canEqual(this) && point.x == x && point.y == y;
}
return false;
}
return false;
}
protected boolean canEqual(Point point) {
return true;
}
protected boolean canEqual(Point point) {
return true;
}
}
module koz01.java2.lab04 {
module koz01.java2.lab05 {
requires transitive javafx.controls;
requires javafx.fxml;
opens koz01.java2.lab05 to javafx.fxml;
exports koz01.java2.lab05;
requires lombok;
}
\ No newline at end of file
package koz01.java2.lab05;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
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() {
Point p1 = new Point(2, 3);
Set<Point> points = new HashSet<>();
points.add(p1);
assertTrue(points.contains(p1));
Point p2 = new Point(2, 3);
assertEquals(p1, p2);
assertTrue(points.contains(p2));
assertFalse(points.contains(new Point(0, 0)));
}
@Test
public void testPerf() {
Set<Point> points = new HashSet<>();
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
points.add(new Point(i, i));
}
assertTrue(points.contains(new Point(10, 10)));
long endTime = System.currentTimeMillis();
log.info("duration = {} ms", endTime - startTime);
}
@Test
public void testDistance() {
Point p1 = new Point(5, 6);
Point p2 = new Point(-7, 11);
assertEquals(13., p1.distance(p2));
}
@Test
public void testGenPoint() {
assertThrows(IllegalArgumentException.class, () -> Point
.getRandomPointWithDistanceFromOrigin(-1));
assertEquals(10, Point.getRandomPointWithDistanceFromOrigin(10).distance(
new Point(0, 0)));
}
}
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