|
| 1 | + |
| 2 | +//Greet Apelles, the approved in Christ. Greet those who are of the household of Aristobulus. (Romans 16:10) |
| 3 | + |
| 4 | +package com.javarush.task.task21.task2106; |
| 5 | + |
| 6 | +import java.util.Date; |
| 7 | + |
| 8 | +/* |
| 9 | +Ошибка в equals/hashCode |
| 10 | +*/ |
| 11 | +public class Solution { |
| 12 | + private int anInt; |
| 13 | + private String string; |
| 14 | + private double aDouble; |
| 15 | + private Date date; |
| 16 | + private Solution solution; |
| 17 | + |
| 18 | + public Solution(int anInt, String string, double aDouble, Date date, Solution solution) { |
| 19 | + this.anInt = anInt; |
| 20 | + this.string = string; |
| 21 | + this.aDouble = aDouble; |
| 22 | + this.date = date; |
| 23 | + this.solution = solution; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public boolean equals(Object o) { |
| 28 | + if (this == o) return true; |
| 29 | + if (!(o instanceof Solution)) return false; |
| 30 | + |
| 31 | + Solution solution1 = (Solution) o; |
| 32 | + |
| 33 | + if (Double.compare(solution1.aDouble, aDouble) != 0) return false; |
| 34 | + if (anInt != solution1.anInt) return false; |
| 35 | + if (date != null ? !date.equals(solution1.date) : solution1.date != null) return false; |
| 36 | + if (solution != null ? !solution.equals(solution1.solution) : solution1.solution != null) return false; |
| 37 | + if (string != null ? !string.equals(solution1.string) : solution1.string != null) return false; |
| 38 | + |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public int hashCode() { |
| 44 | + int result; |
| 45 | + long temp; |
| 46 | + result = anInt; |
| 47 | + temp = aDouble != +0.0d ? Double.doubleToLongBits(aDouble) : 0L; |
| 48 | + result = 31 * result + (int) (temp ^ (temp >>> 32)); |
| 49 | + result = 31 * result + (solution != null ? solution.hashCode() : 0); |
| 50 | + result = 31 * result + (date != null ? date.hashCode() : 0); |
| 51 | + result = 31 * result + (string != null ? string.hashCode() : 0); |
| 52 | + return result; |
| 53 | + } |
| 54 | + |
| 55 | + public static void main(String[] args) { |
| 56 | + |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/* |
| 61 | +Ошибка в equals/hashCode |
| 62 | +Исправьте ошибки реализаций методов equals и hashCode для класса Solution. |
| 63 | +
|
| 64 | +
|
| 65 | +Требования: |
| 66 | +1. Хешкоды одинаковых объектов должны быть равны. |
| 67 | +2. Метод equals должен проверять равен ли переданный объект равен текущему (сравнение через ==). |
| 68 | +3. Метод equals должен проверять является ли переданный объект объектом класса Solution. |
| 69 | +4. Метод equals должен проверять значения всех полей у переданного объекта и текущего (учти что некоторые из них могут быть равны null). |
| 70 | +5. Должно быть обеспечено корректное поведение HashSet с типом элементов Solution. |
| 71 | +6. В классе Solution должен быть реализован метод hashCode. |
| 72 | +
|
| 73 | +package com.javarush.task.task21.task2106; |
| 74 | +
|
| 75 | +import java.util.Date; |
| 76 | +
|
| 77 | +* |
| 78 | +Ошибка в equals/hashCode |
| 79 | +* |
| 80 | +public class Solution { |
| 81 | + private int anInt; |
| 82 | + private String string; |
| 83 | + private double aDouble; |
| 84 | + private Date date; |
| 85 | + private Solution solution; |
| 86 | +
|
| 87 | + public Solution(int anInt, String string, double aDouble, Date date, Solution solution) { |
| 88 | + this.anInt = anInt; |
| 89 | + this.string = string; |
| 90 | + this.aDouble = aDouble; |
| 91 | + this.date = date; |
| 92 | + this.solution = solution; |
| 93 | + } |
| 94 | +
|
| 95 | + @Override |
| 96 | + public boolean equals(Object o) { |
| 97 | + if (this == o) return true; |
| 98 | + if (o instanceof Solution) return false; |
| 99 | +
|
| 100 | + Solution solution1 = (Solution) o; |
| 101 | +
|
| 102 | + if (Double.compare(solution1.aDouble, aDouble) != 0) return false; |
| 103 | + if (anInt != solution1.anInt) return false; |
| 104 | + if (date != null ? !date.equals(solution1.date) : solution1.date == null) return false; |
| 105 | + if (solution != null ? !solution.equals(solution1.solution) : solution1.solution == null) return false; |
| 106 | + if (string != null ? !string.equals(solution1.string) : solution1.string == null) return false; |
| 107 | +
|
| 108 | + return true; |
| 109 | + } |
| 110 | +
|
| 111 | + @Override |
| 112 | + public int hashCode() { |
| 113 | + int result; |
| 114 | + long temp; |
| 115 | + result = anInt; |
| 116 | + temp = aDouble != +0.0d ? Double.doubleToLongBits(aDouble) : 0L; |
| 117 | + result = 31 * result + (int) (temp ^ (temp >>> 32)); |
| 118 | + result = 31 * result + (solution != null ? solution.hashCode() : 0); |
| 119 | + return result; |
| 120 | + } |
| 121 | +
|
| 122 | + public static void main(String[] args) { |
| 123 | +
|
| 124 | + } |
| 125 | +} |
| 126 | +*/ |
0 commit comments