Java Puzzler - on Code-o-matic

Sunday, December 13, 2009
I don't have a puzzle of my own today - I've covered up most things I know & I really don't want to end up repeating myself! So for now I am going to point you to a puzzle I had come across some time ago - http://code-o-matic.blogspot.com/2009/02/crazy-java-puzzler.html

I really don't know the answer to this puzzle - but then again I've never been good with Generics or hardcore Java - you folks might know an answer. Give it a shot - and do let us know too if you got an answer!

Puzzle 59 – Comparing the Java way.

Sunday, November 22, 2009
A really cool part of the collection framework is the Collections.sort() function. The function sorts out a collection based on the natural ordering of elements. Of course – for you own data types you need to define what the ‘natural’ ordering is. Doing that is really simple though – just implement the compareTo() method of the Comparable interface and we are ready to go.

That brings me to today’s questions. The code below for the Points class implements the compareTo() method. Now my manager kept insisting that there is something not right about it – but the code looks alright to me.

What do you folks think – are there any problems with the code below?

package com.twister;

import java.util.ArrayList;
import java.util.Collections;

public class Points implements Comparable<Points>{
int xCoordinate;
/*
* 1. Returns 0 if both points have same xCoordinate (say 3 & 3) - returns 3-3=0
* 2. Returns +ve if first point is on the
* right hand side of the second point (say 5, -3) - returns 5 - (-3) = 8
* 3. Returns -ve if first point is on the
* left hand side of the second point (say -5, 3) - returns (-5) - (3) = -8
*/
public int compareTo(Points p) {
return xCoordinate - p.xCoordinate;
};

public static void main(String[] args) {
ArrayList
<Points> arrPoints = new ArrayList<Points>();
/* Add lots of points to the array list */
Collections.sort(arrPoints);
/*Print the sorted collection */
}
}

Got a view? Leave one here.

So where is the puzzle this week?

Wednesday, November 18, 2009
I have been a irregular with puzzles on Twister for some time now – for one I have covered most of the puzzles that could be covered in the twister format and secondly I been working on Quiz4j – adding puzzles and quizzes to it.

When I started
Quiz4j my vision was to create site that would cater to Java Programming Puzzles and Quizzes. I found programming puzzles a really good way to learn and keep in touch with some challenging programming. The more I got interested the more stuff I found around – and I released that there were loads of good programming quizzes and puzzle resources out there – and really having one more site which did the same thing was not going to help much!
Considering that there is a limitation to what one person could do – my plan is to gradually evolve Quiz4J into a community of people like us who enjoy programming puzzles. It’s not going to be something that happens overnight but something that I look forward to happening in the next 3-4 months. You’ll see some quick updates in the next few weeks on Quiz4J – getting rid of some stuff and addition of a lot more.

So what about twisters? Are we not going to have any more of these puzzles? Yes – sure I am going to continue posting puzzles on Twisters. I planning to cut a few overheads – score cards, answer post are few of the things you would see going off. The comment system would be used more as a discussion tool than just posting answers. Puzzles might get a bit more difficult – and you might see me pointing a to existing discussion that writing my own puzzles.

Well so what do you folks think? I really really interested in hearing from you!!!

Puzzle 58 – Solution

Monday, November 16, 2009
There are a couple of solutions possible for this puzzle – I’ll leave figuring out how these solutions work to you!

The first one,

package com.twister;

import java.util.ArrayList;
import java.util.List;

public class Gener {
public void read(List<?> x){}

public static void main(String[] args) {
new Gener().read(new ArrayList<Float>());
}
}


and the second,

package com.twister;

import java.util.ArrayList;
import java.util.List;

public class Gener {
public <Integer>void read(List<Integer> x){}

public static void main(String[] args) {
new Gener().read(new ArrayList<Float>());
}
}