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
here.

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();
System.out.println(a.area);
}
}

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