Saturday, November 14, 2009

CTRL + ALT + DEL is out, CTRL + SHIFT + ESC is the new IN thing

Sick of watching the Windows User options screen when pressing the famous CTRL + ALT + DEL combination?

Ever wondered why can’t the old three-finger shuffle directly bring up the task manager in vista or Windows 7 like it did in XP?

Thats because CTRL+ALT+DEL no longer brings up the task manager in vista. But that does not mean that there is no shortcut to directly bring up the task manager.

Since vista is build in line with NT, this has been changed to the NT behavior.

The new keyboard combination to bring up the task manager is:

CTRL+SHIFT+ESC

This even works with Windows XP and Windows 7 apart from Vista. Guess its time to practice this new combination and spread the word.



Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Friday, May 22, 2009

Deccan Chargers pulverize the Delhi Daredevils in IPL 2 Semifinals

Hi guys,

I am back and am sure the Deccan chargers followers are gonna enjoy this post of mine, with Chargers and Royal Challengers both entering into the semi finals I finally started watching the whole match instead of checking the scores in between the overs; and boy am I glad I did.

With the first semi final placed between Deccan Chargers (my fav team) and DareDevils (the team I hate the most), I just wanted the chargers to charge on their batteries and shoo away the Darpok(coward)Devils.



               v/s






The first half of the match wasn't quite as I had imagined it to be (but still the 2 wickets at the end of first over was enough to bring a cheer on my face) and the chargers got a target of 154 to chase.

With Adam Gilchrist and Gibbs on the field, I was looking forward to some action and I wasn't disappointed thanks to Gilchrist who slammed 21 runs in the 1st over with 5 boundaries, and that was just the beginning. He banged every bowler (NO PUN INTENDED :D) and every ball for a boundary scoring a 50 in just 17 balls (and also scoring respect in many people's hearts).


Adam Gilchrist - The juggernaut

I was extremely elated at the sight of consecutive boundaries and specially so to see the expressions at all the Darpok Devils team member's faces, they all had 12 struck on them.

Gilchrist got out at 85, leaving an easy target for the rest of the team members. Symonds jumped in and whacked another 24 runs before getting caught.

Finally the winning single was taken by Rohit Sharma, who certainly seemed different from his explosive type.

Today's match was one which certainly none of the chargers fan should have missed, watching Gilchrist thrashing every bowler for a boundary was a treat, even the darpok devil's captain wasn't spared, not to mention the bowling figures of Nannes :D.

Now I am looking forward to watching the finals, not to mention how happy I am to watch the darpok devils getting kicked out of the finals and just can't wait to put the lids on a certain someone's mouth who THINKS he is the biggest fan of Darpok devils.

Lazy Coder signing off for now.....

PS: Expect another post if Chargers win IPL 2

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Monday, May 11, 2009

hashCode and equals Methods in java classes

Often while working with objects, we want to compare whether the objects we are working with are unique or equal to each other, java has provided us with two methods in java.lang.Object and by overriding them we can compare two objects. These methods are :

1. equals method indicates whether some other object is "equal to" this one.

2. hashCode method returns a hash code value for the object, which is unique for unequal objects.

The equals method and hashCode method for any java class should go hand in hand. Any variable contributing to equals method must also be used for hashCode calculation because as per definition Objects having same hashCode should be equal to each other or conversely no two different objects may have the same hashCode.

hashCode values may also be inserted into the database as passwords and when user enters password in the application, its hashCode can be compared with the value in password field. Thus hashCode can also help with data encryption.

Here is an example on how to implement these methods.

/*

  *   @(#)Employee.java       1.1   11/05/09

  *

  *   Copyright   2009   Lazy   Coder.   All   rights   reserved.

  *   To   be   used   only   for   educational   purposes.

  *   Not   to   be   reproduced   and   published

  *   without   seeking   prior   consent.

  */


package   com.lazy.coder.examples;



/**

  *  

  *   @author   lazycoder

  *

  */




public   class   Employee   {

       private   int   empId;

       private   String   empName;



