|
| 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 | +} |
0 commit comments