Skip to content

Commit bbdbe72

Browse files
author
David Reed
committed
Expanding AboutPrimitives + Moving it before AboutStrings
1 parent 0f15df6 commit bbdbe72

4 files changed

Lines changed: 209 additions & 24 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.sandwich.util;
2+
3+
public class TypeUtils {
4+
5+
public static Class<?> getType(int value) {
6+
return int.class;
7+
}
8+
9+
public static Class<?> getType(long value) {
10+
return long.class;
11+
}
12+
13+
public static Class<?> getType(float value) {
14+
return float.class;
15+
}
16+
17+
public static Class<?> getType(double value) {
18+
return double.class;
19+
}
20+
21+
public static Class<?> getType(byte value) {
22+
return byte.class;
23+
}
24+
25+
public static Class<?> getType(char value) {
26+
return char.class;
27+
}
28+
29+
public static Class<?> getType(short value) {
30+
return short.class;
31+
}
32+
33+
public static Class<?> getType(Object value) {
34+
return value.getClass();
35+
}
36+
37+
}

‎koans/config/PathToEnlightenment.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<suite class="AboutAssertions"/>
99
<!-- TODO: AboutDataTypes... -->
1010
<suite class="AboutEquality"/>
11+
<suite class="AboutPrimitives" />
1112
<suite class="AboutStrings"/>
1213
<suite class="AboutObjects"/>
1314
<suite class="AboutLoops" />
@@ -20,7 +21,6 @@
2021
<suite class="AboutMethodPreference"/>
2122
<suite class="AboutOperators"/>
2223
<suite class="AboutArrays"/>
23-
<suite class="AboutPrimitives"/>
2424
</package>
2525
<package pkg="intermediate" name="Intermediate">
2626
<suite class="AboutAutoboxing"/>

‎koans/lib/koans.jar‎

1.18 KB
Binary file not shown.
Lines changed: 171 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,196 @@
11
package beginner;
22

3-
import static com.sandwich.koan.constant.KoanConstants.__;
4-
import static com.sandwich.util.Assert.assertEquals;
5-
63
import com.sandwich.koan.Koan;
74

5+
import static com.sandwich.koan.constant.KoanConstants.*;
6+
import static com.sandwich.util.Assert.*;
7+
import static com.sandwich.util.TypeUtils.*;
8+
89
public class AboutPrimitives {
9-
10+
1011
@Koan()
11-
public void byteSize() {
12-
assertEquals(Byte.SIZE, __);
12+
public void wholeNumbersAreOfTypeInt() {
13+
assertEquals(getType(1), __); // hint: int.class
1314
}
14-
15+
1516
@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, __);
1829
}
19-
30+
2031
@Koan
2132
public void integerSize() {
22-
assertEquals(Integer.SIZE, __);
33+
assertEquals(Integer.SIZE, __); // This is the amount of bits used to store an int
2334
}
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+
2553
@Koan
2654
public void longSize() {
2755
assertEquals(Long.SIZE, __);
2856
}
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+
3069
@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), __);
3689
}
37-
38-
// Floating Points
90+
3991
@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, __);
42102
}
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+
44162
@Koan
45163
public void doubleSize() {
46164
assertEquals(Double.SIZE, __);
47165
}
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+
48196
}

0 commit comments

Comments
 (0)