       public   boolean   equals(Object   obj)   {

              //If   this   object   and   the   Object   in   the   argument   refer   to   same   object   return   true.

              if   (this   ==   obj)

                     return   true;

              //If   obj   is   null   or   of   some   other   class   return   false.

              if   ((obj   ==   null)   ||   (obj.getClass()   !=   this.getClass()))

                     return   false;

              //   object   must   be   of   type   Employee   now

              Employee   emp   =   (Employee)   obj;

              //If   empId   =   obj.emoId   and   empName.equals(obj.empName)   return   true   else   false.

              return   empId   ==   emp.empId

                            &&   (empName   ==   emp.empName   ||   (empName   !=   null   &&   empName

                                          .equals(emp.empName)));

       }



       public   int   hashCode()   {

              int   result   =   17;

              result   =   37   *   result   +   empId;

              result   =   37   *   result   +   (empName   ==   null   ?   0   :   empName.hashCode());

              return   result;

       }

}



Here are a few tips on how to override these methods:

I. hashCode

  1. Store some arbitary constant nonzero integer value, say 17, in an int variable called result.
  2. For each significant field f in your object (each field taken into account by the equals( ) method), calculate an int hash code c for the field in the following manner:

Field type

Calculation

boolean

c = (f ? 0 : 1)

byte, char, short, or int

c = (int)f

long

c = (int)(f ^ (f >>>32))

float

c = Float.floatToIntBits(f);

double

long l = Double.doubleToLongBits(f);
c = (int)(l ^ (l >>> 32))

Object, where equals( ) calls equals( ) for this field

c = f.hashCode( )

Array

Apply above rules to each element

  1. Combine the hash code(s) computed above:
    result = 37 * result + c;
    where 37 can be replaced with any primary number
  2. Return result.
  3. Look at the resulting hashCode( ) and make sure that equal instances have equal hash codes.
II. equals
  1. Use the equality == operator to check if the argument is the reference to this object, if yes. return true. This saves time when actual comparison is costly.
  2. Check that the argument is not null and it is of the correct type, if not then return false.

    Note that, correct type does not mean the same type or class. It could be any class or interface that one or more classes agree to implement for providing the comparison.
  3. Compare significant variables of both, the argument object and this object and check if they are equal. If *all* of them are equal then return true, otherwise return false. Again, as mentioned earlier, while comparing these class variables; primitive variables can be compared directly with an equality operator (==) after performing any necessary conversions (Such as float to Float.floatToIntBits or double to Double.doubleToLongBits). Whereas, object references can be compared by invoking their equals method recursively. You also need to ensure that invoking equals method on these object references does not result in a NullPointerException.
  4. Do not change the type of the argument of the equals method. It takes a java.lang.Object as an argument, do not use your own class instead. If you do that, you will not be overriding the equals method, but you will be overloading it instead; which would cause problems.


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Sunday, May 3, 2009

Java : Adding multiple values to a key in a HashMap / Hashtable

Hi guys,

In today's example I will show you how to add multiple values to a key in a HashMap or Hashtable.

The key is to add the values in an ArrayList everytime.

Check out the next example for more :

/*

  *   @(#)HashMapMultipleValues.java       1.1   03/05/09

  *

  *   Copyright   2009   Lazy   Coder.   All   rights   reserved.

  *   To   be   used   only   for   educational   purposes.

  *   Not   to   be   reproduced   and   published

  *   without   seeking   prior   consent.

  */


package   com.lazy.coder.examples;



import   java.util.ArrayList;

import   java.util.HashMap;



/**

  *   @author   lazycoder

  *

  */


public   class   HashMapMultipleValues   {

      

       public   static   void   addMultipleValues(HashMap   map,  

                     Object   key,   Object   value){

              Object   prevValue   =   map.get(key);

              ArrayList   valueList;

              if(   prevValue   ==   null   ){

                       valueList   =   new   ArrayList();

                       map.put(key,   valueList);

              }else{

                     valueList   =   (ArrayList)prevValue;

              }

                valueList.add(value);

       }



       /**

         *   @param   args

         */


       public   static   void   main(String[]   args)   {

              HashMap   map   =   new   HashMap();

              String   key   =   "LazyCoder";

              addMultipleValues(map,   key,   "LazyCoder");

              addMultipleValues(map,   key,   "is");

              addMultipleValues(map,   key,   "Lazy");

             

              ArrayList   valuesList   =   (ArrayList)map.get(key);

              for(Object   value   :   valuesList){

                     System.out.println(value);

              }

       }



}


If done normally like this :
  map.put(key,   "LazyCoder");

  map.put(key,   "is");

  map.put(key,   "Lazy");

