Remove outdated version of puzzlers from the Kotlin repository. An up-to-date version is available at https://github.com/kotlin-projects/ex-puzzlers

This commit is contained in:
Dmitry Jemerov
2015-01-19 15:58:33 +01:00
parent c60797e306
commit 2e01c3b90a
90 changed files with 0 additions and 1447 deletions
-1
View File
@@ -1 +0,0 @@
exPuzzlers
-7
View File
@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AntConfiguration">
<defaultAnt bundledAnt="true" />
</component>
</project>
-21
View File
@@ -1,21 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="?*.properties" />
<entry name="?*.xml" />
<entry name="?*.gif" />
<entry name="?*.png" />
<entry name="?*.jpeg" />
<entry name="?*.jpg" />
<entry name="?*.html" />
<entry name="?*.dtd" />
<entry name="?*.tld" />
<entry name="?*.ftl" />
</wildcardResourcePatterns>
<annotationProcessing enabled="false" useClasspath="true" />
</component>
</project>
-5
View File
@@ -1,5 +0,0 @@
<component name="CopyrightManager">
<settings default="">
<module2copyright />
</settings>
</component>
-5
View File
@@ -1,5 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>
-10
View File
@@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/exPuzzlers.iml" filepath="$PROJECT_DIR$/exPuzzlers.iml" />
<module fileurl="file://$PROJECT_DIR$/../../stdlib/stdlib.iml" filepath="$PROJECT_DIR$/../../stdlib/stdlib.iml" />
</modules>
</component>
</project>
-5
View File
@@ -1,5 +0,0 @@
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>
-125
View File
@@ -1,125 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
-7
View File
@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
</component>
</project>
-14
View File
@@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/kotlin" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="stdlib" />
</component>
</module>
-36
View File
@@ -1,36 +0,0 @@
namespace std
inline fun <T> array(vararg array : T) = array
inline fun array(vararg array : Byte) = array
inline fun array(vararg array : Char) = array
inline fun array(vararg array : Short) = array
inline fun array(vararg array : Int) = array
inline fun array(vararg array : Long) = array
inline fun array(vararg array : Double) = array
inline fun array(vararg array : Float) = array
//fun Any?.identityEquals(other : Any?) = this === other
inline fun <T : Any> T?.sure() : T {
if (this == null)
throw NullPointerException()
return this;
}
namespace string {
fun String.replaceAllSubstrings(pattern : String, replacement : String) : String {
return (this as java.lang.String).replace(pattern as CharSequence, replacement as CharSequence).sure()
}
fun String.replaceAllWithRegex(pattern : String, replacement : String) : String {
return (this as java.lang.String).replaceAll(pattern, replacement).sure()
}
}
namespace jutils {
fun <T : Any> T.getJavaClass() : Class<T> {
return ((this as Object).getClass()) as Class<T>
}
}
@@ -1,14 +0,0 @@
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));
}
}
@@ -1,19 +0,0 @@
namespace change
import java.math.BigDecimal
import kotlin.io.*
fun main(args : Array<String>) {
// Easy to make BigDecimals user-friendly
println(
"2.00".bd - "1.00"
)
}
val String.bd : BigDecimal get() = BigDecimal(this)
fun BigDecimal.minus(other : BigDecimal) = this.subtract(other)
fun BigDecimal.minus(other : String) = subtract(other.bd) // this can be omitted
@@ -1,11 +0,0 @@
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);
}
}
@@ -1,20 +0,0 @@
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")));
}
}
@@ -1,12 +0,0 @@
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);
}
}
@@ -1,15 +0,0 @@
namespace long.division
import kotlin.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.toLong() * 60 * 60 * 1000 * 1000;
val MILLIS_PER_DAY : Long = 24.toLong() * 60 * 60 * 1000
println(MICROS_PER_DAY / MILLIS_PER_DAY)
}
@@ -1,12 +0,0 @@
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);
}
}
@@ -1,10 +0,0 @@
namespace long.division.solution
import kotlin.io.*
fun main(args : Array<String>) {
val MICROS_PER_DAY : Long = 24.toLong() * 60 * 60 * 1000 * 1000;
val MILLIS_PER_DAY : Long = 24.toLong() * 60 * 60 * 1000
println(MICROS_PER_DAY / MILLIS_PER_DAY)
}
@@ -1,11 +0,0 @@
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);
}
}
@@ -1,11 +0,0 @@
namespace elementary
import kotlin.io.*
fun main(args : Array<String>) {
// Problematic case does not compile
// println(12345 + 5432l)
// Correct syntax:
println(12345 + 5432.toLong())
}
@@ -1,11 +0,0 @@
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);
}
}
@@ -1,11 +0,0 @@
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));
}
}
@@ -1,8 +0,0 @@
namespace multicast
import kotlin.io.*
fun main(args : Array<String>) {
println(-1.toByte().toChar().toInt())
println((-1).toByte().toChar().toInt())
}
@@ -1,8 +0,0 @@
package _02_expressive._06_Multicast;
public class Mutlicast {
public static void main(String[] args) {
System.out.println((int) (char) (byte) -1);
}
}
@@ -1,16 +0,0 @@
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.
}
}
@@ -1,10 +0,0 @@
namespace cleverswap
import kotlin.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)
}
@@ -1,10 +0,0 @@
package _02_expressive._08_Dos_Equis;
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);
}
}
@@ -1,15 +0,0 @@
namespace dos.equis
import kotlin.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
}
@@ -1,10 +0,0 @@
package _02_expressive._09_Tweedledum;
public class Tweedledum {
public static void main(String[] args) {
byte x = 1;
byte i = 1;
x += i;
// x = x + i;
}
}
@@ -1,10 +0,0 @@
package _02_expressive._10_Tweedledee;
public class Tweedledee {
public static void main(String[] args) {
Object x = 1;
String i = "1";
// x += i;
x = x + i;
}
}
@@ -1,12 +0,0 @@
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');
}
}
@@ -1,12 +0,0 @@
namespace last.laugh
import kotlin.io.*
fun main(args : Array<String>) {
print("H" + "a")
// Problematic case does not compile
// System.out?.print('H' + 'a')
// Solution
print('H'.toString() + 'a')
}
@@ -1,19 +0,0 @@
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));
}
}
@@ -1,18 +0,0 @@
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()));
}
}
@@ -1,16 +0,0 @@
namespace animal.farm
import kotlin.io.*
import kotlin.*
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))
}
@@ -1,12 +0,0 @@
package _03_character._14_Escape_Rout;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 31). Pearson Education (USA). Kindle Edition.
*/
public class EscapeRout {
public static void main(String[ ] args) {
// \u0022 is the Unicode escape for double quote (")
System.out.println("a\u0022.length( ) + \u0022b".length( ));
}
}
@@ -1,12 +0,0 @@
namespace escape.rout
import kotlin.io.*
import kotlin.*
fun main(args : Array<String>) {
// \u0022 is the Unicode escape for double quote (")
println("a\u0022.length( ) + \u0022b".length)
// The actual string:
println("a\u0022.length( ) + \u0022b")
}
@@ -1,16 +0,0 @@
package _03_character._15_Hello_Whirled;
// Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 33). Pearson Education (USA). Kindle Edition.
/**
* Generated by the IBM IDL-to-Java compiler, version 1.0
* from F:\TestRoot\apps\a1\ units\include\PolicyHome.idl <-- DELETE THE SPACE BEFORE 'u'
* Wednesday, June 17, 1998 6:44:40 o'clock AM GMT+00:00
*/
public class Test {
public static void main(String[] args) {
System.out.print("Hell");
System.out.println("o world");
}
}
@@ -1,14 +0,0 @@
namespace hello.whirled
import kotlin.io.*
import kotlin.*
/**
* Generated by the IBM IDL-to-Java compiler, version 1.0
* from F:\TestRoot\apps\a1\units\include\PolicyHome.idl
* Wednesday, June 17, 1998 6:44:40 o'clock AM GMT+00:00
*/
fun main(args : Array<String>) {
print("Hell");
println("o world");
}
@@ -1,13 +0,0 @@
package _03_character._16_Line_Printer;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 35). Pearson Education (USA). Kindle Edition.
*/
public class LinePrinter {
public static void main(String[] args) {
// Note: \ u000A is Unicode representation of linefeed (LF) <-- DELETE THE SPACE BEFORE 'u'
char c = 0x000A;
System.out.println(c);
}
}
@@ -1,10 +0,0 @@
namespace line.printer
import kotlin.io.*
import kotlin.*
fun main(args : Array<String>) {
// Note: \u000A is Unicode representation of linefeed (LF)
val c : Char = 0x000A .toChar();
println(c);
}
@@ -1,18 +0,0 @@
package _03_character._17_Huh;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 37). Pearson Education (USA). Kindle Edition.
*/
\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020
\u0063\u006c\u0061\u0073\u0073\u0020\u0055\u0067\u006c\u0079
\u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020
\u0020\u0020\u0020\u0020\u0073\u0074\u0061\u0074\u0069\u0063
\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028
\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0020
\u0020\u0020\u0020\u0020\u0061\u0072\u0067\u0073\u0029\u007b
\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074
\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0020
\u0022\u0048\u0065\u006c\u006c\u006f\u0020\u0077\u0022\u002b
\u0022\u006f\u0072\u006c\u0064\u0022\u0029\u003b\u007d\u007d
@@ -1 +0,0 @@
// You must be kidding
@@ -1,16 +0,0 @@
package _03_character._18_String_Cheese;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 39). Pearson Education (USA). Kindle Edition.
*/
public class StringCheese {
public static void main(String[] args) {
byte[] bytes = new byte[256];
for (int i = 0; i < 256; i++)
bytes[i] = (byte) i;
String str = new String(bytes);
for (int i = 0, n = str.length(); i < n; i++)
System.out.print((int) str.charAt(i) + " ");
}
}
@@ -1,11 +0,0 @@
package _03_character._20_Whats_My_Class;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 43). Pearson Education (USA). Kindle Edition.
*/
public class Me {
public static void main(String[] args) {
System.out.println(Me.class.getName().replaceAll(".", "/") + ".class");
}
}
@@ -1,18 +0,0 @@
namespace whats.my.`class`
import kotlin.io.*
import kotlin.string.*
import kotlin.jutils.*
class Me() {
fun main() {
println(getJavaClass().getCanonicalName()?.replaceAllSubstrings(".", "/"))
// Regex is denoted explicitly
println(getJavaClass().getCanonicalName()?.replaceAllWithRegex(".", "/"))
}
}
fun main(args : Array<String>) {
Me().main()
}
@@ -1,13 +0,0 @@
package _03_character._21_Whats_My_Class_Take_2;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 45). Pearson Education (USA). Kindle Edition.
*/
public class MeToo {
public static void main(String[] args) {
System.out.println(MeToo.class.getName().
replaceAll("\\.", /*File.separator*/"\\") + ".class");
}
}
@@ -1,19 +0,0 @@
namespace whats.my.`class`.take2
import kotlin.io.*
import kotlin.string.*
import kotlin.jutils.*
import java.io.File;
class MeToo() {
fun main() {
println(getJavaClass().getCanonicalName()?.replaceAllSubstrings(".", /*File.separator.sure()*/"\\"))
// Regex is denoted explicitly
println(getJavaClass().getCanonicalName()?.replaceAllWithRegex(".", "\\"))
}
}
fun main(args : Array<String>) {
MeToo().main()
}
@@ -1,28 +0,0 @@
package _03_character._23_No_Pain_No_Gain;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 49). Pearson Education (USA). Kindle Edition.
*/
import java.util.Random;
public class Rhymes {
private static Random rnd = new Random();
public static void main(String[] args) {
StringBuffer word = null;
switch (rnd.nextInt(2)) {
case 1:
word = new StringBuffer('P');
case 2:
word = new StringBuffer('G');
default:
word = new StringBuffer('M');
}
word.append('a');
word.append('i');
word.append('n');
System.out.println(word);
}
}
@@ -1,33 +0,0 @@
namespace no.pain.no.gain
import kotlin.io.*
val rnd = java.util.Random();
fun main(args : Array<String>) {
// The erroneous code does not compile:
// var word : StringBuffer? = null
// when (rnd.nextInt(2)) {
// 1 => word = StringBuffer('P')
// 2 => word = StringBuffer('G')
// else => word = StringBuffer('M')
// }
// word?.append('a')
// word?.append('i')
// word?.append('n')
// println(word)
// A natural implementation can't possibly have two problems out of three:
// * fall-through cases
// * StringBuffer(int) called with 'P'
val word =
when (rnd.nextInt(2)) {
1 => StringBuffer().append('P')
2 => StringBuffer().append('G')
else => StringBuffer().append('M')
}
word?.append('a')
word?.append('i')
word?.append('n')
println(word)
}
@@ -1,14 +0,0 @@
package _04_loopy._24_Big_Delight_in_Every_Byte;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 53). Pearson Education (USA). Kindle Edition.
*/
public class BigDelight {
public static void main(String[] args) {
for (byte b = Byte.MIN_VALUE; b < Byte.MAX_VALUE; b++) {
if (b == 0x90)
System.out.print("Joy!");
}
}
}
@@ -1,43 +0,0 @@
namespace big.delight.in.every.byte
import kotlin.io.*
fun main(args : Array<String>) {
// for (b : Byte in Byte.MIN_VALUE..Byte.MAX_VALUE) {
// // Problematic code does not compile
// if (b == 0x90 .toByte())
// println("joy")
// }
for (b : Byte in Byte.MIN_VALUE to Byte.MAX_VALUE) {
// Problematic code does not compile
if (b == 0x90 .toByte())
println("joy")
}
}
trait IntCompatible<T> {
fun valueOf(i : Int) : T
fun next(current : T) : T
fun compareToInt(t : T, int : Int) : Int
}
class NumberRange<T : Comparable<T>>(val from : Int, val to : Int) : Range<Int>, Iterable<T>
where class object T : IntCompatible<T> {
override fun contains(item: Int) : Boolean = from <= item && item <= to
fun contains(item: T) : Boolean = T.compareToInt(item, from) >= 0 && T.compareToInt(item, to) <= 0
override fun iterator(): Iterator<T> = object : Iterator<T> {
private var current : T = T.valueOf(from)
override fun next() : T {
if (!hasNext) throw java.util.NoSuchElementException()
val result = current
current = T.next(current)
return result
}
override val hasNext : Boolean
get() = T.compareToInt(current, to) < 0
}
}
fun Byte.to(to : Byte) = NumberRange<Byte>(this.toInt(), to.toInt())
@@ -1,23 +0,0 @@
package _04_loopy._25_In_the_Loop;
/**
* Bloch, Joshua; Gafter, Neal (2005-06-24). Java™ Puzzlers: Traps, Pitfalls, and Corner Cases (p. 57). Pearson Education (USA). Kindle Edition.
*/
public class InTheLoop {
public static final int END = Integer.MAX_VALUE;
public static final int START = END - 100;
public static void main(String[] args) {
// int count = 0;
// for (int i = START; i <= END; i++)
// count++;
// System.out.println(count);
final int end = Integer.MAX_VALUE;
int start = end - 100;
int count = 0;
for (int i = start; i <= end; i++)
count++;
System.out.println("count = " + count);
}
}
@@ -1,14 +0,0 @@
namespace `in`.the.loop
import kotlin.io.*
val END = Integer.MAX_VALUE
val START = END - 100
fun main(args : Array<String>) {
var count = 0
// For-loop over integrals always terminates
for (i in START..END)
count++
println(count)
}
@@ -1,10 +0,0 @@
package _04_loopy._31_Ghost_of_Looper;
public class GhostOfLooper {
public static void main(String[] args) {
short i = -1;
while (i != 0)
i >>>= 1;
}
}
@@ -1,10 +0,0 @@
namespace ghost.of.looper
import kotlin.io.*
fun main(args : Array<String>) {
var i : Short = -1.toShort()
while (i != 1.toShort())
// Lots of magic made explicit:
i = (i.toInt() ushr 1).toShort()
}
@@ -1,11 +0,0 @@
package _04_loopy._32_Curse_of_Looper;
public class CurseOfLooper {
public static void main(String[] args) {
// Place your declarations for i and j here
Integer i = 128;
Integer j = 128;
while (i <= j && j <= i && i != j) {
}
}
}
@@ -1,19 +0,0 @@
namespace curse.of.loooper // BUG!!!
import kotlin.io.*
class Curse<T : Comparable<T>>() {
fun curse(i : T, j : T) {
while (i <= j && j <= i && i != j) {
}
}
}
fun main(args : Array<String>) {
// BUG:
// val i : Integer = (128 : Int?) as Integer
// val j : Integer = (128 : Int?) as Integer
// while (i <= j && j <= i && i != j) {
// }
Curse<Int>.curse(128, 128)
}
@@ -1,11 +0,0 @@
package _04_loopy._34_Down_for_the_Count;
public class Count {
public static void main(String[] args) {
final int START = 2000000000;
int count = 0;
for (float f = START; f < START + 50; f++)
count++;
System.out.println(count);
}
}
@@ -1,11 +0,0 @@
namespace down.`for`.the.count
import kotlin.io.*
fun main(args : Array<String>) {
// Problematic code does not compile
// val START : Int = 2000000000
// for (f : Float in START..(START + 50)) {
//
// }
}
@@ -1,15 +0,0 @@
package _05_exceptional._36_Indecision;
public class Indecisive {
public static void main(String[] args) {
System.out.println(decision());
}
static boolean decision() {
try {
return true;
} finally {
return false;
}
}
}
@@ -1,16 +0,0 @@
namespace indecision
import kotlin.io.*
fun main(args : Array<String>) {
println(decision())
}
fun decision() : Boolean {
try {
// Problematic code is illegal, as Java Puzzlers recommend:
// return true;
} finally {
return false;
}
}
@@ -1,27 +0,0 @@
package _05_exceptional._38_Unwelcome_Guest;
public class UnwelcomeGuest {
public static final long GUEST_USER_ID = -1;
// private static final long USER_ID;
// static {
// try {
// USER_ID = getUserIdFromEnvironment();
// } catch (IdUnavailableException e) {
// USER_ID = GUEST_USER_ID;
// System.out.println("Logging in as guest");
// }
// }
private static long getUserIdFromEnvironment()
throws IdUnavailableException {
throw new IdUnavailableException(); // Simulate an error
}
public static void main(String[] args) {
// System.out.println("User ID: " + USER_ID);
}
}
class IdUnavailableException extends Exception {
}
@@ -1,22 +0,0 @@
namespace unwelcome.guest
import kotlin.io.*
val GUEST_USER_ID = -1
val USER_ID =
try {
getUserIdFromEnvironment()
}
catch (e : UnsupportedOperationException) {
// catch (e : IdUnavailableException) { BUG
GUEST_USER_ID
}
fun getUserIdFromEnvironment() = throw UnsupportedOperationException()
//fun getUserIdFromEnvironment() = throw IdUnavailableException()
//class IdUnavailableException() : Ex() {}
fun main(args : Array<String>) {
println("User ID: " + USER_ID)
}
@@ -1,18 +0,0 @@
package _05_exceptional._39_The_Reluctant_Constructor;
public class Reluctant {
private Reluctant internalInstance = new Reluctant();
public Reluctant() throws Exception {
throw new Exception("I'm not coming out");
}
public static void main(String[] args) {
try {
Reluctant b = new Reluctant();
System.out.println("Surprise!");
} catch (Exception ex) {
System.out.println("I told you so");
}
}
}
@@ -1,20 +0,0 @@
namespace the.reluctant.constructor
import kotlin.io.*
public class Reluctant() {
val internalInstance = Reluctant() // Recursion is obvious
{
throw Exception("I'm not coming out")
}
}
fun main(args : Array<String>) {
try {
val b = Reluctant()
println("Surprise!")
}
catch (ex : Exception) {
println("I told you so")
}
}
@@ -1,24 +0,0 @@
package _05_exceptional._42_Thrown_for_a_Loop;
public class Loop {
public static void main(String[] args) {
int[][] tests = { { 6, 5, 4, 3, 2, 1 }, { 1, 2 },
{ 1, 2, 3 }, { 1, 2, 3, 4 }, { 1 } };
int n = 0;
try {
int i = 0;
while (true) {
if (thirdElementIsThree(tests[i++]))
n++;
}
} catch(ArrayIndexOutOfBoundsException e) {
// No more tests to process
}
System.out.println(n);
}
private static boolean thirdElementIsThree(int[] a) {
return a.length >= 3 & a[2] == 3;
}
}
@@ -1,32 +0,0 @@
namespace thrown.`for`.a.looop // BUG
import kotlin.io.*
import kotlin.*
fun iarr(vararg a : Int) = a // due to a BUG
fun main(args : Array<String>) {
val tests = array(
iarr(6, 5, 4, 3, 2, 1), iarr(1, 2),
iarr(1, 2, 3), iarr(1, 2, 3, 4), iarr(1)
)
var n = 0
try {
var i = 0
while (true) {
if (thirdElementIsThree(tests[i++]))
n++
}
}
catch (e : ArrayIndexOutOfBoundsException) {
// No more tests to process
}
println(n)
}
fun thirdElementIsThree(a : IntArray) =
// Problematic code does not compile
// a.size >= 3 & a[2] == 3
a.size >= 3 && a[2] == 3
@@ -1,34 +0,0 @@
package _06_classy._47_Well_Dog_My_Cats;
class Counter {
private static int count = 0;
public static final synchronized void increment() {
count++;
}
public static final synchronized int getCount() {
return count;
}
}
class Dog extends Counter {
public Dog() { }
public void woof() { increment(); }
}
class Cat extends Counter {
public Cat() { }
public void meow() { increment(); }
}
public class Ruckus {
public static void main(String[] args) {
Dog dogs[] = { new Dog(), new Dog() };
for (int i = 0; i < dogs.length; i++)
dogs[i].woof();
Cat cats[] = { new Cat(), new Cat(), new Cat() };
for (int i = 0; i < cats.length; i++)
cats[i].meow();
System.out.print(Dog.getCount() + " woofs and ");
System.out.println(Cat.getCount() + " meows");
}
}
@@ -1,38 +0,0 @@
namespace well.dog.my.cats
import kotlin.io.*
import kotlin.*
open class Counter {
class object {
private var count = 0
fun increment() { count++ }
fun getCount() = count
}
}
class Dog() : Counter {
// Class objects are not inherited:
// fun woof() { increment() }
fun woof() { Counter.increment() }
}
class Cat() : Counter {
// Class objects are not inherited:
// fun meow() { increment() }
fun meow() { Counter.increment() }
}
fun main(args : Array<String>) {
val dogs = array(Dog(), Dog())
for (dog in dogs)
dog.woof()
val cats = array(Cat(), Cat())
for (cat in cats)
cat.meow()
// Problematic code does not compile:
// println("${Dog.getCount()} woofs")
println("${Counter.getCount()} woofs")
// println("${Cat.getCount()} meows")
println("${Counter.getCount()} meows")
}
@@ -1,20 +0,0 @@
package _06_classy._48_All_I_Get_Is_Static;
class Dog {
public static void bark() {
System.out.print("woof ");
}
}
class Basenji extends Dog {
public static void bark() { }
}
public class Bark {
public static void main(String args[]) {
Dog woofer = new Dog();
Dog nipper = new Basenji();
woofer.bark();
nipper.bark();
}
}
@@ -1,28 +0,0 @@
namespace all.i.get.`is`.static
import kotlin.io.*
import kotlin.*
open class Dog() {
class object {
fun bark() {
print("woof")
}
}
}
class Basenji() : Dog() {
class object {
fun bark() { }
}
}
fun main(args : Array<String>) {
val woofer : Dog = Dog()
val nipper : Dog = Basenji()
// Problematic code does not compile:
// woofer.bark()
// nipper.bark()
Dog.bark()
Basenji.bark()
}
@@ -1,24 +0,0 @@
package _06_classy._53_Do_Your_Thing;
public class MyThing extends Thing {
private final int arg;
/*
* This constructor is illegal. Rewrite it so that it has the same
* effect but is legal.
*/
public MyThing() {
this((int)System.currentTimeMillis());
}
/*
* This constructor is illegal. Rewrite it so that it has the same
* effect but is legal.
*/
public MyThing(int arg) {
super(arg);
this.arg = arg;
}
}
@@ -1,14 +0,0 @@
namespace `do`.your.thing
import kotlin.io.*
import kotlin.*
import _06_classy._53_Do_Your_Thing.Thing
class MyThing(val arg : Int) : Thing(arg) {
}
fun main(args : Array<String>) {
println(MyThing(10).arg)
}
@@ -1,7 +0,0 @@
package _06_classy._53_Do_Your_Thing;
// This is meant to represent a library class. You must not modify it.
public class Thing {
public Thing(int i) {
}
}
@@ -1,11 +0,0 @@
package _06_classy._54_Null_and_Void;
public class Null {
public static void greet() {
System.out.println("Hello world!");
}
public static void main(String[] args) {
((Null) null).greet();
}
}
@@ -1,19 +0,0 @@
namespace `null`.and.void
import kotlin.io.*
import kotlin.*
class Null() {
class object {
fun greet() {
println("Hello world!")
}
}
}
fun main(args : Array<String>) {
// The problemati code does not compile:
// (null as Null).greet()
Null.greet()
}
@@ -1,21 +0,0 @@
package _06_classy._55_Creationism;
public class Creator {
public static void main(String[] args) {
// for (int i = 0; i < 100; i++)
// Creature creature = new Creature();
System.out.println(Creature.numCreated());
}
}
class Creature {
private static long numCreated = 0;
public Creature() {
numCreated++;
}
public static long numCreated() {
return numCreated;
}
}
@@ -1,21 +0,0 @@
namespace creationism
import kotlin.io.*
import kotlin.*
class Creature() {
class object {
var numCreated = 0
private set
}
{
// Creature.numCreated++
}
}
fun main(args : Array<String>) {
// for (i in 1..100)
// val creature = Creature()
}
@@ -1,18 +0,0 @@
package _07_library._56_Big_Problem;
import java.math.BigInteger;
public class BigProblem {
public static void main(String[] args) {
BigInteger fiveThousand = new BigInteger("5000");
BigInteger fiftyThousand = new BigInteger("50000");
BigInteger fiveHundredThousand
= new BigInteger("500000");
BigInteger total = BigInteger.ZERO;
total.add(fiveThousand);
total.add(fiftyThousand);
total.add(fiveHundredThousand);
System.out.println(total);
}
}
@@ -1,26 +0,0 @@
namespace big.problem
import kotlin.io.*
import kotlin.*
import java.math.BigInteger
fun main(args : Array<String>) {
val fiveThousand = "5000".bi()
val fiftyThousand = "50000".bi()
val fiveHundredThousand = "500000".bi()
val total : BigInteger = "0".bi()//BigInteger.ZERO
total + fiveThousand
total + fiftyThousand
total + fiveHundredThousand
println(total) // No surprise
var total1 : BigInteger = "0".bi()//BigInteger.ZERO
total1 += fiveThousand
total1 += fiftyThousand
total1 += fiveHundredThousand
println(total1) // Works
}
inline fun String.bi() : BigInteger = BigInteger(this)
inline fun BigInteger.plus(other : BigInteger) = this add other
@@ -1,15 +0,0 @@
package _07_library._59_Whats_the_Difference;
import java.util.*;
public class Differences {
public static void main(String[] args) {
int vals[] = { 789, 678, 567, 456, 345, 234, 123, 012 };
Set<Integer> diffs = new HashSet<Integer>();
for (int i = 0; i < vals.length; i++)
for (int j = i; j < vals.length; j++)
diffs.add(vals[i] - vals[j]);
System.out.println(diffs.size());
}
}
@@ -1,20 +0,0 @@
namespace whats.the.difference
import kotlin.io.*
import kotlin.*
import java.util.*
fun iarray(vararg a : Int) = a // BUG
fun main(args : Array<String>) {
// Problematic code does not compile
// val vals = iarray(789, 678, 567, 456, 345, 234, 123, 012)
val vals = iarray(789, 678, 567, 456, 345, 234, 123, 12)
val diffs = HashSet<Int>
for (i in vals.indices)
for (j in i..vals.lastIndex())
diffs.add(vals[i] - vals[j])
println(diffs.size())
}
fun IntArray.lastIndex() = size - 1