As big fans of the book “Java Puzzlers” by Joshua Bloch and Neal Gafter, we’re always looking for new Java puzzles to solve. Since we haven’t seen good ones in a while, and as a tribute to their Java brainteasers we’ve decided to add a puzzle of our own. In their own words: “By working through the puzzles, you will become less likely to fall prey to these dangers in your code and more likely to spot them in code that you are reviewing or revising”. And besides, it’s also really fun. To spice it up a bit more we’ve added a $250 Amazon gift card to the one developer who manages to solve it. In one move. And give the right explanation as to what exactly happened there. We hope your kung fu is strong today.
Two teams are playing basketball, each is scoring points against the other and then suddenly – boom and game over. The project structure is pretty simple:
. +-- Main.java +-- simulator | +-- MatchSimulator.java | +-- SimulationVerifier.java +-- data | +-- BoxScore.java | +-- BasketballMatch.java
Two game simulator threads invoked by the main class, a simple data structure to hold the scores and the rest is up to you.
1. Head over to: https://github.com/takipi/puzzle-bballsim.
2. Clone, compile, run and see it crash and burn.
3. Solve the bug by changing ONE single keyword/token/identifier: it could be by replacing it, adding one, or deleting one.
4. Explain what caused the bug and how your solution fixes it.
5. Send us the solution and explanation to firstname.lastname@example.org by
Friday September 12, 12pm (PDT).
Update (12/9): We’d like to thanks everyone who participated and attempted to solve the puzzle, hope you had a good time! You can find the correct solution here – follow us on twitter to get updates on new puzzles.
** Please note that the explanation will be a crucial part of the answer. A wrong or an incomplete explanation may disqualify you even if you fixed the bug.
In case there’s more than one developer who solves the puzzle correctly, we’ll randomly choose one winner. In any case, the right answer will be published on the following week and all those who got the right answer will receive their recognition.
If you have any questions, please post them in the comments section below. Oh, and if you reach an answer, please don’t spoil it for everyone. At least not before the deadline. Good luck! 🙂
Java Developers – Know when and why production code breaks – read more
Already using Java 8? Try Takipi for Java 8