Skip to content

Commit 3903026

Browse files
committed
Merged pull request matyb#5 from hvolkmer/master.
Added more intermediate koans
2 parents 77baf4a + 8bd63a9 commit 3903026

3 files changed

Lines changed: 229 additions & 0 deletions

File tree

‎koans/src/PathToEnlightment.xml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,26 @@
157157
lesson="With AutoBoxing, we can intermix as well" />
158158
<koan name="allPrimativesCanBeAutoboxed" lesson="All primatives can be autoboxed" />
159159
</suite>
160+
<suite class="AboutFileIO">
161+
<koan name="fileObjectDoesntCreateFile" />
162+
<koan name="fileCreationAndDeletion" />
163+
<koan name="basicFileWritingAndReading" />
164+
<koan name="betterFileWritingAndReading" />
165+
<koan name="directChainingForReadingAndWriting" />
166+
</suite>
167+
<suite class="AboutInnerClasses">
168+
<koan name="creatingInnerClassInstance" />
169+
<koan name="creatingInnerClassInstanceWithOtherSyntax" />
170+
<koan name="accessingOuterClassMembers" />
171+
<koan name="innerClassesInMethods" />
172+
<koan name="innerClassesInMethodsThatEscape" />
173+
<koan name="creatingAnonymousInnerClasses" />
174+
<koan name="creatingAnonymousInnerClassesToImplementIntercae" />
175+
<koan name="innerClassAndIneritance" />
176+
<koan name="innerClassAndIneritanceOther" />
177+
<koan name="staticInnerClass" />
178+
<koan name="staticInnerClassFullyQualified" />
179+
</suite>
160180
<suite class="AboutSerialization">
161181
<koan name="simpleSerialization" />
162182
<koan name="customObjectSerialization" />
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package intermediate;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileReader;
6+
import java.io.FileWriter;
7+
import java.io.IOException;
8+
import java.io.PrintWriter;
9+
10+
import static com.sandwich.koan.constant.KoanConstants.__;
11+
import static com.sandwich.util.Assert.assertEquals;
12+
13+
import com.sandwich.koan.Koan;
14+
15+
public class AboutFileIO {
16+
17+
@Koan
18+
public void fileObjectDoesntCreateFile() {
19+
File f = new File("foo.txt");
20+
assertEquals(f.exists(), __);
21+
}
22+
23+
@Koan
24+
public void fileCreationAndDeletion() throws IOException {
25+
File f = new File("foo.txt");
26+
f.createNewFile();
27+
assertEquals(f.exists(), __);
28+
f.delete();
29+
assertEquals(f.exists(), __);
30+
}
31+
32+
@Koan
33+
public void basicFileWritingAndReading() throws IOException {
34+
File file = new File("file.txt");
35+
FileWriter fw = new FileWriter(file);
36+
fw.write("First line\nSecond line");
37+
fw.flush();
38+
fw.close();
39+
40+
char[] in = new char[50];
41+
int size = 0;
42+
FileReader fr = new FileReader(file);
43+
size = fr.read(in);
44+
// No flush necessary!
45+
fr.close();
46+
assertEquals(size, __);
47+
assertEquals(new String(in), __);
48+
file.delete();
49+
}
50+
51+
@Koan
52+
public void betterFileWritingAndReading() throws IOException {
53+
File file = new File("file.txt");
54+
FileWriter fw = new FileWriter(file);
55+
PrintWriter pw = new PrintWriter(fw);
56+
pw.println("First line");
57+
pw.println("Second line");
58+
pw.close();
59+
60+
FileReader fr = new FileReader(file);
61+
BufferedReader br = new BufferedReader(fr);
62+
assertEquals(br.readLine(), __); // first line
63+
assertEquals(br.readLine(), __); // second line
64+
assertEquals(br.readLine(), __); // what now?
65+
}
66+
67+
@Koan
68+
public void directChainingForReadingAndWriting() throws IOException {
69+
File file = new File("file.txt");
70+
PrintWriter pw = new PrintWriter(file);
71+
pw.println("1. line");
72+
pw.println("2. line");
73+
pw.close();
74+
75+
StringBuffer sb = new StringBuffer();
76+
// Add the loop to go through the file line by line and add the line
77+
// to the StringBuffer
78+
assertEquals(sb.toString(), "1. line\n2. line");
79+
}
80+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package intermediate;
2+
3+
import static com.sandwich.koan.constant.KoanConstants.__;
4+
import static com.sandwich.util.Assert.assertEquals;
5+
6+
import com.sandwich.koan.Koan;
7+
8+
interface Ignorable {
9+
String ignoreAll();
10+
}
11+
12+
public class AboutInnerClasses {
13+
14+
class Inner {
15+
public String doStuff() { return "stuff"; }
16+
public int returnOuter() { return x; }
17+
}
18+
19+
@Koan
20+
public void creatingInnerClassInstance() {
21+
Inner someObject = new Inner();
22+
assertEquals(someObject.doStuff(),__);
23+
}
24+
25+
@Koan
26+
public void creatingInnerClassInstanceWithOtherSyntax() {
27+
AboutInnerClasses.Inner someObject = this.new Inner();
28+
assertEquals(someObject.doStuff(),__);
29+
}
30+
31+
private int x = 10;
32+
33+
@Koan
34+
public void accessingOuterClassMembers() {
35+
Inner someObject = new Inner();
36+
assertEquals(someObject.returnOuter(),__);
37+
}
38+
39+
@Koan
40+
public void innerClassesInMethods() {
41+
class MethodInnerClass {
42+
int oneHundred() { return 100; }
43+
}; // <- Why do you need a semicolon here?
44+
assertEquals(new MethodInnerClass().oneHundred(), __);
45+
// Where can you use this class?
46+
}
47+
48+
class AnotherInnerClass {
49+
int thousand() { return 1000; }
50+
51+
AnotherInnerClass crazyReturn() {
52+
class SpecialInnerClass extends AnotherInnerClass {
53+
int thousand() {
54+
return 2000;
55+
}
56+
};
57+
return new SpecialInnerClass();
58+
}
59+
}
60+
61+
@Koan
62+
public void innerClassesInMethodsThatEscape() {
63+
AnotherInnerClass ic = new AnotherInnerClass();
64+
assertEquals(ic.thousand(), __);
65+
AnotherInnerClass theCrazyIC = ic.crazyReturn();
66+
assertEquals(theCrazyIC.thousand(), __);
67+
}
68+
69+
int theAnswer() { return 42; }
70+
71+
@Koan
72+
public void creatingAnonymousInnerClasses() {
73+
AboutInnerClasses anonymous = new AboutInnerClasses() {
74+
int theAnswer() { return 23; }
75+
};
76+
assertEquals(anonymous.theAnswer(), __);
77+
}
78+
79+
@Koan
80+
public void creatingAnonymousInnerClassesToImplementIntercae() {
81+
Ignorable ignorable = null; // Complete the code so that the statement below is correct.
82+
// Look at the koan above for inspiration
83+
assertEquals(ignorable.ignoreAll(), "SomeInterestingString");
84+
// Did you just created an object of an iterface type?
85+
// Or did you create a class that implemented this interface and
86+
// an object of that type?
87+
}
88+
89+
@Koan
90+
public void innerClassAndIneritance() {
91+
Inner someObject = new Inner();
92+
// The statement below is obvious...
93+
// Try to change the 'Inner' below to "AboutInnerClasses'
94+
// Why do you get an error?
95+
// What does that imply for inner classes and inheritance?
96+
assertEquals(someObject instanceof Inner, __);
97+
}
98+
99+
class OtherInner extends AboutInnerClasses { }
100+
101+
@Koan
102+
public void innerClassAndIneritanceOther() {
103+
OtherInner someObject = new OtherInner();
104+
// What do you expect here?
105+
// Complare this result with the last koan. What does that mean?
106+
assertEquals(someObject instanceof AboutInnerClasses, __);
107+
}
108+
109+
static class StaticInnerClass {
110+
public int importantNumber() { return 3; }
111+
}
112+
113+
@Koan
114+
public void staticInnerClass() {
115+
StaticInnerClass someObject = new StaticInnerClass();
116+
assertEquals(someObject.importantNumber(), __);
117+
// What happens if you try to access 'x' or 'theAnswer' from the outer class?
118+
// What does this mean for static inner classes?
119+
// Try to create a sub package of this package which is named 'StaticInnerClass'
120+
// Does it work? Why not?
121+
}
122+
123+
@Koan
124+
public void staticInnerClassFullyQualified() {
125+
AboutInnerClasses.StaticInnerClass someObject = new AboutInnerClasses.StaticInnerClass();
126+
assertEquals(someObject.importantNumber(), __);
127+
}
128+
129+
}

0 commit comments

Comments
 (0)