Skip to content

Commit 1da739d

Browse files
committed
Added AboutEnums and AboutConditionals (basic versions. More to come...)
1 parent 0f2c6f3 commit 1da739d

3 files changed

Lines changed: 147 additions & 0 deletions

File tree

‎koans/src/PathToEnlightment.xml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@
6565
<Koan name="upCastAndPolymophism" />
6666
<Koan name="complicatedCast" />
6767
<Koan name="complicatedCastWithInterface" />
68+
</Suite>
69+
<Suite class="AboutConditionals">
70+
<Koan name="basicIfWithoutCurly" />
71+
<Koan name="basicIfElseWithoutCurly" />
72+
<Koan name="basicIfElseIfElseWithoutCurly" />
73+
<Koan name="nestedIfsWithoutCurlysAreReallyMisleading" />
74+
<Koan name="ifAsIntended" />
6875
</Suite>
6976
<Suite class="AboutInheritance">
7077
<Koan name="differenceBetweenOverloadingAndOverriding" />
@@ -75,6 +82,12 @@
7582
<Koan name="simpleConstructorOrder" />
7683
<Koan name="complexConstructorOrder" />
7784
</Suite>
85+
<Suite class="AboutEnums">
86+
<Koan name="basicEnums" />
87+
<Koan name="basicEnumsAccess" />
88+
<Koan name="enumsWithAttributes" />
89+
<Koan name="enumsWithMethods" />
90+
</Suite>
7891
<Suite class="AboutMethodPreference">
7992
<Koan name="methodPreferenceInt" />
8093
<Koan name="methodPreferenceInteger" />
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
9+
public class AboutConditionals {
10+
11+
@Koan
12+
public void basicIfWithoutCurly(){
13+
// Ifs without curly braces are ugly and not recommended but still valid:
14+
int x = 1;
15+
if (true)
16+
x++;
17+
assertEquals(x,__);
18+
}
19+
20+
@Koan
21+
public void basicIfElseWithoutCurly(){
22+
// Ifs without curly braces are ugly and not recommended but still valid:
23+
int x = 1;
24+
boolean secretBoolean = false;
25+
if (secretBoolean)
26+
x++;
27+
else
28+
x--;
29+
assertEquals(x,__);
30+
}
31+
32+
@Koan
33+
public void basicIfElseIfElseWithoutCurly(){
34+
int x = 1;
35+
boolean secretBoolean = false;
36+
boolean otherBooleanCondition = true;
37+
// Ifs without curly braces are ugly and not recommended but still valid:
38+
if (secretBoolean)
39+
x++;
40+
else if (otherBooleanCondition)
41+
x = 10;
42+
else
43+
x--;
44+
assertEquals(x,__);
45+
}
46+
47+
@Koan
48+
public void nestedIfsWithoutCurlysAreReallyMisleading() {
49+
// Why are these ugly you ask? Well, try for yourself
50+
int x = 1;
51+
boolean secretBoolean = false;
52+
boolean otherBooleanCondition = true;
53+
// Ifs without curly braces are ugly and not recommended but still valid:
54+
if (secretBoolean) x++;
55+
if (otherBooleanCondition) x = 10;
56+
else x--;
57+
// Where does this else belong to!?
58+
assertEquals(x,__);
59+
}
60+
61+
@Koan
62+
public void ifAsIntended() {
63+
boolean secretBoolean = true;
64+
int x = 1;
65+
if(secretBoolean) {
66+
x++;
67+
} else {
68+
x = 0;
69+
}
70+
// There are different opinions on where the curly braces go...
71+
// But as long as you put them here. You avoid problems as seen above.
72+
assertEquals(x,__);
73+
}
74+
75+
}

‎koans/src/beginner/AboutEnums.java‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 AboutEnums {
8+
9+
10+
enum Colors {
11+
Red, Blue, Green, Yellow // what happens if you add a ; here?
12+
// What happens if you type Red() instead?
13+
}
14+
15+
@Koan
16+
public void basicEnums() {
17+
Colors blue = Colors.Blue;
18+
assertEquals(blue == Colors.Blue, __);
19+
assertEquals(blue == Colors.Red, __);
20+
assertEquals(blue instanceof Colors, __);
21+
}
22+
23+
@Koan
24+
public void basicEnumsAccess() {
25+
Colors[] colorArray = Colors.values();
26+
assertEquals(colorArray[2], __);
27+
}
28+
29+
enum SkatSuits {
30+
Clubs(12), Spades(11), Hearts(10), Dimonds(9);
31+
SkatSuits(int v) { value = v; }
32+
private int value;
33+
}
34+
35+
@Koan
36+
public void enumsWithAttributes() {
37+
// value is private but we still can access it. Why?
38+
// Try moving the enum outside the AboutEnum class... What do you expect?
39+
// What happens?
40+
assertEquals(SkatSuits.Clubs.value > SkatSuits.Spades.value, __);
41+
}
42+
43+
enum OpticalMedia {
44+
CD(650), DVD(4300), BluRay(50000);
45+
OpticalMedia(int c) {
46+
capacityInMegaBytes = c;
47+
}
48+
int capacityInMegaBytes;
49+
int getCoolnessFactor() {
50+
return (capacityInMegaBytes - 1000) * 10;
51+
}
52+
}
53+
54+
@Koan
55+
public void enumsWithMethods() {
56+
assertEquals(OpticalMedia.CD.getCoolnessFactor(), __);
57+
assertEquals(OpticalMedia.BluRay.getCoolnessFactor(), __);
58+
}
59+
}

0 commit comments

Comments
 (0)