Skip to content

Commit c478307

Browse files
committed
Added more koans: AboutConstructors, AboutInheritance, AboutMethodPreference, AboutOperators
1 parent 1164cff commit c478307

4 files changed

Lines changed: 195 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package beginner;
2+
3+
4+
import com.sandwich.koan.Koan;
5+
import static com.sandwich.koan.constant.KoanConstants.__;
6+
import static com.sandwich.util.Assert.assertEquals;
7+
8+
public class AboutConstructors {
9+
10+
class A {
11+
String someString = "a";
12+
public A() { someString+= "x"; }
13+
14+
}
15+
16+
class B extends A {
17+
public B() { someString += "g"; };
18+
}
19+
20+
@Koan
21+
public void simpleConstructorOrder(){
22+
assertEquals(new B().someString, __);
23+
}
24+
25+
class Aa {
26+
String someString = "a";
27+
public Aa() { someString+= "x"; }
28+
public Aa(String s) {
29+
someString += s;
30+
}
31+
}
32+
33+
class Bb extends Aa {
34+
public Bb() { super("Boo"); someString += "g"; };
35+
}
36+
37+
@Koan
38+
public void complexConstructorOrder(){
39+
assertEquals(new B().someString, __);
40+
}
41+
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package beginner;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
6+
import com.sandwich.koan.Koan;
7+
import static com.sandwich.koan.constant.KoanConstants.__;
8+
import static com.sandwich.util.Assert.assertEquals;
9+
10+
public class AboutInheritance {
11+
12+
class Parent {
13+
public String doStuff() { return "parent"; }
14+
}
15+
class Child extends Parent {
16+
public String doStuff() { return "child"; }
17+
public String doStuff(String s) { return s; }
18+
}
19+
20+
@Koan
21+
public void differenceBetweenOverloadingAndOverriding() {
22+
assertEquals(new Parent().doStuff(),__);
23+
assertEquals(new Child().doStuff(),__);
24+
assertEquals(new Child().doStuff("oh no"),__);
25+
}
26+
27+
abstract class ParentTwo {
28+
abstract public Collection doStuff();
29+
}
30+
31+
class ChildTwo extends ParentTwo {
32+
public Collection doStuff() { return null; };
33+
}
34+
35+
@Koan
36+
public void overridenMethodsMayReturnSubtype() {
37+
// What do you need to change in order to get rid of the type cast?
38+
// Why does this work?
39+
ArrayList arraylist = (ArrayList)new ChildTwo().doStuff();
40+
assertEquals(arraylist instanceof ArrayList, true);
41+
}
42+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package beginner;
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 AboutMethodPreference {
8+
9+
class A {
10+
public String doStuff(int i) { return "int"; }
11+
public String doStuff(Integer i) { return "Integer"; }
12+
public String doStuff(Object i) { return "Object"; }
13+
public String doStuff(int...i) { return "int vararg"; }
14+
}
15+
16+
@Koan
17+
public void methodPreferenceInt() {
18+
assertEquals(new A().doStuff(1), __);
19+
}
20+
21+
@Koan
22+
public void methodPreferenceInteger() {
23+
assertEquals(new A().doStuff(Integer.valueOf(1)), __);
24+
}
25+
26+
@Koan
27+
public void methodPreferenceLong() {
28+
long l = 1;
29+
assertEquals(new A().doStuff(l), __);
30+
}
31+
32+
@Koan
33+
public void methodPreferenceBoxedLong() {
34+
Long l = Long.valueOf(1);
35+
assertEquals(new A().doStuff(l), __);
36+
}
37+
38+
@Koan
39+
public void methodPreferenceDouble() {
40+
Double l = Double.valueOf(1);
41+
assertEquals(new A().doStuff(l), __);
42+
}
43+
44+
@Koan
45+
public void methodPreferenceMore() {
46+
// What happens if you change 'Integer' to 'Double'
47+
// Does this explain 'methodPreferenceDouble'?
48+
// Think about why this happens?
49+
assertEquals(new A().doStuff(1,Integer.valueOf(2)), __);
50+
}
51+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package beginner;
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 AboutOperators {
8+
9+
@Koan
10+
public void plusPlusVariablePlusPlus(){
11+
int i = 1;
12+
assertEquals(++i,__);
13+
assertEquals(i,__);
14+
assertEquals(i++,__);
15+
assertEquals(i,__);
16+
}
17+
18+
@Koan
19+
public void shortCircuit() {
20+
int i = 1;
21+
int a = 6; // Why did we use a variable here?
22+
// What happens if you replace 'a' with '6' below?
23+
// Try this with an IDE like Eclipse...
24+
if ( (a < 9 ) || (++i < 8) ) i = i + 1;
25+
assertEquals(i,__);
26+
}
27+
28+
@Koan
29+
public void fullAnd(){
30+
int i = 1;
31+
if ( true & (++i < 8) ) i = i + 1;
32+
assertEquals(i,__);
33+
}
34+
35+
@Koan
36+
public void shortCircuitAnd(){
37+
int i = 1;
38+
if ( true && (i < -28) ) i = i + 1;
39+
assertEquals(i,__);
40+
}
41+
42+
@Koan
43+
public void aboutXOR() {
44+
int i = 1;
45+
int a = 6;
46+
if ( (a < 9 ) ^ false) i = i + 1;
47+
assertEquals(i,__);
48+
}
49+
50+
@Koan
51+
public void dontMistakeEqualsForEqualsEquals() {
52+
int i = 1;
53+
boolean a = false;
54+
if (a = true) i++;
55+
assertEquals(a, __);
56+
assertEquals(i,__);
57+
// How could you write the condition 'with a twist' to avoid this trap?
58+
}
59+
60+
}

0 commit comments

Comments
 (0)