forked from int28h/JavaTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0010.java
More file actions
45 lines (38 loc) · 1.43 KB
/
0010.java
File metadata and controls
45 lines (38 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Дан класс ComplexNumber. Переопределите в нем методы equals() и hashCode() так, чтобы equals()
* сравнивал экземпляры ComplexNumber по содержимому полей re и im, а hashCode() был бы согласованным с реализацией equals().
*
* Реализация hashCode(), возвращающая константу или не учитывающая дробную часть re и im, засчитана не будет
*/
public final class ComplexNumber {
private final double re;
private final double im;
public ComplexNumber(double re, double im) {
this.re = re;
this.im = im;
}
public double getRe() {
return re;
}
public double getIm() {
return im;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ComplexNumber that = (ComplexNumber) o;
if (Double.compare(that.re, re) != 0) return false;
return Double.compare(that.im, im) == 0;
}
@Override
public int hashCode() {
int result;
long temp;
temp = Double.doubleToLongBits(re);
result = (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(im);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
}