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