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

Solution

parent 924a86a7
No related merge requests found
Pipeline #125 failed with stages
in 0 seconds
package koz01.java2.lab05;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode
public class Point {
public static Point getPointWithDistanceFromOrigin(double distance) {
if (distance < 0) {
// IllegalArgumentException
throw new IllegalArgumentException("distance = " + distance +
" is negative");
}
// x = distance*cos(alfa)
// y = distance*sin(alfa)
double alfa = Math.random() * 2 * Math.PI;
return new Point(distance * Math.cos(alfa), distance * Math.sin(alfa));
}
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.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;
}
protected boolean canEqual(Point point) {
return true;
public double distance(Point p) {
double dx = p.x - x;
double dy = p.y - y;
return Math.sqrt(dx * dx + dy * dy);
}
/*
@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;
}
@Override
public int hashCode() {
int result = 1;
result = result * 31 + Double.hashCode(x);
result = result * 31 + Double.hashCode(y);
// TODO
// result = result * 31 + hash for attribute_n;
return result;
}
protected boolean canEqual(Point point) {
return true;
}
*/
}
package koz01.java2.lab05;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class Sample {
@Test
public void testFoo() {
assertEquals(2, 1 + 1);
}
}
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(1, 2);
Set<Point> setOfPoint = new HashSet<>();
assertFalse(setOfPoint.contains(p1));
setOfPoint.add(p1);
assertTrue(setOfPoint.contains(p1));
Point p2 = new Point(1, 2);
log.info("p1.equals(p2) = {}", p1.equals(p2));
assertTrue(setOfPoint.contains(p2));
}
@Test
public void testPerf() {
long t_0 = System.currentTimeMillis();
Set<Point> setOfPoint = new HashSet<>();
for (int i = 0; i < 10000; i++) {
setOfPoint.add(new Point(i, i));
}
assertTrue(setOfPoint.contains(new Point(10, 10)));
long t_1 = System.currentTimeMillis();
log.info("duration = {} ms", t_1 - t_0);
}
@Test
public void testGenerate() {
assertThrows(IllegalArgumentException.class, () -> Point
.getPointWithDistanceFromOrigin(-1));
assertEquals(200, Point.getPointWithDistanceFromOrigin(200).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