forked from matyb/java-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expanding AboutPrimitives + Moving it before AboutStrings
- Loading branch information
David Reed
committed
Aug 24, 2012
1 parent
0f15df6
commit bbdbe72
Showing
4 changed files
with
209 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.sandwich.util; | ||
|
||
public class TypeUtils { | ||
|
||
public static Class<?> getType(int value) { | ||
return int.class; | ||
} | ||
|
||
public static Class<?> getType(long value) { | ||
return long.class; | ||
} | ||
|
||
public static Class<?> getType(float value) { | ||
return float.class; | ||
} | ||
|
||
public static Class<?> getType(double value) { | ||
return double.class; | ||
} | ||
|
||
public static Class<?> getType(byte value) { | ||
return byte.class; | ||
} | ||
|
||
public static Class<?> getType(char value) { | ||
return char.class; | ||
} | ||
|
||
public static Class<?> getType(short value) { | ||
return short.class; | ||
} | ||
|
||
public static Class<?> getType(Object value) { | ||
return value.getClass(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,196 @@ | ||
package beginner; | ||
|
||
import static com.sandwich.koan.constant.KoanConstants.__; | ||
import static com.sandwich.util.Assert.assertEquals; | ||
|
||
import com.sandwich.koan.Koan; | ||
|
||
import static com.sandwich.koan.constant.KoanConstants.*; | ||
import static com.sandwich.util.Assert.*; | ||
import static com.sandwich.util.TypeUtils.*; | ||
|
||
public class AboutPrimitives { | ||
|
||
@Koan() | ||
public void byteSize() { | ||
assertEquals(Byte.SIZE, __); | ||
public void wholeNumbersAreOfTypeInt() { | ||
assertEquals(getType(1), __); // hint: int.class | ||
} | ||
|
||
@Koan() | ||
public void shortSize() { | ||
assertEquals(Short.SIZE, __); | ||
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, __); | ||
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 charSizeAndValue() { | ||
// a char basically is an unsigned int | ||
assertEquals(Character.SIZE,__); | ||
assertEquals(Character.MIN_VALUE,__); | ||
assertEquals(Character.MAX_VALUE,__); | ||
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), __); | ||
} | ||
|
||
// Floating Points | ||
|
||
@Koan | ||
public void floatSize() { | ||
assertEquals(Float.SIZE,__); | ||
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, __); | ||
} | ||
|
||
} |