Skip to content

Commit

Permalink
Expanding AboutPrimitives + Moving it before AboutStrings
Browse files Browse the repository at this point in the history
  • Loading branch information
David Reed committed Aug 24, 2012
1 parent 0f15df6 commit bbdbe72
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 24 deletions.
37 changes: 37 additions & 0 deletions koans-lib/src/com/sandwich/util/TypeUtils.java
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();
}

}
2 changes: 1 addition & 1 deletion koans/config/PathToEnlightenment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<suite class="AboutAssertions"/>
<!-- TODO: AboutDataTypes... -->
<suite class="AboutEquality"/>
<suite class="AboutPrimitives" />
<suite class="AboutStrings"/>
<suite class="AboutObjects"/>
<suite class="AboutLoops" />
Expand All @@ -20,7 +21,6 @@
<suite class="AboutMethodPreference"/>
<suite class="AboutOperators"/>
<suite class="AboutArrays"/>
<suite class="AboutPrimitives"/>
</package>
<package pkg="intermediate" name="Intermediate">
<suite class="AboutAutoboxing"/>
Expand Down
Binary file modified koans/lib/koans.jar
Binary file not shown.
194 changes: 171 additions & 23 deletions koans/src/beginner/AboutPrimitives.java
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, __);
}

}

0 comments on commit bbdbe72

Please sign in to comment.