Sunday, August 30, 2009

Puzzle 50 – Before & After

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

Do you remember the first time you wrote some java code and were stuck on a really silly thing for hours? Well this puzzle is right up that alley,

package com.twister; public class Area { int length = 10; int area = length*breath; int breath = 20; public static void main(String[] args) { Area a = new Area(); System.out.println(a.area); } }

Apart for being a really silly piece of code with all values ‘hard coded’ – as you might have already guessed this code fails to compile.
The puzzle - what is the minimum code addition (no deletes, no moving the code – plain addition only) that is needed to make this code compile and run to give the expected output – 200?

Got an answer? Do leave it here.

Puzzle 49 – Solution

With 49 (coincidence?) comments posted for the last puzzle – this was the most solved puzzle till date. There were a range of solutions and I have listed out the common solution below,

Solution 1- Use a Finalizer.

The simplest solution that one could use for this problem is to add a finalizer in the code that restores the looper object.

package com.twister; public class StopTheLoop { static StopTheLoop looper; public static void main(String[] args) { looper = new StopTheLoop(); looper = null; do{ System.out.println("Infinite Loop");; }while(looper==null); } @Override protected void finalize() throws Throwable { looper = this; } }

The hint to use this solution came from the fact the line -- first created a new Object and then the reference was set to null – which meant that when the garbage collector runs you had a chance to restore the object.

Solution 2 – Override the System.out with a custom object that exits, after doing a single print.

package com.twister; import java.io.PrintStream; public class StopTheLoop { static StopTheLoop looper; public StopTheLoop() { PrintStream ps = new PrintStream(System.out){ @Override public void println(String x) { super.println(x); System.exit(0); } }; System.setOut(ps); } public static void main(String[] args) { looper = new StopTheLoop(); looper = null; do{ System.out.println("Infinite Loop");; }while(looper==null); } }

Solution 3 – Creating a thread that exits or restores looper after some time or by checking if the looper reference is null and creating a new object.

