Skip to content

Commit aadbd55

Browse files
committed
Broken down AboutOperators to separate Arithmetic and Bitwise Operator Koans. Cleaned up conditionals.
1 parent e2d926c commit aadbd55

3 files changed

Lines changed: 150 additions & 112 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package beginner;
2+
3+
import com.sandwich.koan.Koan;
4+
5+
import static com.sandwich.koan.constant.KoanConstants.__;
6+
import static com.sandwich.util.Assert.assertEquals;
7+
8+
public class AboutArithmeticOperators {
9+
10+
@Koan
11+
public void simpleOperations() {
12+
assertEquals(1, __);
13+
assertEquals(1 + 1, __);
14+
assertEquals(2 + 3 * 4, __);
15+
assertEquals((2 + 3) * 4, __);
16+
assertEquals(2 * 3 + 4, __);
17+
assertEquals(2 - 3 + 4, __);
18+
assertEquals(2 + 4 / 2, __);
19+
assertEquals((2 + 4) / 2, __);
20+
}
21+
22+
@Koan
23+
public void notSoSimpleOperations() {
24+
assertEquals(1 / 2, __);
25+
assertEquals(3 / 2, __);
26+
assertEquals(1 % 2, __);
27+
assertEquals(3 % 2, __);
28+
}
29+
30+
@Koan
31+
public void minusMinusVariableMinusMinus() {
32+
int i = 1;
33+
assertEquals(--i, __);
34+
assertEquals(i, __);
35+
assertEquals(i--, __);
36+
assertEquals(i, __);
37+
}
38+
39+
@Koan
40+
public void plusPlusVariablePlusPlus() {
41+
int i = 1;
42+
assertEquals(++i, __);
43+
assertEquals(i, __);
44+
assertEquals(i++, __);
45+
assertEquals(i, __);
46+
}
47+
48+
@Koan
49+
public void timesAndDivInPlace() {
50+
int i = 1;
51+
i *= 2;
52+
assertEquals(i, __);
53+
i /= 2;
54+
assertEquals(i, __);
55+
}
56+
57+
}
Lines changed: 65 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,65 @@
1-
package beginner;
2-
3-
import com.sandwich.koan.Koan;
4-
5-
import static com.sandwich.koan.constant.KoanConstants.__;
6-
import static com.sandwich.util.Assert.assertEquals;
7-
8-
public class AboutOperators {
9-
10-
@Koan
11-
public void plusPlusVariablePlusPlus() {
12-
int i = 1;
13-
assertEquals(++i, __);
14-
assertEquals(i, __);
15-
assertEquals(i++, __);
16-
assertEquals(i, __);
17-
}
18-
19-
@Koan
20-
public void shortCircuit() {
21-
int i = 1;
22-
int a = 6; // Why did we use a variable here?
23-
// What happens if you replace 'a' with '6' below?
24-
// Try this with an IDE like Eclipse...
25-
if ((a < 9) || (++i < 8)) i = i + 1;
26-
assertEquals(i, __);
27-
}
28-
29-
@Koan
30-
public void fullAnd() {
31-
int i = 1;
32-
if (true & (++i < 8)) i = i + 1;
33-
assertEquals(i, __);
34-
}
35-
36-
@Koan
37-
public void shortCircuitAnd() {
38-
int i = 1;
39-
if (true && (i < -28)) i = i + 1;
40-
assertEquals(i, __);
41-
}
42-
43-
@Koan
44-
public void aboutXOR() {
45-
int i = 1;
46-
int a = 6;
47-
if ((a < 9) ^ false) i = i + 1;
48-
assertEquals(i, __);
49-
}
50-
51-
@Koan
52-
public void dontMistakeEqualsForEqualsEquals() {
53-
int i = 1;
54-
boolean a = false;
55-
if (a = true) i++;
56-
assertEquals(a, __);
57-
assertEquals(i, __);
58-
// How could you write the condition 'with a twist' to avoid this trap?
59-
}
60-
61-
@Koan
62-
public void aboutBitShiftingRightShift() {
63-
int rightShift = 8;
64-
rightShift = rightShift >> 1;
65-
assertEquals(rightShift, __);
66-
}
67-
68-
@Koan
69-
public void aboutBitShiftingLeftShift() {
70-
int leftShift = 0x80000000; // Is this number positive or negative?
71-
leftShift = leftShift << 1;
72-
assertEquals(leftShift, __);
73-
}
74-
75-
@Koan
76-
public void aboutBitShiftingRightUnsigned() {
77-
int rightShiftNegativeStaysNegative = 0x80000000;
78-
rightShiftNegativeStaysNegative = rightShiftNegativeStaysNegative >> 4;
79-
assertEquals(rightShiftNegativeStaysNegative, __);
80-
int unsignedRightShift = 0x80000000; // always fills with 0
81-
unsignedRightShift >>>= 4; // Just like +=
82-
assertEquals(unsignedRightShift, __);
83-
}
84-
85-
}
1+
package beginner;
2+
3+
import com.sandwich.koan.Koan;
4+
5+
import static com.sandwich.koan.constant.KoanConstants.__;
6+
import static com.sandwich.util.Assert.assertEquals;
7+
8+
public class AboutBitwiseOperators {
9+
10+
@Koan
11+
public void fullAnd() {
12+
int i = 1;
13+
if (true & (++i < 8)) i = i + 1;
14+
assertEquals(i, __);
15+
}
16+
17+
@Koan
18+
public void shortCircuitAnd() {
19+
int i = 1;
20+
if (true && (i < -28)) i = i + 1;
21+
assertEquals(i, __);
22+
}
23+
24+
@Koan
25+
public void aboutXOR() {
26+
int i = 1;
27+
int a = 6;
28+
if ((a < 9) ^ false) i = i + 1;
29+
assertEquals(i, __);
30+
}
31+
32+
@Koan
33+
public void dontMistakeEqualsForEqualsEquals() {
34+
int i = 1;
35+
boolean a = false;
36+
if (a = true) i++;
37+
assertEquals(a, __);
38+
assertEquals(i, __);
39+
// How could you write the condition 'with a twist' to avoid this trap?
40+
}
41+
42+
@Koan
43+
public void aboutBitShiftingRightShift() {
44+
int rightShift = 8;
45+
rightShift = rightShift >> 1;
46+
assertEquals(rightShift, __);
47+
}
48+
49+
@Koan
50+
public void aboutBitShiftingLeftShift() {
51+
int leftShift = 0x80000000; // Is this number positive or negative?
52+
leftShift = leftShift << 1;
53+
assertEquals(leftShift, __);
54+
}
55+
56+
@Koan
57+
public void aboutBitShiftingRightUnsigned() {
58+
int rightShiftNegativeStaysNegative = 0x80000000;
59+
rightShiftNegativeStaysNegative = rightShiftNegativeStaysNegative >> 4;
60+
assertEquals(rightShiftNegativeStaysNegative, __);
61+
int unsignedRightShift = 0x80000000; // always fills with 0
62+
unsignedRightShift >>>= 4; // Just like +=
63+
assertEquals(unsignedRightShift, __);
64+
}
65+
}

