Sunday, September 27, 2009

Out on Vacation!

I am on a vacation this week – so no new puzzle this week.

However for those looking for a Java puzzle to think over this post should be interesting -

Puzzle 53 – Solution

The problem that occurred was that even though the Constants class was recompiled the MyData class still continues to retain the old value of the constant Author. This happens because static final fields are in-lined into the code.

As c0dep0et points out in his comment,
"From JLS:
Simple names that refer to final variables whose initializers are constant expressions qualify to be compile-time constants."

One way to resolve this problem would be to make sure that all files depended on the Constants file are recompiled when the Constants file is recompiled. (Alternately not declaring the field as final would work!).

Modern IDE are more intelligent and usually recompile depended class when a final static field is changed in a class!

Sunday, September 20, 2009

Twister updates.

There have been a few suggestions and questions that have been asked and since a lot of new folks have joined Twisters its time for a few updates.

1. @Anonymous – Appreciate your comment on 'code not showing up right in feed readers.' I think I have got that fixed and things should look right from now on (at least that’s what I hope)!

2. @tonthatduy – There is a score sheet that I update after every puzzle (it's the last link in the Top Score widget).
If your solution is deemed right – you get points for that puzzle.
If your solution is wrong – well it should be marked up as a 0 (or sometime when I am just feeling lazy – I just leave it blank!)

To answer your question in particular, your solution for puzzle 51 was not quite right. It broke one of the rules – "no deletes, no moving about the code,
no commenting out …" Putting two slashes // ended up commenting out some of the code!

3. I have made a small change to the way the sharing icons works (if you have not noticed they are the cute icons that you can see at the bottom of each post). Clicking on the link would no longer bookmark/submit the individual post but rather submits the Twisters site. Considering that a post on usually Twisters expire in a week – sharing an individual post does not make much sense.

4. For those who like solving puzzles there is
JFactor - Java Treasure Hunt that I had designed a few moths ago, that you could try out. Hope you find the Treasure Hunt fun and challenging!!

Finally, I would like to thank all you folks for your support and encouragement in keeping Twisters going!!!

Puzzle 53 – Statically Speaking.

Language – Java | Type – Concept | Last date 27-Sep-2009 12:00 p.m. IST | Points 3

This puzzle is interesting because it's based on a real life incident that happened with a colleague of mine. To give you a gist of what happened, here is the code (well this isn't the real code – just the important part)

package com.twister;
class MyData{
String author
= com.twister.Constants.AUTHOR;
//lot of other stuff comes here…

public static void main(String[] args) {
MyData mydata
= new MyData();

package com.twister;
class Constants{
public static final String AUTHOR = "SAM"; //Me of course


Here is what happened. This was some code that I had written – see my name in the Constants file. Well my colleague picked up my code and just modified the String Author to his, recompiled the Constants file and went on to show a demo to the manager. Ops something went wrong here, which got him into a lot of trouble. Can you figure out what went wrong? (Assume he codes using Notepad). What are the possible solutions to this problem?

Got an answer? Do leave it here.

Puzzle 52 – Solution

The answer to last weeks puzzle is 0. That’s right the minimum code change needed to get the code to print true is 0!!

Surprised – well let’s have a look at what the Java Docs have to say for the getBoolean() method -

"Returns true if and only if the system property named by the argument exists and is equal to the string true …" (read more).

To get the code to print true, we just need to run the program with the right command line arguments, namely,

java -Dfalse=true com.twister.MyTruth

You would find a good discussion on this topic here.

@Sebastian & Mohamed El-Beltagy – Good catch folks!!!

Sunday, September 13, 2009

Puzzle 52 – "Satyameva Jayate" : Truth shall always prevail.

Language – Java | Type – Concept | Last date 20-Sep-2009 12:00 p.m. IST | Points 3

What is the minimum change (additions only – no deleting or commenting out code) that you need to make to the code so that the program prints true?

This should be easy – but it does highlight another peculiarity to look out for.

package com.twister; public class MyTruth { public static void main(String[] args) { boolean b = Boolean.getBoolean("false"); System.out.println(b); //This should print true } }

Got an answer? Do leave it

Puzzle 51 – Solution

This puzzle brought in a variety of solutions and as usual all solutions that meet the conditions of the puzzle would be considered correct.

The solution I had in mind is,

package com.twister; public class Area { //should initialize to 0 - formula mentioned for documentation int area = this.length*this.breath; //Instance variables get initialized to 0 int length = 10; int breath = 20; public static void main(String[] args) { Area a = new Area(); //Do whatever needs to be done in main } }

I find this solution pretty suitable for this problem for a couple of reasons:

1. It does not destroy the essence of the code – the
variable area remains part of the class Area which makes the code look logical. The other solution that does the same is making area a method.

2. It goes with the comment of keeping the code self documenting which getting the code to compile.

Of course some of the other solutions are pretty neat too – It's just that I am a little biased towards this one!!

@Matthieu – I missed giving you points for your solution last time – thats corrected now.

@Simonz – You solution (for puzzle 50) is correct too – It satisfies all the rules of puzzle 50.

@Yauheni – Welcome back - : ) – I wish I get my next vacation soon!

Sunday, September 6, 2009

Puzzle 51 – This and That!

Language – Java | Type – Concept | Last date 13-Sep-2009 12:00 p.m. IST | Points 3

Improving upon last week’s code, I came up with this:

package com.twister; public class Area { //should initialize to 0 - formula mentioned for documentation int area = length*width; //Instance variables get initialized to 0 int length; int width; public static void main(String[] args) { Area a = new Area(); //Do whatever needs to be done in main } }

Now while this is much better than the previous code (no hard coded values, self documenting), I seem to be having a problem with the code. As you might have already guessed this code does not compile!!!

Your challenge is pretty simple – get this code to compile by adding minimum number of characters to this code. Also a few additional conditions that you need to meet are -

a. You may only add code to get this to work – no deletes, no moving about the code, no commenting out any code! Additions only!

b. Obviously as before, declaring length and width as static would do the trick – but then that is so obvious that it can’t be the right solution, can it?
(So I am looking for something that solves the problem in less than 12 characters).

Got an answer? Do leave it here.

Puzzle 50 – Solution

Declaring the variable breadth as a
static variable solves the problem of forward reference in the puzzle. Static code is referenced and initialized before any instance code – and so the forward reference problem of the breadth variable gets solved.

package com.twister;

public class Area {

int length = 10;
int area = length*breadth;
static int breadth = 20;

public static void main(String[] args) {
Area a
= new Area();

A neat trick pointed out by TheMalkolm solves the problem using just 5 characters!