package com.twister; public class StopTheLoop extends Thread { static StopTheLoop looper; static { new StopTheLoop().start(); } public static void main(String[] args) { looper = new StopTheLoop(); looper = null; do { System.out.println("Infinite Loop"); } while (looper == null); } @Override public void run() { Thread.yield(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.exit(0); } }

I think this about covers all the solutions for this puzzle.

Sunday, August 23, 2009

Puzzle 49 – Stop me if you can ...

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

After a long week here is the next puzzle,

package com.twister;
public class StopTheLoop {
static StopTheLoop looper;

public static void main(String[] args) {
looper
= new StopTheLoop();
looper
= null;
do{
System.out.println(
"Infinite Loop");;
}
while(looper==null);
}
}

This program keeps printing the string "Infinite Loop" an infinite number of times. Without making any changes to the main method (no additions, deletions, modifications, redefining main) - convert this program so that the loop runs a finite number of times.
Note : the loop must run at least one time.

Got an answer? Do leave it here.

Puzzle 48 - Solution

The code snippet defines an Enum, a feature introduced in Java 1.5.

This code creates a type-safe enumeration with apples, oranges, and grapes as members and for each member a String value is stored (the color of the fruit).

You can read more about enums on http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html

Sunday, August 16, 2009

Puzzle 48 - Fruity Tale

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

I have been keeping pretty busy these days with lots of parallel stuff going on. Unfortunately this means that I will have to reduce some of the stuff I do and twister is one of them. Of course I still going to keep posting puzzles but at a reduced frequency - of one puzzle a week at least for some time!

On the other hand, this should give me some time to get Quiz4J up in shape which would also explore features of Java in a fun filled manner - keep watching this space for more information on that.

Coming back to Today's question, it is not really a puzzle and depending on your comfort level with Java this might just be easy. It does bring out an interesting feature though so give it a shot ...

<modifier> Fruits{
apples(
"red"), oranges("orange"), grapes("green");
String color
= null;
Fruits(String color){
this.color = color;
}
}

What does this code do? Can you complete this code and explain what it does or give an example of how it can be used?

Got a solution? Do leave it here.

Puzzle 47 - Solution

There are different categories of solutions to this puzzle, with the most simplest placed first to the most difficult!

1. DataType1 - Datatype4 all have different values and we rely on autoboxing and numeric promotions to get the job done.

package com.twister;
public class Mystery {

public static boolean isEqual(Integer f1,Long f2){
return f1.equals(f2);
}

public static void main(String[] args) {
int f1 = 1;
long f2 = 1;

System.out.println(
"f1 is equals to f2 : "+ (f1==f2));//prints true
System.out.println("f1 is equals to

f2 :
"+ isEqual(f1,f2));//prints false
}

}


2. DataType1 - Datatype2 are same and Datatype3 - Datatype4 are the same. Relies on autoboxing and the fact that the equals method of Float treats +0 as not equal to -0.

package com.twister;
public class Mystery {

public static boolean isEqual(Float f1, Float f2){
return f1.equals(f2);
}

public static void main(String[] args) {
float f1 = +0.0f;
float f2 = -0.0f;

System.out.println(
"f1 is equals to f2 : "+ (f1==f2));//prints true
System.out.println("f1 is equals to

f2 :
"+ isEqual(f1,f2));//prints false
}

}


3. DataType1 - Datatype4 are all the same.

package com.twister;
public class Mystery {

public static boolean isEqual(Object f1, Object f2){
return f1.equals(f2);
}

public static void main(String[] args) {
Object f1
= new Object()
{
@Override
public boolean equals(Object obj) {
return false;
}
};
Object f2
= f1;

System.out.println(
"f1 is equals to f2 : "+ (f1==f2));//prints true
System.out.println("f1 is equals to

f2 :
"+ isEqual(f1,f2));//prints false
}
}

Treasure Hunt Results

Congratulations to

1. Vishwanath
2. Arshia
3. RaymondTau

on completing all the stages of the treasure hunt.
10 Point have been awarded to all the winners.

You can try out the treasure hunt at http://www.treasureHunt.quiz4j.com

Wednesday, August 12, 2009

Puzzle 47 - IsEqual Unequal?

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

It's been a long break since we had a puzzle here, so without much ado here is the next one.

package com.twister;
public class Mystery {

public static boolean isEqual(<DataType1> f1, <DataType2>f2){
return f1.equals(f2);
}

public static void main(String[] args) {
<DataType3> f1= <value1>
<DataType4> f2 = <value2>
System.out.println(
"f1 is equals to f2 : "+ (f1==f2)); //prints true
System.out.println("f1 is equals to f2 : "+ isEqual(f1,f2)); //prints false
}
}


Everything that you need to solve this is present in the code snippet above. All you need to do is replace everything in brackets namely - DataType1 - DataType4 & Value-1 - Value-2 with appropriate constructs, so that the prints happen as per the comments. No other changes to be done to this code!

You might want to watch out for hints and more restrictions @ Twitter.

Got a solution. Do leave it here.

Treasure Hunt Results to be updated next week.

Sunday, August 9, 2009

Out on Vacation -:)

I am out on a this weekend - so there is no puzzle today.

If you still have not had a look you could solve Java Treasure Hunt which has loads of puzzles packed in it -:)

Points for the treasure hunt to be updated with the next post...

Wednesday, August 5, 2009

Java Treasure Hunt-09 -- Updates

It's been a few days since we had the treasure hunt - and surprisingly not many people have made till the end!

I haven't been clear enough about everything and I thought I thrown in a couple of hints. It's a treasure hunt - treat everything as a potential clue - and of course everything that you need to start of with the hunt after registering is present in the home page. If you have not found where to start the hunt, you have not looked hard enough for The Link ...

I would also like to congratulate Vishwanath on being the first (and till now the only) person to complete the hunt!

The offer is still on - you do score 10 points on twister if you complete the hunt before noon Sunday. I sure would love to see a lot of people scoring those 10 points - though I think the treasure hunt is really not as easy as I thought :).

Feedback on the treasure hunt would be really great - I did get a few mails - and its great to hear from you folks.

In case you face any problems you could mail me at admin@Quiz4J.com.

Sunday, August 2, 2009

~~ Java Treasure Hunt 09 ~~

I finally got myself a new domain Quiz4J - a site dedicated to Java Puzzles and Quizzes!!
It’s still early days and a long way to go - looking to turn in it something new and exciting.

The good news is I finally was able to put together the Java Treasure Hunt that I have been clamoring so much about. Please do check it out - I hope its as much fun as I imagine!!

This week there are no other puzzles (today & even on Wednesday) though there are 10 points on offer for everyone who completes the treasure hunt.
I would love to get some feed back on how you found treasure hunt and any suggestions for improvements would be great - this is just the first and I hope to improve over time!
In case you face any problems you could mail me at admin@Quiz4J.com.

If you do like Treasure Hunt - consider sharing this post or spreading the word around in any manner you deem fit!!! Your support in any manner is always helpful and encouraging.

Going a little off topic, Twisters has also found a place in this new domain @ http://twisters.quiz4j.com. The old URL redirects to this one so all of your bookmarks should work fine.

Cheers & Best of luck
Treasure Hunters.

Puzzle 46 - Solution

The puzzle that I had posted (print just the max) is actually a toned down version of the much harder - print out the 10 numbers in ascending order (hence the title Just a sort). You might want to give that a try at your own leisure (and remember writing more than 25 lines of code is still a strict no-no!!)

Here is a solution that I picked up from TheMalkolm which covers the essence of how this could be done!

private static int max(int x, int y){
return x > y ? x : y;
}

private static printMax(num0, num1..., num9){
return max(num0,max(num1,max(num2,max(num3,max(num4,max(num5,max(num6,max(num7,max(num8,num9)))))))));

@Nash - You took me way too literally. When I said no other classes - I really meant no other classes to be used to sort out the numbers. Sorry for the confusion - I need to be more specific with the words I use. Using native code was a really novel idea!

Saturday, August 1, 2009

Puzzle 45 - Individual Points!

I had promised to post the individual scores Puzzle 45. I had planned to give an explanation as to how I did arrive at the score but that got too complicated - so I am just posting a score with a short reason for the score. See solution where I have listed out most of the unique solutions to this problem.

The points for the puzzle are as below,

Vishwanath - 2 points.

Kevin - 2 points

TheMalkolm - 6 points - Pretty much all the solution had a different principal

Nash - 4 points - A few of the solution were based on generating random numbers that I clubbed under a single category while awarding the points.

George PĆ³lya - 10 points - A few of the solutions were based on changing external condition/parameters that I clubbed under a single category while awarding the points. I found some of your solution really cool - changing the OS to get different solutions was a gem -:).

Sebastian - 8 points

Yauheni - 2 points

You may not agree with all my classification on this puzzle and I do admit that in this puzzle judging the points was really hard :). In case you feel I have missed something leave a comment here ...

The points would be updated with the next post!!