‎koans/src/beginner/AboutConditionals.java‎

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,38 @@
1010
public class AboutConditionals {
1111

1212
@Koan
13-
public void basicIfWithoutCurly() {
14-
// Ifs without curly braces are ugly and not recommended but still valid:
13+
public void basicIf() {
1514
int x = 1;
16-
if (true)
15+
if (true) {
1716
x++;
17+
}
1818
assertEquals(x, __);
1919
}
2020

2121
@Koan
22-
public void basicIfElseWithoutCurly() {
23-
// Ifs without curly braces are ugly and not recommended but still valid:
22+
public void basicIfElse() {
2423
int x = 1;
2524
boolean secretBoolean = false;
26-
if (secretBoolean)
25+
if (secretBoolean) {
2726
x++;
28-
else
27+
} else {
2928
x--;
29+
}
3030
assertEquals(x, __);
3131
}
3232

3333
@Koan
34-
public void basicIfElseIfElseWithoutCurly() {
34+
public void basicIfElseIfElse() {
3535
int x = 1;
3636
boolean secretBoolean = false;
3737
boolean otherBooleanCondition = true;
38-
// Ifs without curly braces are ugly and not recommended but still valid:
39-
if (secretBoolean)
38+
if (secretBoolean) {
4039
x++;
41-
else if (otherBooleanCondition)
40+
} else if (otherBooleanCondition) {
4241
x = 10;
43-
else
42+
} else {
4443
x--;
44+
}
4545
assertEquals(x, __);
4646
}
4747

@@ -52,24 +52,15 @@ public void nestedIfsWithoutCurlysAreReallyMisleading() {
5252
boolean secretBoolean = false;
5353
boolean otherBooleanCondition = true;
5454
// Ifs without curly braces are ugly and not recommended but still valid:
55-
if (secretBoolean) x++;
56-
if (otherBooleanCondition) x = 10;
57-
else x--;
58-
// Where does this else belong to!?
59-
assertEquals(x, __);
60-
}
61-
62-
@Koan
63-
public void ifAsIntended() {
64-
boolean secretBoolean = true;
65-
int x = 1;
6655
if (secretBoolean) {
6756
x++;
57+
}
58+
if (otherBooleanCondition) {
59+
x = 10;
6860
} else {
69-
x = 0;
61+
x--;
7062
}
71-
// There are different opinions on where the curly braces go...
72-
// But as long as you put them here. You avoid problems as seen above.
63+
// Where does this else belong to!?
7364
assertEquals(x, __);
7465
}
7566

@@ -142,7 +133,7 @@ public void switchStatementSwitchValues() {
142133
// Try different (primitive) types for 'c'
143134
// Which types do compile?
144135
// Does boxing work?
145-
byte c = 'a';
136+
char c = 'a';
146137
String result = "Basic ";
147138
switch (c) {
148139
case 'a':
@@ -153,4 +144,14 @@ public void switchStatementSwitchValues() {
153144
}
154145
assertEquals(result, __);
155146
}
147+
148+
@Koan
149+
public void shortCircuit() {
150+
int i = 1;
151+
int a = 6;
152+
// Why did we use a variable here?
153+
// What happens if you replace 'a' with '6' below?
154+
if ((a < 9) || (++i < 8)) i = i + 1;
155+
assertEquals(i, __);
156+
}
156157
}

0 commit comments

Comments
 (0)