Java Puzzle 1: ClownsFeb 16, 2012
This is the first in a series of Java puzzles, that put your Java skills to the test, in a challenging and fun way!
A puzzle consists of some given Java code with a line in it that seems to be impossible to reach. It's up to you to find the hole in it, abuse a subtle behavior of Java to make execution reach that line anyways.
There almost aren't any rules; any cheating inside your code is allowed; it is the whole point of the puzzle. [Clarification: Cheating the environment is not]. You must run with the security manager enabled (
java -Djava.security.manager), otherwise it would be too easy (with setAccessible for example). [Update: Use common sense, and see the exact rules if you're in doubt].
How can you fit 20 clowns into a Volkswagen? Two classes are given: an empty Clown class, and a Volkswagen class to which you can add clowns. When you try to add a Clown, it is checked that it isn't already full. But if you just try hard enough, there's always room for some extra clowns...
Write a class that when executed pushes 20 clowns into the little car, and reaches the marked line. Here is one that won't really work, just to get you started:
You can copy-paste the code into your IDE, or get it from github (zip).
If you've solved it,
let me know (email or Twitter). If you're the first, you'll get... the honor of being the first. compare it to the solution posted here.
The solution and a new challenge will follow, so stay tuned!
Update: Don't post answers as comments here, they will be blocked by moderation. You may comment, but no spoiler. If you have an alternative solution, post it as a comment on the solution post
Update 2: Added clarification of the rules.