Created
February 27, 2013 21:14
-
-
Save werand/5051788 to your computer and use it in GitHub Desktop.
Solution for the java puzzle from http://zeroturnaround.com/fun/magical-java-puzzle-pat-the-unicorns/
This file contains hidden or 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
/** | |
* Solution for the java puzzle from http://zeroturnaround.com/fun/magical-java-puzzle-pat-the-unicorns/ | |
*/ | |
public class Unicorn { | |
private static int currentLine = 0; | |
public static boolean pat() { | |
int line = getExecutionLineFromMagicalLand(); | |
boolean isUniquePat = line != currentLine; | |
currentLine = line; | |
return isUniquePat; | |
} | |
private static int getExecutionLineFromMagicalLand() { | |
Exception e = new Exception(); | |
e.fillInStackTrace(); | |
StackTraceElement[] stackTrace = e.getStackTrace(); | |
return stackTrace[2].getLineNumber(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment