Fixing Java puzzlers
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package _02_expressive._01_Oddity;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 5). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class Oddity {
|
||||
public static boolean isOdd(int i) {
|
||||
return i % 2 == 1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(isOdd(-1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace change
|
||||
|
||||
import java.math.BigDecimal
|
||||
import std.io.*
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
// Easy to make BigDecimals user-friendly
|
||||
println(
|
||||
"2.00".bd - "1.00"
|
||||
)
|
||||
}
|
||||
|
||||
val String.bd = BigDecimal(this)
|
||||
|
||||
fun BigDecimal.minus(other : BigDecimal) = this.subtract(other)
|
||||
fun BigDecimal.minus(other : String) = subtract(other.bd) // this can be omitted
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package _02_expressive._02_Time_for_a_Change;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 7). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class Change {
|
||||
public static void main(String args[ ] ) {
|
||||
System.out.println(2.00 - 1.10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package _02_expressive._02_Time_for_a_Change;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 7). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class ChangeSolutions {
|
||||
public static void main(String args[ ] ) {
|
||||
// Poor solution - still uses binary floating-point!
|
||||
System.out.printf("%.2f%n", 2.00 - 1.10);
|
||||
|
||||
// Calculation in cents
|
||||
System.out.println((200 - 110) + " cents");
|
||||
|
||||
// BigDecimal
|
||||
System.out.println(new BigDecimal("2.00"). subtract(new BigDecimal("1.10")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package _02_expressive._03_Long_Division;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 9). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class LongDivision {
|
||||
public static void main(String[] args) {
|
||||
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
|
||||
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
|
||||
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace long.division
|
||||
|
||||
import std.io.*
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
// Problematic case does not compile
|
||||
// val MICROS_PER_DAY_ : Long = 24 * 60 * 60 * 1000 * 1000;
|
||||
// val MILLIS_PER_DAY_ : Long = 24 * 60 * 60 * 1000
|
||||
|
||||
// Solution:
|
||||
val MICROS_PER_DAY : Long = 24.lng * 60 * 60 * 1000 * 1000;
|
||||
val MILLIS_PER_DAY : Long = 24.lng * 60 * 60 * 1000
|
||||
|
||||
println(MICROS_PER_DAY / MILLIS_PER_DAY)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package _02_expressive._03_Long_Division;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 9). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class LongDivisionSolution {
|
||||
public static void main(String[] args) {
|
||||
final long MICROS_PER_DAY = 24L * 60 * 60 * 1000 * 1000;
|
||||
final long MILLIS_PER_DAY = 24L * 60 * 60 * 1000;
|
||||
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace long.division.solution
|
||||
|
||||
import std.io.*
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
val MICROS_PER_DAY : Long = 24.lng * 60 * 60 * 1000 * 1000;
|
||||
val MILLIS_PER_DAY : Long = 24.lng * 60 * 60 * 1000
|
||||
|
||||
println(MICROS_PER_DAY / MILLIS_PER_DAY)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package _02_expressive._04_Elementary;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 11). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class Elementary {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(12345 + 5432l);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace elementary
|
||||
|
||||
import std.io.*
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
// Problematic case does not compile
|
||||
// println(12345 + 5432l)
|
||||
|
||||
// Correct syntax:
|
||||
println(12345 + 5432.lng)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package _02_expressive._04_Elementary;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 11). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class ElementarySolution {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(12345 + 5432L);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package _02_expressive._05_The_Joy_of_Hex;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 13). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class JoyOfHex {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Long.toHexString(0x100000000L + 0xcafebabe));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace multicast
|
||||
|
||||
import std.io.*
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
println(-1.byt.chr.int)
|
||||
println((-1).byt.chr.int)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package _02_expressive._06_Multicast;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public class Mutlicast {
|
||||
public static void main(String[] args) {
|
||||
System.out.println((int) (char) (byte) -1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package _02_expressive._07_Swap_Meat;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 17). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class CleverSwap {
|
||||
public static void main(String[] args) {
|
||||
int x = 1984; // (0x7c0)
|
||||
int y = 2001; // (0x7d1)
|
||||
x ^= y ^= x ^= y;
|
||||
System.out.println("x = " + x + "; y = " + y);
|
||||
|
||||
// Solution: swap with a temp variable
|
||||
// Do not assign to the same variable more than once in a single expression.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace cleverswap
|
||||
|
||||
import std.io.*
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
var x = 1
|
||||
// Do not assign to the same variable more than once in a single expression.
|
||||
// Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 19). Pearson Education (USA). Kindle Edition.
|
||||
// x += (y += z)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package _02_expressive._08_Dos_Equis;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public class DosEquis {
|
||||
public static void main(String[] args) {
|
||||
char x = 'X';
|
||||
int i = 0;
|
||||
System.out.print(true ? x : 0);
|
||||
System.out.print(false ? i : x);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace dos.equis
|
||||
|
||||
import std.io.*
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
// Problematic case does not compile
|
||||
val x = 'X'
|
||||
val i = 0
|
||||
print(if (true) x else 0)
|
||||
print(if (false) 0 else x)
|
||||
|
||||
// Mixed computations do not compile:
|
||||
// val int : Int = if (true) x else 0
|
||||
// val char : Char = if (false) 0 else x
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package _02_expressive._09_Tweedledum;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public class Tweedledum {
|
||||
public static void main(String[] args) {
|
||||
byte x = 1;
|
||||
byte i = 1;
|
||||
x += i;
|
||||
// x = x + i;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package _02_expressive._10_Tweedledee;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public class Tweedledee {
|
||||
public static void main(String[] args) {
|
||||
Object x = 1;
|
||||
String i = "1";
|
||||
// x += i;
|
||||
x = x + i;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package _03_character._11_The_Last_Laugh;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 25). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class LastLaugh {
|
||||
public static void main(String args[]) {
|
||||
System.out.print("H" + "a");
|
||||
System.out.print('H' + 'a');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace last.laugh
|
||||
|
||||
import std.io.*
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
print("H" + "a")
|
||||
// Problematic case does not compile
|
||||
// System.out?.print('H' + 'a')
|
||||
|
||||
// Solution
|
||||
print('H'.toString() + 'a')
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package _03_character._12_ABC;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 27). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class Abc {
|
||||
public static void main(String[] args) {
|
||||
String letters = "ABC";
|
||||
char[] numbers = {'1', '2', '3'};
|
||||
System.out.println(letters + " easy as " + numbers);
|
||||
|
||||
// Solution:
|
||||
System.out.println(letters + " easy as ");
|
||||
System.out.println(numbers);
|
||||
// or
|
||||
System.out.println(letters + " easy as " + String.valueOf(numbers));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package _03_character._13_Animal_Farm;
|
||||
|
||||
/**
|
||||
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 29). Pearson Education (USA). Kindle Edition.
|
||||
*/
|
||||
public class AnimalFarm {
|
||||
public static void main(String[] args) {
|
||||
final String pig = "length: 10";
|
||||
final String dog = "length: " + pig.length();
|
||||
System.out.println("Animals are equal: " + pig == dog);
|
||||
|
||||
// Solution:
|
||||
System.out.println("Animals are equal: " + pig.equals(dog));
|
||||
// or
|
||||
System.out.println("Animals are equal: " + (pig.intern() == dog.intern()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace animal.farm
|
||||
|
||||
import std.io.*
|
||||
import std.*
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
val pig = "length: 10";
|
||||
val dog = "length: " + pig.length;
|
||||
println("Animals are equal: " + pig == dog);
|
||||
|
||||
// Solution:
|
||||
println("Animals are equal: " + (pig == dog));
|
||||
|
||||
// Note:
|
||||
println("Animals are equal: " + (pig identityEquals dog));
|
||||
}
|
||||
Reference in New Issue
Block a user