This is my solution to the puzzle described here: http://zeroturnaround.com/fun/magical-java-puzzle-pat-the-unicorns/
Last active
December 14, 2015 01:19
-
-
Save wytten/5005473 to your computer and use it in GitHub Desktop.
Unicorn solution
http://zeroturnaround.com/fun/magical-java-puzzle-pat-the-unicorns/
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
*.class | |
*~ | |
.project |
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
<project default="compile" basedir="."> | |
<property name="build.src" value="."/> | |
<property name="build.out" value="."/> | |
<target name="clean"> | |
<delete> | |
<fileset dir="." includes="*.class"/> | |
</delete> | |
</target> | |
<target name="compile"> | |
<javac srcdir="${build.src}" destdir="${build.out}" includeantruntime="false" /> | |
</target> | |
<target name="run" depends="compile"> | |
<java classpath="." className="MagicalLand" /> | |
</target> | |
</project> |
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
public class MagicalLand { | |
public static void main(String[] args) { | |
for (int i = 0; i < (Math.random() * 500) + 2; i++) { | |
if (Unicorn.pat()) { | |
System.out.println("UNICORN #1: PAT THIS UNICORN ONCE"); | |
} | |
} | |
for (int i = 0; i < (Math.random() * 500) + 2; i++) { | |
if (Unicorn.pat()) { | |
System.out.println("UNICORN #2: PAT THIS UNICORN ONCE"); | |
} | |
} | |
System.out.println("END OF PROGRAM"); | |
} | |
} |
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
public class Math { | |
public static double random() { | |
return (double) -1 / (double) 500; | |
} | |
} |
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
public class Unicorn { | |
static boolean pat() { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment