Skip to content

Commit 0f2c6f3

Browse files
committed
adding updated data dir for backup/restore functionality
1 parent 7af08de commit 0f2c6f3

6 files changed

Lines changed: 349 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
@SuppressWarnings("unused")
9+
public class AboutCasting {
10+
11+
@Koan
12+
public void longPlusInt() {
13+
int a = 6;
14+
long b = 10;
15+
Object c = a + b;
16+
assertEquals(c, __);
17+
assertEquals(c instanceof Integer, __);
18+
assertEquals(c instanceof Long, __);
19+
}
20+
21+
@Koan
22+
public void forceIntTypecast() {
23+
long a = 2147483648L;
24+
// What happens if we force a long value into an int?
25+
int b = (int)a;
26+
assertEquals(b, __);
27+
}
28+
29+
@Koan
30+
public void implicitTypecast() {
31+
int a = 1;
32+
int b = Integer.MAX_VALUE;
33+
long c = a + b;
34+
assertEquals(c, __);
35+
}
36+
37+
class Parent { public String doThing() { return "parent style"; } }
38+
class Child extends Parent { public String doThing() { return "child style"; } }
39+
40+
@Koan
41+
public void downCastWithInerhitance() {
42+
Child child = new Child();
43+
Parent parent = child; // Why isn't there an explicit cast?
44+
assertEquals(parent instanceof Parent,__);
45+
assertEquals(child instanceof Parent,__);
46+
assertEquals(child instanceof Child,__);
47+
}
48+
49+
@Koan
50+
public void downCastAndPolymophism() {
51+
Child child = new Child();
52+
Parent parent = child;
53+
// Think about the result. Did you expect that? Why?
54+
// Think about inheritance, objects, classes and instances.
55+
assertEquals(parent.doThing(),__);
56+
assertEquals(child.doThing(),__);
57+
}
58+
59+
@Koan
60+
public void upCastWithInheritance() {
61+
Parent parent = new Child();
62+
Child child = (Child)parent; // Why do we need an explicit cast here?
63+
assertEquals(parent instanceof Parent,__);
64+
assertEquals(child instanceof Parent,__);
65+
assertEquals(child instanceof Child,__);
66+
}
67+
68+
@Koan
69+
public void upCastAndPolymophism() {
70+
Parent parent = new Child();
71+
Child child = (Child)parent;
72+
// Think about the result. Did you expect that? Why?
73+
// How is that different from above?
74+
assertEquals(parent.doThing(),__);
75+
assertEquals(child.doThing(),__);
76+
}
77+
78+
class SuperChild extends Child {
79+
public String doSuperChildThing() { return "Superchild"; }
80+
}
81+
82+
@Koan
83+
public void complicatedCast() {
84+
Parent parent = new SuperChild();
85+
// What do you need to do in order to call "doSuperChildThing"?
86+
assertEquals("Superchild", __);
87+
}
88+
89+
interface Sleepable {
90+
String sleep();
91+
}
92+
93+
class SleepChild extends Child implements Sleepable {
94+
public String sleep() { return "zzzz"; }
95+
}
96+
97+
@Koan
98+
public void complicatedCastWithInterface() {
99+
Parent parent = new SleepChild();
100+
// What do you need to do in order to call "sleep"?
101+
assertEquals("zzzz", __);
102+
}
103+
104+
105+
}
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package beginner;
2+
3+
import static com.sandwich.koan.constant.KoanConstants.__;
4+
import static com.sandwich.util.Assert.assertEquals;
5+
6+
import java.util.Collection;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
import com.sandwich.koan.Koan;
11+
12+
public class AboutInheritance {
13+
14+
class Parent {
15+
public String doStuff() { return "parent"; }
16+
}
17+
class Child extends Parent {
18+
public String doStuff() { return "child"; }
19+
public String doStuff(String s) { return s; }
20+
}
21+
22+
@Koan
23+
public void differenceBetweenOverloadingAndOverriding() {
24+
assertEquals(new Parent().doStuff(),__);
25+
assertEquals(new Child().doStuff(),__);
26+
assertEquals(new Child().doStuff("oh no"),__);
27+
}
28+
29+
abstract class ParentTwo {
30+
abstract public Collection<?> doStuff();
31+
}
32+
33+
class ChildTwo extends ParentTwo {
34+
public Collection<?> doStuff() { return Collections.emptyList(); };
35+
}
36+
37+
@Koan
38+
public void overridenMethodsMayReturnSubtype() {
39+
// What do you need to change in order to get rid of the type cast?
40+
// Why does this work?
41+
List<?> list = (List<?>)new ChildTwo().doStuff();
42+
assertEquals(list instanceof List, __);
43+
}
44+
}
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+
}
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)