Thursday, May 14, 2009

Puzzle 23 - Solution

There are two set of solutions to the last puzzle. One of them wasn't exactly what I had in mind.

Here the first one,
package com.twisters;
interface TwistedInterface{void getA();}

class TwistedBase implements TwistedInterface {
public void getA() {}
}

public class TwistedClass extends TwistedBase implements TwistedInterface {
public static void main(String[] args) {
System.out.println(
"In Main");
}
}

Pretty simple explanation – the TwistedClass extends the TwistedBase that implements the getA() method. Since the getA() method is implemented in the base class it need not be implemented in the child class.

The second one,
package com.twisters;
interface TwistedInterface{void getA();}

public abstract class TwistedClass implements TwistedInterface {
public static void main(String[] args) {
System.out.println(
"In Main");
}
}

This solution is even more simpler – Just make the TwistedClass abstract. You no longer need to implement the getA() method in TwistedClass!

The class Twisted had a main method - a probable hint that the class should run.
However as the question was worded - get the class to 'Compile' and said nothing about 'Running' both solution are correct! Maybe I should be more careful with my wordings - but this just brings out the essence of what twisters was supposed to be - many ways to solve a puzzles.

All I can add is this is going to get more fun going forward so keep watching this space for more...

Congrats to
1. Stefan
2. Behive
3. George PĆ³lya
4. Shyam
5. Joe Smith
For getting the right solution.

Leaders board will be updated with next post.

No comments:

Post a Comment

Solution for this question?