the old value gets overriden with the latest value put against the key in the HashMap
in this case "Lazy" will be saved against the key in map.

LazyCoder
Signing off for now...

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Friday, May 1, 2009

Java : Converting java.util.Date to java.sql.Date

Many a times we need to insert the date of transaction in the database, but the problem is that the PreparedStatement Implementations have a setDate() method which takes java.sql.Date as an argument and same is the case with the ResultSet which is returned from the database as java.sql.Date is an equivalent to Date datatype in the DB.

But in java we can get current date from a new Object of java.util.Date, so here is how to convert it into java.sql.Date

java.util.Date today = new java.util.Date();

java.sql.Date date = new java.sql.Date(today .getTime());

java.sql.Date only saves the date though and not other attributes like day, time, timezone etc.

If the field in the Database is defined as DATETIME then we can use java.sql.Timestamp instead.

java.sql.Timestamp ts = new java.sql.Timestamp(today .getTime());

here is the result

System.out.println(today); ---> Fri May 01 17:54:26 IST 2009 (util.Date)
System.out.println(date); ---> 2009-05-01 (sql.Date)
System.out.println(ts); ---> 2009-05-01 17:54:26.199 (sql.Timestamp)

Timestamp doesn't hold timezone and day attributes.

Thats it for now folks.

LazyCoder
Signing off for now...



Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Bachachans showing middle finger after casting their vote

May Day being a holiday I lazily woke up at 9am, opened the door of my home to pick up the newspaper and there it was right on the front page, the Bachchans posing with their middle finger after casting their votes.

Bachchans


I was taken aback but still couldn't help giggling and was quite curious as to why the ink was applied to their middle finders instead of the index finger, right besides their photo, was a picture of Aamir Khan with his middle finger also inked.

I just got curious and performed a quick search only to find this article on TOI's website

Voters will now show middle finger

That sounds absurd and watching the celebrities posing in such a fashion is tasteless indeed.

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Tuesday, April 28, 2009

Java Example : How To Swap Integer and String Variables Without Using a Temporary Variable

Hi guys,

Swapping two variable has always been a classic learning example for beginners, well for a change in the example below we would be learning to swap two variables (String or Integer) without using a temporary variable.

/*

  *   @(#)Swap.java       1.1   28/04/09

  *

  *   Copyright   2009   Lazy   Coder.   All   rights   reserved.

  *   To   be   used   only   for   educational   purposes.

  *   Not   to   be   reproduced   and   published

  *   without   seeking   prior   consent.

  */


package   com.lazy.coder.examples;





/**

  *   A   class   that   demonstrates   how   to   swap  

  *   two   strings   or   two   integers   without

  *   using   a   temporary   variable

  *  

  *   @author   lazycoder

  *  

  */


public   class   Swap   {



      

       /**

         *   @param   args

         */


       public   static   void   main(String[]   args)   {

              //Swapping   two   String   without   using   temp   variable.

              String   firstString   =   "Lazy";

              String   secondString   =   "LazyCoder";

              System.out.println("firstString   =   "   +   firstString);

              System.out.println("secondString   =   "   +   secondString);

              System.out.println("Swapping");

              firstString   =   firstString   +   secondString;

              secondString   =   firstString.substring(0,   firstString.length()

                            -   secondString.length());

              firstString   =   firstString.substring(secondString.length());

              System.out.println("firstString   =   "   +   firstString);

              System.out.println("secondString   =   "   +   secondString);

             

              //Swapping   two   Integers   without   using   temp   variable.

              int   firstInt   =   111111111;

              int   secondInt   =   999999999;

              System.out.println("firstInt   =   "   +   firstInt);

              System.out.println("secondInt   =   "   +   secondInt);

              System.out.println("Swapping");

              /*

                *   Using   exclusive-or   (XOR)   to   avoid  

                *   stack   overflow/underflow

                */


              firstInt   =   firstInt   ^   secondInt;

              secondInt   =   firstInt   ^   secondInt;

              firstInt   =   firstInt   ^   secondInt;

              System.out.println("firstInt   =   "   +   firstInt);

              System.out.println("secondInt   =   "   +   secondInt);

       }

}



Thats all for now, check back for more java examples.

Lazy Coder
signing off for now...

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Monday, April 27, 2009

Adding two Object arrays or Collections in Java

