|
1 | 1 | package beginner; |
2 | 2 |
|
3 | | -import static com.sandwich.koan.constant.KoanConstants.__; |
4 | | -import static com.sandwich.util.Assert.assertEquals; |
5 | | - |
6 | 3 | import com.sandwich.koan.Koan; |
7 | 4 |
|
| 5 | +import static com.sandwich.koan.constant.KoanConstants.*; |
| 6 | +import static com.sandwich.util.Assert.*; |
| 7 | +import static com.sandwich.util.TypeUtils.*; |
| 8 | + |
8 | 9 | public class AboutPrimitives { |
9 | | - |
| 10 | + |
10 | 11 | @Koan() |
11 | | - public void byteSize() { |
12 | | - assertEquals(Byte.SIZE, __); |
| 12 | + public void wholeNumbersAreOfTypeInt() { |
| 13 | + assertEquals(getType(1), __); // hint: int.class |
13 | 14 | } |
14 | | - |
| 15 | + |
15 | 16 | @Koan() |
16 | | - public void shortSize() { |
17 | | - assertEquals(Short.SIZE, __); |
| 17 | + public void primitivesOfTypeIntHaveAnObjectTypeInteger() { |
| 18 | + Object number = 1; |
| 19 | + assertEquals(getType(number), __); |
| 20 | + |
| 21 | + // Primitives can be automatically changed into their object type via a process called auto-boxing |
| 22 | + // We will explore this in more detail in intermediate.AboutAutoboxing |
| 23 | + } |
| 24 | + |
| 25 | + @Koan |
| 26 | + public void integersHaveAFairlyLargeRange() { |
| 27 | + assertEquals(Integer.MIN_VALUE, __); |
| 28 | + assertEquals(Integer.MAX_VALUE, __); |
18 | 29 | } |
19 | | - |
| 30 | + |
20 | 31 | @Koan |
21 | 32 | public void integerSize() { |
22 | | - assertEquals(Integer.SIZE, __); |
| 33 | + assertEquals(Integer.SIZE, __); // This is the amount of bits used to store an int |
23 | 34 | } |
24 | | - |
| 35 | + |
| 36 | + @Koan() |
| 37 | + public void wholeNumbersCanAlsoBeOfTypeLong() { |
| 38 | + assertEquals(getType(1l), __); |
| 39 | + } |
| 40 | + |
| 41 | + @Koan() |
| 42 | + public void primitivesOfTypeLongHaveAnObjectTypeLong() { |
| 43 | + Object number = 1l; |
| 44 | + assertEquals(getType(number), __); |
| 45 | + } |
| 46 | + |
| 47 | + @Koan |
| 48 | + public void longsHaveALargerRangeThanInts() { |
| 49 | + assertEquals(Long.MIN_VALUE, __); |
| 50 | + assertEquals(Long.MAX_VALUE, __); |
| 51 | + } |
| 52 | + |
25 | 53 | @Koan |
26 | 54 | public void longSize() { |
27 | 55 | assertEquals(Long.SIZE, __); |
28 | 56 | } |
29 | | - |
| 57 | + |
| 58 | + @Koan() |
| 59 | + public void wholeNumbersCanAlsoBeOfTypeShort() { |
| 60 | + assertEquals(getType((short) 1), __); // The '(short)' is called an explicit cast - to type 'short' |
| 61 | + } |
| 62 | + |
| 63 | + @Koan() |
| 64 | + public void primitivesOfTypeShortHaveAnObjectTypeShort() { |
| 65 | + Object number = (short) 1; |
| 66 | + assertEquals(getType(number), __); |
| 67 | + } |
| 68 | + |
30 | 69 | @Koan |
31 | | - public void charSizeAndValue() { |
32 | | - // a char basically is an unsigned int |
33 | | - assertEquals(Character.SIZE,__); |
34 | | - assertEquals(Character.MIN_VALUE,__); |
35 | | - assertEquals(Character.MAX_VALUE,__); |
| 70 | + public void shortsHaveASmallerRangeThanInts() { |
| 71 | + assertEquals(Short.MIN_VALUE, __); // hint: You'll need an explicit cast |
| 72 | + assertEquals(Short.MAX_VALUE, __); |
| 73 | + } |
| 74 | + |
| 75 | + @Koan() |
| 76 | + public void shortSize() { |
| 77 | + assertEquals(Short.SIZE, __); |
| 78 | + } |
| 79 | + |
| 80 | + @Koan() |
| 81 | + public void wholeNumbersCanAlsoBeOfTypeByte() { |
| 82 | + assertEquals(getType((byte) 1), __); |
| 83 | + } |
| 84 | + |
| 85 | + @Koan() |
| 86 | + public void primitivesOfTypeByteHaveAnObjectTypeByte() { |
| 87 | + Object number = (byte) 1; |
| 88 | + assertEquals(getType(number), __); |
36 | 89 | } |
37 | | - |
38 | | - // Floating Points |
| 90 | + |
39 | 91 | @Koan |
40 | | - public void floatSize() { |
41 | | - assertEquals(Float.SIZE,__); |
| 92 | + public void bytesHaveASmallerRangeThanShorts() { |
| 93 | + assertEquals(Byte.MIN_VALUE, __); |
| 94 | + assertEquals(Byte.MAX_VALUE, __); |
| 95 | + |
| 96 | + // Why would you use short or byte considering that you need to do explicit casts? |
| 97 | + } |
| 98 | + |
| 99 | + @Koan() |
| 100 | + public void byteSize() { |
| 101 | + assertEquals(Byte.SIZE, __); |
42 | 102 | } |
43 | | - |
| 103 | + |
| 104 | + @Koan() |
| 105 | + public void wholeNumbersCanAlsoBeOfTypeChar() { |
| 106 | + assertEquals(getType((char) 1), __); |
| 107 | + } |
| 108 | + |
| 109 | + @Koan |
| 110 | + public void singleCharactersAreOfTypeChar() { |
| 111 | + assertEquals(getType('a'), __); |
| 112 | + } |
| 113 | + |
| 114 | + @Koan() |
| 115 | + public void primitivesOfTypeCharHaveAnObjectTypeCharacter() { |
| 116 | + Object number = (char) 1; |
| 117 | + assertEquals(getType(number), __); |
| 118 | + } |
| 119 | + |
| 120 | + @Koan |
| 121 | + public void charsCanOnlyBePositive() { |
| 122 | + assertEquals((int) Character.MIN_VALUE, __); |
| 123 | + assertEquals((int) Character.MAX_VALUE, __); |
| 124 | + |
| 125 | + // Why did we cast MIN_VALUE and MAX_VALUE to int? Try it without the cast. |
| 126 | + } |
| 127 | + |
| 128 | + @Koan() |
| 129 | + public void charSize() { |
| 130 | + assertEquals(Character.SIZE, __); |
| 131 | + } |
| 132 | + |
| 133 | + @Koan() |
| 134 | + public void decimalNumbersAreOfTypeDouble() { |
| 135 | + assertEquals(getType(1.0), __); |
| 136 | + } |
| 137 | + |
| 138 | + @Koan() |
| 139 | + public void primitivesOfTypeDoubleCanBeDeclaredWithoutTheDecimalPoint() { |
| 140 | + assertEquals(getType(1d), __); |
| 141 | + } |
| 142 | + |
| 143 | + @Koan() |
| 144 | + public void primitivesOfTypeDoubleCanBeDeclaredWithExponents() { |
| 145 | + assertEquals(getType(1e3), __); |
| 146 | + assertEquals(1.0e3, __); |
| 147 | + assertEquals(1E3, __); |
| 148 | + } |
| 149 | + |
| 150 | + @Koan() |
| 151 | + public void primitivesOfTypeDoubleHaveAnObjectTypeDouble() { |
| 152 | + Object number = 1.0; |
| 153 | + assertEquals(getType(number), __); |
| 154 | + } |
| 155 | + |
| 156 | + @Koan |
| 157 | + public void doublesHaveALargeRange() { |
| 158 | + assertEquals(Double.MIN_VALUE, __); |
| 159 | + assertEquals(Double.MAX_VALUE, __); |
| 160 | + } |
| 161 | + |
44 | 162 | @Koan |
45 | 163 | public void doubleSize() { |
46 | 164 | assertEquals(Double.SIZE, __); |
47 | 165 | } |
| 166 | + |
| 167 | + @Koan() |
| 168 | + public void decimalNumbersCanAlsoBeOfTypeFloat() { |
| 169 | + assertEquals(getType(1f), __); |
| 170 | + } |
| 171 | + |
| 172 | + @Koan() |
| 173 | + public void primitivesOfTypeFloatCanBeDeclaredWithExponents() { |
| 174 | + assertEquals(getType(1e3f), __); |
| 175 | + assertEquals(1.0e3f, __); |
| 176 | + assertEquals(1E3f, __); |
| 177 | + } |
| 178 | + |
| 179 | + @Koan() |
| 180 | + public void primitivesOfTypeFloatHaveAnObjectTypeFloat() { |
| 181 | + Object number = 1f; |
| 182 | + assertEquals(getType(number), __); |
| 183 | + } |
| 184 | + |
| 185 | + @Koan |
| 186 | + public void floatsHaveASmallerRangeThanDoubles() { |
| 187 | + assertEquals(Float.MIN_VALUE, __); |
| 188 | + assertEquals(Float.MAX_VALUE, __); |
| 189 | + } |
| 190 | + |
| 191 | + @Koan |
| 192 | + public void floatSize() { |
| 193 | + assertEquals(Float.SIZE, __); |
| 194 | + } |
| 195 | + |
48 | 196 | } |
0 commit comments