Skip to content

Commit 819527d

Browse files
committed
Added basic versions of AboutCasting and AboutPrimitves
1 parent 0b836a8 commit 819527d

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package beginner;
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+
public class AboutCasting {
9+
@Koan
10+
public void longPlusInt() {
11+
int a = 6;
12+
long b = 10;
13+
Object c = a + b;
14+
assertEquals(16, c);
15+
assertEquals(c instanceof Integer, __);
16+
assertEquals(c instanceof Long, __);
17+
}
18+
19+
@Koan
20+
public void forceIntTypecast() {
21+
long a = 2147483648L;
22+
// What happens if we force a long value into an int?
23+
int b = (int)a;
24+
assertEquals(b, __);
25+
}
26+
27+
@Koan
28+
public void implicitTypecast() {
29+
int a = 1;
30+
int b = Integer.MAX_VALUE;
31+
long c = a + b;
32+
assertEquals(c, __);
33+
}
34+
35+
class Parent { public String doThing() { return "parent style"; } }
36+
class Child extends Parent { public String doThing() { return "child style"; } }
37+
38+
@Koan
39+
public void downCastWithInerhitance() {
40+
Child child = new Child();
41+
Parent parent = child; // Why isn't there an explicit cast?
42+
assertEquals(parent instanceof Parent,__);
43+
assertEquals(child instanceof Parent,__);
44+
assertEquals(child instanceof Child,__);
45+
}
46+
47+
@Koan
48+
public void downCastAndPolymophism() {
49+
Child child = new Child();
50+
Parent parent = child;
51+
// Think about the result. Did you expect that? Why?
52+
// Think about inheritance, objects, classes and instances.
53+
assertEquals(parent.doThing(),__);
54+
assertEquals(child.doThing(),__);
55+
}
56+
57+
@Koan
58+
public void upCastWithInheritance() {
59+
Parent parent = new Child();
60+
Child child = (Child)parent; // Why do we need an explicit cast here?
61+
assertEquals(parent instanceof Parent,__);
62+
assertEquals(child instanceof Parent,__);
63+
assertEquals(child instanceof Child,__);
64+
}
65+
66+
@Koan
67+
public void upCastAndPolymophism() {
68+
Parent parent = new Child();
69+
Child child = (Child)parent;
70+
// Think about the result. Did you expect that? Why?
71+
// How is that different from above?
72+
assertEquals(parent.doThing(),__);
73+
assertEquals(child.doThing(),__);
74+
}
75+
76+
class SuperChild extends Child {
77+
public String doSuperChildThing() { return "Superchild"; }
78+
}
79+
80+
@Koan
81+
public void complicatedCast() {
82+
Parent parent = new SuperChild();
83+
// What do you need to do in order to call "doSuperChildThing"?
84+
assertEquals("Superchild", __);
85+
}
86+
87+
interface Sleepable {
88+
String sleep();
89+
}
90+
91+
class SleepChild extends Child implements Sleepable {
92+
public String sleep() { return "zzzz"; }
93+
}
94+
95+
@Koan
96+
public void complicatedCastWithInterface() {
97+
Parent parent = new SleepChild();
98+
// What do you need to do in order to call "sleep"?
99+
assertEquals("zzzz", __);
100+
}
101+
102+
103+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package beginner;
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+
public class AboutPrimitives {
9+
@Koan()
10+
public void byteSize() {
11+
assertEquals(Byte.SIZE, __);
12+
}
13+
14+
@Koan()
15+
public void shortSize() {
16+
assertEquals(Short.SIZE, __);
17+
}
18+
19+
@Koan
20+
public void integerSize() {
21+
assertEquals(Integer.SIZE, __);
22+
}
23+
24+
@Koan
25+
public void longSize() {
26+
assertEquals(Long.SIZE, __);
27+
}
28+
29+
@Koan
30+
public void charSizeAndValue() {
31+
// a char basically is an unsigned int
32+
assertEquals(Character.SIZE,__);
33+
assertEquals(Character.MIN_VALUE,__);
34+
assertEquals(Character.MAX_VALUE,__);
35+
}
36+
37+
// Floating Points
38+
@Koan
39+
public void floatSize() {
40+
assertEquals(Float.SIZE,__);
41+
}
42+
43+
@Koan
44+
public void doubleSize() {
45+
assertEquals(Double.SIZE, __);
46+
}
47+
}

0 commit comments

Comments
 (0)