Skip to content

Commit b36e489

Browse files
committed
Merge pull request matyb#6 from hvolkmer/master.
Added AboutExceptions, AboutComparison and AboutEquality
2 parents be4997c + 6a84a83 commit b36e489

4 files changed

Lines changed: 302 additions & 0 deletions

File tree

‎koans/src/PathToEnlightment.xml‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@
100100
<koan name="enumsWithAttributes" />
101101
<koan name="enumsWithMethods" />
102102
</suite>
103+
<suite class="AboutExceptions">
104+
<koan name="catchCheckedExceptions" />
105+
<koan name="useFinally" />
106+
<koan name="finallyWithoutCatch" />
107+
<koan name="catchUncheckedExceptions" />
108+
<koan name="catchOrder" />
109+
</suite>
103110
<suite class="AboutMethodPreference">
104111
<koan name="methodPreferenceInt" />
105112
<koan name="methodPreferenceInteger" />
@@ -157,6 +164,22 @@
157164
lesson="With AutoBoxing, we can intermix as well" />
158165
<koan name="allPrimativesCanBeAutoboxed" lesson="All primatives can be autoboxed" />
159166
</suite>
167+
<suite class="AboutComparison">
168+
<koan name="compareObjects" />
169+
<koan name="makeObjectsComparable" />
170+
<koan name="makeObjectsComparableWithoutComparable" />
171+
</suite>
172+
<suite class="AboutEquality">
173+
<koan name="sameObject" />
174+
<koan name="equalObject" />
175+
<koan name="noObjectShouldbeEqualToNull" />
176+
<koan name="equalForOwnObjects" />
177+
<koan name="unequalForOwnObjects" />
178+
<koan name="unequalForOwnObjectsWithDifferentType" />
179+
<koan name="equalNullForOwnObjects" />
180+
<koan name="ownHashCode" />
181+
<koan name="ownHashCodeImplementationPartTwo" />
182+
</suite>
160183
<suite class="AboutFileIO">
161184
<koan name="fileObjectDoesntCreateFile" />
162185
<koan name="fileCreationAndDeletion" />
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package beginner;
2+
3+
import java.io.IOException;
4+
5+
import com.sandwich.koan.Koan;
6+
import static com.sandwich.koan.constant.KoanConstants.__;
7+
import static com.sandwich.util.Assert.assertEquals;
8+
9+
public class AboutExceptions {
10+
11+
private void doSutff() throws IOException {
12+
throw new IOException();
13+
}
14+
15+
@Koan
16+
public void catchCheckedExceptions() {
17+
String s;
18+
try {
19+
doSutff();
20+
s = "code run normally";
21+
} catch(IOException e) {
22+
s = "exception thrown";
23+
}
24+
assertEquals(s,__);
25+
}
26+
27+
@Koan
28+
public void useFinally() {
29+
String s = "";
30+
try {
31+
doSutff();
32+
s = "code run normally";
33+
} catch(IOException e) {
34+
s = "exception thrown";
35+
} finally {
36+
s += " and finally ran as well";
37+
}
38+
assertEquals(s,__);
39+
}
40+
41+
@Koan
42+
public void finallyWithoutCatch() {
43+
String s = "";
44+
try {
45+
s = "code run normally";
46+
} finally {
47+
s += " and finally ran as well";
48+
}
49+
assertEquals(s,__);
50+
}
51+
52+
private void doUncheckedStuff() {
53+
throw new RuntimeException();
54+
}
55+
56+
@Koan
57+
public void catchUncheckedExceptions() {
58+
// What do you need to do to catch the
59+
// unchecked exception?
60+
doUncheckedStuff();
61+
}
62+
63+
@SuppressWarnings("serial")
64+
static class ParentException extends Exception {}
65+
@SuppressWarnings("serial")
66+
static class ChildException extends ParentException {}
67+
68+
private void throwIt() throws ParentException {
69+
throw new ChildException();
70+
}
71+
72+
@Koan
73+
public void catchOrder() {
74+
String s = "";
75+
try {
76+
throwIt();
77+
} catch(ChildException e) {
78+
s = "ChildException";
79+
} catch(ParentException e) {
80+
s = "ParentException";
81+
}
82+
assertEquals(s, __);
83+
}
84+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package intermediate;
2+
3+
import static com.sandwich.koan.constant.KoanConstants.__;
4+
import static com.sandwich.util.Assert.assertEquals;
5+
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
9+
import com.sandwich.koan.Koan;
10+
11+
public class AboutComparison {
12+
13+
@Koan
14+
public void compareObjects() {
15+
String a = "abc";
16+
String b = "bcd";
17+
assertEquals(a.compareTo(b), __);
18+
assertEquals(a.compareTo(a), __);
19+
assertEquals(b.compareTo(a), __);
20+
}
21+
22+
static class Car implements Comparable<Car> {
23+
int horsepower;
24+
// For an explanation for this implementation look at
25+
// http://download.oracle.com/javase/6/docs/api/java/lang/Comparable.html#compareTo(T)
26+
public int compareTo(Car o) {
27+
return horsepower - o.horsepower;
28+
}
29+
30+
}
31+
32+
@Koan
33+
public void makeObjectsComparable() {
34+
Car vwbeetle = new Car();
35+
vwbeetle.horsepower = 50;
36+
Car porsche = new Car();
37+
porsche.horsepower = 300;
38+
assertEquals(vwbeetle.compareTo(porsche), __);
39+
}
40+
41+
static class RaceHorse {
42+
int speed;
43+
int age;
44+
@Override
45+
public String toString() {
46+
return "Speed: " + speed + " Age: " + age;
47+
}
48+
}
49+
static class HorseSpeedComparator implements Comparator<RaceHorse> {
50+
public int compare(RaceHorse o1, RaceHorse o2) {
51+
return o1.age - o2.age;
52+
}
53+
}
54+
static class HorseAgeComparator implements Comparator<RaceHorse> {
55+
public int compare(RaceHorse o1, RaceHorse o2) {
56+
return o1.speed - o2.speed;
57+
}
58+
}
59+
60+
@Koan
61+
public void makeObjectsComparableWithoutComparable() {
62+
RaceHorse lindy = new RaceHorse();
63+
lindy.age = 10; lindy.speed = 2;
64+
RaceHorse lightning = new RaceHorse();
65+
lightning.age = 2; lightning.speed = 10;
66+
RaceHorse slowy = new RaceHorse();
67+
slowy.age = 12; slowy.speed = 1;
68+
69+
RaceHorse[] horses = {lindy, slowy, lightning};
70+
71+
Arrays.sort(horses, new HorseAgeComparator());
72+
assertEquals(horses[0], __);
73+
Arrays.sort(horses, new HorseSpeedComparator());
74+
assertEquals(horses[0], __);
75+
}
76+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package intermediate;
2+
3+
import com.sandwich.koan.Koan;
4+
import static com.sandwich.koan.constant.KoanConstants.__;
5+
import static com.sandwich.util.Assert.assertEquals;
6+
7+
public class AboutEquality {
8+
9+
@Koan
10+
public void sameObject() {
11+
Object a = new Object();
12+
Object b = a;
13+
assertEquals(a == b, __);
14+
}
15+
16+
@Koan
17+
public void equalObject() {
18+
Integer a = new Integer(1);
19+
Integer b = new Integer(1);
20+
assertEquals(a.equals(b), __);
21+
assertEquals(b.equals(a), __);
22+
}
23+
24+
@Koan
25+
public void noObjectShouldbeEqualToNull() {
26+
assertEquals(new Object().equals(null), __);
27+
}
28+
29+
static class Car {
30+
private String name = "";
31+
private int horsepower = 0;
32+
public Car(String s, int p) {
33+
name = s; horsepower = p;
34+
}
35+
@Override
36+
public boolean equals(Object other) {
37+
// Change this implementation to match the equals contract
38+
// Car objects with same horsepower and name values should be considered equal
39+
// http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object)
40+
return false;
41+
}
42+
43+
@Override
44+
public int hashCode() {
45+
// see koan ownHashCode
46+
return super.hashCode();
47+
}
48+
}
49+
@Koan
50+
public void equalForOwnObjects() {
51+
Car car1 = new Car("Beetle", 50);
52+
Car car2 = new Car("Beetle", 50);
53+
// See line 37 for the task you have to solve
54+
assertEquals(car1.equals(car2), true);
55+
assertEquals(car2.equals(car1), true);
56+
}
57+
58+
@Koan
59+
public void unequalForOwnObjects() {
60+
Car car1 = new Car("Beetle", 50);
61+
Car car2 = new Car("Prosche", 300);
62+
// See line 37 for the task you have to solve
63+
assertEquals(car1.equals(car2), false);
64+
}
65+
66+
@Koan
67+
public void unequalForOwnObjectsWithDifferentType() {
68+
Car car1 = new Car("Beetle", 50);
69+
String s = "foo";
70+
// See line 37 for the task you have to solve
71+
assertEquals(car1.equals(s), false);
72+
}
73+
74+
@Koan
75+
public void equalNullForOwnObjects() {
76+
Car car1 = new Car("Beetle", 50);
77+
// See line 37 for the task you have to solve
78+
assertEquals(car1.equals(null), false);
79+
}
80+
81+
@Koan
82+
public void ownHashCode() {
83+
// As a general rule: When you override equals you should override
84+
// hash code
85+
// Read the hash code contract to figure out why
86+
// http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode()
87+
88+
// Implement a hash code version in line 47 so that the following assertions pass
89+
Car car1 = new Car("Beetle", 50);
90+
Car car2 = new Car("Beetle", 50);
91+
assertEquals(car1.equals(car2), true);
92+
assertEquals(car1.hashCode() == car2.hashCode(), true);
93+
}
94+
95+
static class Chicken {
96+
String color = "green";
97+
@Override
98+
public int hashCode() {
99+
return 4000;
100+
}
101+
@Override
102+
public boolean equals(Object other) {
103+
if(!(other instanceof Chicken))
104+
return false;
105+
return ((Chicken)other).color.equals(color);
106+
}
107+
}
108+
109+
@Koan
110+
public void ownHashCodeImplementationPartTwo() {
111+
Chicken chicken1 = new Chicken(); chicken1.color = "black";
112+
Chicken chicken2 = new Chicken();
113+
assertEquals(chicken1.equals(chicken2), __);
114+
assertEquals(chicken1.hashCode() == chicken2.hashCode(), __);
115+
// Does this still fit the hashCode contract? Why?
116+
// If it's valid why is this still not a good idea?
117+
}
118+
119+
}

0 commit comments

Comments
 (0)