fork download
  1. final class ComplexNumber {
  2. private final double re;
  3. private final double im;
  4.  
  5. public ComplexNumber(double re, double im) {
  6. this.re = re;
  7. this.im = im;
  8. }
  9.  
  10. public double getRe() {
  11. return re;
  12. }
  13.  
  14. @Override
  15. public boolean equals(Object o) {
  16. if (this == o) return true; //(this == o) - ругается
  17. if (o == null || getClass() != o.getClass()) return false; //ругается getClass() != o.getClass()
  18.  
  19.  
  20. ComplexNumber that = (ComplexNumber) o; //(ComplexNumber) o - ругается
  21.  
  22. if (Double.compare(that.re, re) != 0) return false;
  23. if (Double.compare(that.im, im) != 0) return false;
  24.  
  25. return true;
  26. }
  27.  
  28. @Override
  29. public int hashCode() {
  30. int result;
  31. long temp;
  32. temp = Double.doubleToLongBits(re);
  33. result = (int) (temp ^ (temp >>> 32));
  34. temp = Double.doubleToLongBits(im);
  35. result = 31 * result + (int) (temp ^ (temp >>> 32));
  36. return result;
  37. }
  38.  
  39. public double getIm() {
  40. return im;
  41.  
  42.  
  43.  
  44. }
  45. public static void main(String[] args) {
  46.  
  47. ComplexNumber a = new ComplexNumber(1, 1);
  48. ComplexNumber b = new ComplexNumber(1, 1);
  49. System.out.println(a.equals(b));
  50. System.out.println(a.hashCode() + " " + a.hashCode());
  51. }
  52. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
true
-33554432 -33554432