-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAboutEquality.java
136 lines (114 loc) · 4.1 KB
/
AboutEquality.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package intermediate;
import com.sandwich.koan.Koan;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutEquality {
// This suite of Koans expands on the concepts introduced in beginner.AboutEquality
@Koan
public void sameObject() {
Object a = new Object();
Object b = a;
assertEquals(a == b, true);
}
@Koan
public void equalObject() {
Integer a = new Integer(1);
Integer b = new Integer(1);
assertEquals(a.equals(b), true);
assertEquals(b.equals(a),true);
}
@Koan
public void noObjectShouldBeEqualToNull() {
assertEquals(new Object().equals(null), false);
}
static class Car {
private String name = "";
private int horsepower = 0;
public Car(String s, int p) {
name = s;
horsepower = p;
}
@Override
public boolean equals(Object other) {
// Change this implementation to match the equals contract
// Car objects with same horsepower and name values should be considered equal
// http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object)
//car c= (car)other;
if(!(other instanceof Car))
return false;
if(this.hashCode()==other.hashCode())
return true;
else
return false;
}
@Override
public int hashCode() {
// @see http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode()
return (this.name+this.horsepower).hashCode();
// return super.hashCode();
}
}
@Koan
public void equalForOwnObjects() {
Car car1 = new Car("Beetle", 50);
Car car2 = new Car("Beetle", 50);
// @see Car.equals (around line 45) for the place to solve this
assertEquals(car1.equals(car2), true);
assertEquals(car2.equals(car1), true);
}
@Koan
public void unequalForOwnObjects() {
Car car1 = new Car("Beetle", 50);
Car car2 = new Car("Porsche", 300);
// @see Car.equals (around line 45) for the place to solve this
assertEquals(car1.equals(car2), false);
}
@Koan
public void unequalForOwnObjectsWithDifferentType() {
Car car1 = new Car("Beetle", 50);
String s = "foo";
// @see Car.equals (around line 45) for the place to solve this
assertEquals(car1.equals(s), false);
}
@Koan
public void equalNullForOwnObjects() {
Car car1 = new Car("Beetle", 50);
// @see Car.equals (around line 45) for the place to solve this
assertEquals(car1.equals(null), false);
}
@Koan
public void ownHashCode() {
// As a general rule: When you override equals you should override
// hash code
// Read the hash code contract to figure out why
// http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode()
// Implement Car.hashCode around line 51 so that the following assertions pass
Car car1 = new Car("Beetle", 50);
Car car2 = new Car("Beetle", 50);
assertEquals(car1.equals(car2), true);
assertEquals(car1.hashCode() == car2.hashCode(), true);
}
static class Chicken {
String color = "green";
@Override
public int hashCode() {
return 4000;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Chicken))
return false;
return ((Chicken) other).color.equals(color);
}
}
@Koan
public void ownHashCodeImplementationPartTwo() {
Chicken chicken1 = new Chicken();
chicken1.color = "black";
Chicken chicken2 = new Chicken();
assertEquals(chicken1.equals(chicken2), false);
assertEquals(chicken1.hashCode() == chicken2.hashCode(), true);
// Does this still fit the hashCode contract? Why (not)?
// Fix the Chicken class to correct this.
}
}