Monday, March 24, 2008

A lil homework

As part of my lil homework yesterday, I had this 'kahani'.

'A toy is controlled remotely over some grid like terrain. Along with its positional coordinates, the toy also maintains its facing (North, South, East, West) so that when remotecontrol commands 'move' - it moves in its facing direction. On turnLeft command, if facing North it would face West. On turnRight command, facing East then would be South. And so on .. '

Well smarty pants, I, declared an enum type.
public enum Facing {North, South, East, West;}


Writing a function to turn things right seemed simple enough!

public void turnRight() {
switch (this.currentFacing) {
case East : this.currentFacing = Facing.South; break;
case West : this.currentFacing = Facing.North; break;
case North: this.currentFacing = Facing.East; break;
case South: this.currentFacing = Facing.West; break;
default: break;
}
}


the similar way to implement the turnLeft() method!!! Now, this didn't seem to tough, eh? Ehrrrrrr! too many conditional statements? If say a new facing (NorthEast) is added, then for things to work, existing code would have to be broken and modified?

Now, Java 5 came with linguistic support for enumerated types enum. All enums implicitly extend java.lang.Enum and allow you to add typical class constructs. 'enum' is also Comparable and Serializable. Now, I could write some complicated program to solve the conditional expressions above. But hey all java enum return an array of declared enums through enum.values(). Voila!

So I wrote:

public enum Facing {North, East, South, West;} //change in order!

public Facing getFace(Facing refFace, int factor) {
int currentIdx = 0;
Facing[] faces = Facing.values();
for (int i=0; i < faces.length; i++) {
Facing face = faces[i];
if (face.equals(refFace)) {
currentIdx = i;
break;
}
}
currentIdx = currentIdx + factor;//the next facing index
if (currentIdx < 0) {
while (currentIdx < 0)
currentIdx = currentIdx + faces.length;
} else if (currentIdx >= faces.length) {
currentIdx = currentIdx - faces.length;
}
return faces[currentIdx];
}


if I consider turning left/anticlockwise as negative ..

getFace(Facing.North, -1); //=>Facing.West
getFace(Facing.North, +5); //=>Facing.East


So in the end, my turnRight() and turnLeft() turned to

public void turnRight() {
this.currentFacing = getFace(1, this.currentFacing);
}
public void turnLeft() {
this.currentFacing = getFace(-1, this.currentFacing);
}

Even if I go and change the Facing declaration to something like
public enum Facing {North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest;}
things would still work as expected if you want to turn left from say North Facing 1 step to NorthWest, 2 steps to West or even turn 720 deg! :)

Think about implementing the same for Days of week, or Months and do similar mathematics on them.
 
day = add(Day.Monday,4);
day = add(Day.Monday,-3);
month = add(Month.January,-3);


You can further generalize things further using
day.getClass().getEnumConstants()

enum is Tiger introduced. Prior to that java, standard way was like declaring a public static final constant usually in an interface/abstract class! For example, how do you add days, month or years to a calendar object?
calendarInstance.add(Calendar.DATE, 2);

Notice the similarity of definition but the differentiation? Declared in Calendar class
public static final int DATE = 5; 

if only java had operator overloading instead of the exotic darker beans and means for the riches! :((

Sunday, March 23, 2008

VS2005 installation on Vista!

finally managed to install Visual studio 2005 on Vista Home Premium. Slow-ish internet bandwidth didn't help. After installing VS, I had to apply Team suite SP1. and then run VS2005 Sp1 for vista, which is kinda like service pack for service pack. These aren't just the patches, I still have to run and fix for various other patches toolsets and extensions, but I will take them on need basis. If you have UAC enabled, it can be frustratingly slow and irritating

There are plenty of articles thankfully on MSDN and on internet, which does provide detailed information.

http://mvolo.com/blogs/serverside/archive/2006/12/28/Fix-problems-with-Visual-Studio-F5-debugging-of-ASP.NET-applications-on-IIS7-Vista.aspx (ASP.NET problems)
http://support.microsoft.com/kb/937523
http://support.microsoft.com/?kbid=929470

oh, VS.NET 2002 and 2003 aren't supported on Vista, but VB6.0 supported??? I am also facing problem with windows explorer. Crashes and restarts once in a while. I am guessing that its a incompatible shell extension. No time to debug and fix .. My vista experience has been bumpy alright! (Humpty dumpty sat on a wall, humty dumpty had a great fall?). I like to adopt softwares early and its fun sometimes finding ways around blocks, but it can be maddenning when pressed for time.

Gonna watch F1 (Sepang) and then got some 'homework' to do ..

hasta la vista!

Friday, March 21, 2008

YASST (Yet another SQL scripting tool)

I was trying to revive my almost broken down PC, when I unearthed this program, SQLScripter.
What does it do? It reads excel spreadsheets, and generates SQL scripts for each row. Simple rules guide the format of XLS file:
1. Spreadsheet name decides the TABLE name.
2. Within each spreadsheet, first row denotes the COLUMNS. By default first column is taken as primary key, unless you mention
3. The Last column denotes action to be performed. (INSERT, UPDATE or DELETE)
How it works?
1. Pick up the excel file. The UI helps you choose the spreadsheets you want to script. Clicking a button generates the SQL scripts and displays in a text area (optionally multi panes for each TABLE).
2. Edit, save as SQL file(s).

Deploy: Single exe and a properties file on Windows 2000 and above (checked on vista .. works!) . Oh, you should have excel or equivalent extentions (eg Microsoft works)

I wrote it initially in Delphi 5. (I recompiled it today on D7, no change required). It uses OLE Automation to connect to MS Excel and read contents. I wrote it for Netkraft, who wanted a pilot for certain client. I went to Netkraft’s office one afternoon, took me about 5/6 hours or so, for the first version. (Never really got paid for that, but got something else going .. different story!)

Later on, I added few other features. As simple it maybe, 4 organizations still use it extensively – two of them in production and support!! So I thought maybe, just maybe, this old outdated thing can be of some use to somebody! :))
Now, where do I upload the source codes?