Hi guys,
I was feeling a little bit bored so thought of posting a java example on my blog. Starting with a basic one for now, many of the java newbies have faced problems while trying to combine two Object arrays or collections in java. We will learn just how to do that in this post.

PS: I have used the extended for loop in the example below and so it requires JDK 5 or above to run.

The code below will not hold duplicates in the resultant; if you want to hold duplicates as well then define alist as ArrayList instead of a HashSet.

/*

  *   @(#)CombiningCollections.java       1.1   27/04/09

  *

  *   Copyright   2009   Lazy   Coder.   All   rights   reserved.

  *   To   be   used   only   for   educational   purposes.

  *   Not   to   be   reproduced   and   published

  *   without   seeking   prior   consent.

  */


package   com.lazy.coder.examples;



import   java.util.Arrays;

import   java.util.HashSet;



/**

  *   @author   lazycoder

  *  

  */


public   class   CombiningCollections   {



       /**

         *   A   method   which   takes   two   collections

         *   as   parameters   and   returns   a   resultant

         *  

         *   @param   collection1

         *   @param   collection2

         *   @return

         */


       public   Object[]   combineCollections(Object[]   collection1,

                     Object[]   collection2)   {

              HashSet   alist   =   new   HashSet();

              for   (Object   obj   :   collection1)

                     alist.add(obj);

              for   (Object   obj   :   collection2)

                     alist.add(obj);

              Object[]   collection   =   alist.toArray();

              /*

                *   Used   to   sort   the   elements   in   ascending   order

                *   all   elements   in   the   array   must   be   mutually

                *   comparable(that   is,   e1.compareTo(e2)   must   not  

                *   throw   aClassCastException   for   any   elements  

                *   e1   and   e2   in   the   array).

                */


              Arrays.sort(collection);

              return   collection;

       }



       /**

         *   @param   args

         */


       public   static   void   main(String[]   args)   {

              Object[]   collection1   =   {   "A",   "C",   "1",   "D"   };

              Object[]   collection2   =   {   "Bits",   "Nibble",   "Bank"   };

              CombiningCollections   combine   =  

                     new   CombiningCollections();

              Object[]   collection   =  

                     combine.combineCollections(collection1,

                            collection2);

              for   (Object   obj   :   collection)

                     System.out.println(obj);

       }

}



Hope you guys will find this helpful.

Lazy Coder
Signing off for now....

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Sunday, April 26, 2009

Roadies Hell Down Under Concludes

I think there are many more like me who would glue themselves to their couches and bean bags with their Tv sets turned on during weekends just to watch youngsters abuse each other and fight besides performing tasks.

Yes, I am talking about fans of reality shows Splitsvilla 2 and Roadies Hell Down under 6.0, Raghu and Rannvijay became celebrities with these shows becoming an instant hit.

To cut to the chase, today the final episode of Roadie hell down under was telecasted in India, and must say I was very much disappointed after watching it. With Sufi, already losing the wildcard entry to the finals I wasn't much interested but then again carried on just to watch cute babes like Tammana, Roop and Bobby once again.

Kiri Timung
Nauman SaitPalak

Kiri, Nauman and Palak were the three roadies who ended up in the final episode, I wasn't taking any sides and din't care much as to who would win. Kiri won the first task which was some form of rock climbing and had the power to pick a roadie besides him for the final showdown.

But the devils Raghu and Rajiv are they confused him and Kiri made a dumb choice by picking up Nauman for the final task and ultimately losing to him in the last task.

Guess thats a decision he will keep on regretting upon; and finally we ended up by having a Roadie like Nauman who doesn't even have a back bone who couldnt perform the tasks with elan.

Well I guess its luck after all that matters with skill and hard work... As for Palak I am sure she must be crying somewhere.

All in all I didn't find this season better than the previous ones and IMHO the tasks all sucked mostly comprising of sports like rugby, ice hockey etc.

Lazy coder signing off for now....

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl

Friday, April 24, 2009

Yawn !!! And so it begins

Hi guys,

The lazy guy that I am, I never opened a blog earlier, just the thought of creating a blog and updating it regularly seemed like a drag to me. Pressing numerous keys to get my message to the internet users, huh well thats something for sure.

So finally here I am writing out my very first post on my blog, hope I get atleast a warm response, hope to keep you people updated weekly on what goes on in my life, how I pass my days coding boring classes etc etc.

Lazy Coder
Signing off for now .....


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl