Sunday, November 22, 2009

Puzzle 59 – Comparing the Java way.

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.

Wednesday, November 18, 2009

So where is the puzzle this week?

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!!!

Monday, November 16, 2009

Puzzle 58 – Solution

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>());
}
}

Sunday, November 8, 2009

Puzzle 58 – Simple Upgrade.

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

Here is the puzzle straight and simple. A piece of code was written which had a read() method with the signature below. Thing change and instead of using integers – it was now required to use Floats instead of Integers.

To cut a long story short what is the minimum change considering additions/deletions (each char added or deleted count as 1) to get the code below to compile. (I think it can be done in less than 10 characters)

package com.twister;

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

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

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


Got an answer? Leave one here

Puzzle 57 – Solution

Here is the first solution that works for last weeks puzzle,

class X{
public static void main(String[] a){
System.out.print(a[
0]);
}
}

Run the program as given below
java X
"Hello World"


The second one – (the one that I had in mind) is

class X {
static {
System.out.print(
"Hello World");
System.exit(
0);
}
}

Sunday, November 1, 2009

Puzzle 57 – Hello World - Again

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

It's been two weeks since I wrote a puzzle out here – so I end up typing the simplest program I could think of in Java.

class X{
public static void main(String[] a){
System.out.print(
"Hello World");
}
}

That's the smallest program that I could in java that prints hello world (72 characters excluding all the white spaces). Hope you folks noticed the clever use of variable names and print instead of println.

Well here is the really simple challenge. Write some code that does exactly what the above code does – just use less number of characters. Remember the code has got to compile cleanly and run cleanly and produce the same output as the snippet above. Easy!

Looking for more Java Puzzles? Check out these sites.

Got an answer? Leave one here.