Skip to content

Commit c8f2568

Browse files
committed
Updating backup
1 parent f74da39 commit c8f2568

7 files changed

Lines changed: 600 additions & 0 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
@Koan
76+
public void basicSwitchStatement() {
77+
int i = 1;
78+
String result = "Basic ";
79+
switch(i) {
80+
case 1:
81+
result += "One";
82+
break;
83+
case 2:
84+
result += "Two";
85+
break;
86+
default:
87+
result += "Nothing";
88+
}
89+
assertEquals(result, __);
90+
}
91+
92+
@Koan
93+
public void switchStatementFallThrough() {
94+
int i = 1;
95+
String result = "Basic ";
96+
switch(i) {
97+
case 1:
98+
result += "One";
99+
case 2:
100+
result += "Two";
101+
default:
102+
result += "Nothing";
103+
}
104+
assertEquals(result, __);
105+
}
106+
107+
@Koan
108+
public void switchStatementCrazyFallThrough() {
109+
int i = 5;
110+
String result = "Basic ";
111+
switch(i) {
112+
case 1:
113+
result += "One";
114+
default:
115+
result += "Nothing";
116+
case 2:
117+
result += "Two";
118+
}
119+
assertEquals(result, __);
120+
}
121+
122+
@Koan
123+
public void switchStatementConstants() {
124+
int i = 5;
125+
// What happens if you remove the 'final' modifier?
126+
// What does this mean for case values?
127+
final int caseOne = 1;
128+
String result = "Basic ";
129+
switch(i) {
130+
case caseOne:
131+
result += "One";
132+
break;
133+
default:
134+
result += "Nothing";
135+
}
136+
assertEquals(result, __);
137+
}
138+
139+
@Koan
140+
public void switchStatementSwitchValues() {
141+
// Try different (primitive) types for 'c'
142+
// Which types do compile?
143+
// Does boxing work?
144+
byte c = 'a';
145+
String result = "Basic ";
146+
switch(c) {
147+
case 'a':
148+
result += "One";
149+
break;
150+
default:
151+
result += "Nothing";
152+
}
153+
assertEquals(result, __);
154+
}
155+
}
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+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 AboutLoops {
10+
11+
@Koan
12+
public void basicForLoop() {
13+
String s = "";
14+
for(int i = 0; i < 5; i++) {
15+
s += i + " ";
16+
}
17+
assertEquals(s, __);
18+
}
19+
20+
@Koan
21+
public void basicForLoopWithTwoVariables() {
22+
String s = "";
23+
for(int i = 0, j = 10; i < 5 && j > 5; i++, j--) {
24+
s += i + " " + j + " ";
25+
}
26+
assertEquals(s, __);
27+
}
28+
29+
@Koan
30+
public void extendedForLoop() {
31+
int[] is = {1,2,3,4};
32+
String s = "-";
33+
for(int j : is) {
34+
s += "." + j;
35+
}
36+
assertEquals(s, __);
37+
}
38+
39+
@Koan
40+
public void whileLoop() {
41+
int result = 0;
42+
while(result < 3) {
43+
result++;
44+
}
45+
assertEquals(result, __);
46+
}
47+
48+
@Koan
49+
public void doLoop() {
50+
int result = 0;
51+
do {
52+
result++;
53+
} while(false);
54+
assertEquals(result, __);
55+
}
56+
57+
@Koan
58+
public void extendedForLoopBreak() {
59+
String[] sa = {"Dog", "Cat", "Tiger" };
60+
int count = 0;
61+
for(String current : sa) {
62+
if("Cat".equals(current)) {
63+
break;
64+
}
65+
count++;
66+
}
67+
assertEquals(count, __);
68+
}
69+
70+
@Koan
71+
public void extendedForLoopContinue() {
72+
String[] sa = {"Dog", "Cat", "Tiger" };
73+
int count = 0;
74+
for(String current : sa) {
75+
if("Dog".equals(current)) {
76+
continue;
77+
} else {
78+
count++;
79+
}
80+
}
81+
assertEquals(count, __);
82+
}
83+
84+
85+
@Koan
86+
public void forLoopContinueLabel() {
87+
int count = 0;
88+
outerLabel:
89+
for(int i = 0; i < 5; i++) {
90+
for(int j = 0; j < 5; j++)
91+
{
92+
count++;
93+
if(count > 2) {
94+
continue outerLabel;
95+
}
96+
}
97+
count += 10;
98+
}
99+
// What does continue with a label mean?
100+
// What gets executed? Where does the program flow continue?
101+
assertEquals(count, __);
102+
}
103+
104+
@Koan
105+
public void forLoopBreakLabel() {
106+
int count = 0;
107+
outerLabel:
108+
for(int i = 0; i < 5; i++) {
109+
for(int j = 0; j < 5; j++)
110+
{
111+
count++;
112+
if(count > 2) {
113+
break outerLabel;
114+
}
115+
}
116+
count += 10;
117+
}
118+
// What does break with a label mean?
119+
// What gets executed? Where does the program flow continue?
120+
assertEquals(count, __);
121+
}
122+
123+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 AboutStrings {
9+
10+
@Koan
11+
public void implicitStrings(){
12+
Class<?> c = "just a plain ole string".getClass();
13+
assertEquals(c, __);
14+
}
15+
16+
@Koan
17+
public void newString(){
18+
// very rarely if ever should Strings be created explicitly in
19+
// practice - generally it is redundant
20+
String string = new String();
21+
String empty = "";
22+
assertEquals(string.equals(empty), __);
23+
}
24+
25+
@Koan
26+
public void newStringIsRedundant(){
27+
String stringInstance = "zero";
28+
String stringReference = new String(stringInstance);
29+
assertEquals(stringInstance.equals(stringReference), __);
30+
}
31+
32+
@Koan
33+
public void newStringIsNotIdentitical(){
34+
String stringInstance = "zero";
35+
String stringReference = new String(stringInstance);
36+
assertEquals(stringInstance == stringReference, __);
37+
}
38+
39+
@Koan
40+
public void stringConcatination(){
41+
String one = "one";
42+
String space = " ";
43+
String two = "two";
44+
assertEquals(one+space+two, __);
45+
}
46+
47+
@Koan
48+
public void efficientStringConcatenation(){
49+
// the above implicit string concatenation looks nice - but, it is expensive.
50+
// it creates a new string instance on each concatenation. here's a better alternative:
51+
assertEquals(new StringBuilder("one").append(" ").append("two").toString(), __);
52+
}
53+
}

0 commit comments

Comments
 (0)