Month: September 2010

Tu Jahan Jahan Chalega Mera Saya Saath Hoga

Posted on Updated on

very loving song.

 

free counters

Im blessed …

Posted on

its so blessing having someone in life who cares you, guids you for what best for you, advice you sincerly, take your prblem as own’s and discus possible solutions. Im proud im blessed, advised, guided. Time has come to see new horizons.

You failed the test!

Posted on Updated on

my god… i did never see such comedy … now i’m not sure whether i passed the test or not 😀

 

and then latter on …

 

 

 

free counters

Java Sorting: Comparator vs Comparable

Posted on Updated on

Hello All …
hope everyone is in good health and enjoying the Allah Almighty’s blessings.
I was working on a task where i need to sort a collection (List) of objects on the basis of some attribute within single element of that list. Though i have used comparators many times in applications but today first time i wrote my first comparator and sort a list using Collection.sort(listName, comparatorClass). Sharing this knowledge with you people too.

What are Java Comparators and Comparables? As both names suggest (and you may have guessed), these are used for comparing objects in Java. Using these concepts; Java objects can be sorted according to a predefined order.

Two of these concepts can be explained as follows.

Comparable

A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator

A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.

Do we need to compare objects? The simplest answer is yes. When there is a list of objects, ordering these objects into different orders becomes a must in some situations. For example; think of displaying a list of employee objects in a web page. Generally employees may be displayed by sorting them using the employee id. Also there will be requirements to sort them according to the name or age as well. In these situations both these (above defined) concepts will become handy.

How to use these?

There are two interfaces in Java to support these concepts, and each of these has one method to be implemented by user.
Those are;

java.lang.Comparable: int compareTo(Object o1)
This method compares this object with o1 object. Returned int value has the following meanings.

  1. positive – this object is greater than o1
  2. zero – this object equals to o1
  3. negative – this object is less than o1

java.util.Comparator: int compare(Object o1, Objecto2)
This method compares o1 and o2 objects. Returned int value has the following meanings.

  1. positive – o1 is greater than o2
  2. zero – o1 equals to o2
  3. negative – o1 is less than o1

java.util.Collections.sort(List) and java.util.Arrays.sort(Object[]) methods can be used to sort using natural ordering of objects.
java.util.Collections.sort(List, Comparator) and java.util.Arrays.sort(Object[], Comparator) methods can be used if a Comparator is available for comparison.

The above explained Employee example is a good candidate for explaining these two concepts. First we’ll write a simple Java bean to represent the Employee.

public class Employee {
    private int empId;
    private String name;
    private int age;

    public Employee(int empId, String name, int age) {
        // set values on attributes
    }
    // getters & setters
}

Next we’ll create a list of Employees for using in different sorting requirements. Employees are added to a List without any specific order in the following class.

import java.util.*;

public class Util {

    public static List<Employee> getEmployees() {

        List<Employee> col = new ArrayList<Employee>();

        col.add(new Employee(5, "Frank", 28));
        col.add(new Employee(1, "Jorge", 19));
        col.add(new Employee(6, "Bill", 34));
        col.add(new Employee(3, "Michel", 10));
        col.add(new Employee(7, "Simpson", 8));
        col.add(new Employee(4, "Clerk",16 ));
        col.add(new Employee(8, "Lee", 40));
        col.add(new Employee(2, "Mark", 30));

        return col;
    }
}

Sorting in natural ordering

Employee’s natural ordering would be done according to the employee id. For that, above Employee class must be altered to add the comparing ability as follows.

public class Employee implements Comparable<Employee> {
    private int empId;
    private String name;
    private int age;

    /**
     * Compare a given Employee with this object.
     * If employee id of this object is
     * greater than the received object,
     * then this object is greater than the other.
     */
    public int compareTo(Employee o) {
        return this.empId - o.empId ;
    }
    ….
}

The new compareTo() method does the trick of implementing the natural ordering of the instances. So if a collection of Employee objects is sorted using Collections.sort(List) method; sorting happens according to the ordering done inside this method.

We’ll write a class to test this natural ordering mechanism. Following class use the Collections.sort(List) method to sort the given list in natural order.

import java.util.*;

public class TestEmployeeSort {

    public static void main(String[] args) {
        List coll = Util.getEmployees();
        Collections.sort(coll); // sort method
        printList(coll);
    }

    private static void printList(List<Employee> list) {
        System.out.println("EmpId\tName\tAge");
        for (Employee e: list) {
            System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());
        }
    }
}
EmpId Name  Age
1    Jorge   19
2    Mark    30
3    Michel  10
4    Clerk   16
5    Frank   28
6    Bill    34
7    Simp     8
8    Lee     40

Sorting by other fields

If we need to sort using other fields of the employee, we’ll have to change the Employee class’s compareTo() method to use those fields. But then we’ll loose this empId based sorting mechanism. This is not a good alternative if we need to sort using different fields at different occasions. But no need to worry; Comparator is there to save us.

By writing a class that implements the java.util.Comparator interface, you can sort Employees using any field as you wish even without touching the Employee class itself; Employee class does not need to implement java.lang.Comparable or java.util.Comparator interface.
Sorting by name field
Following EmpSortByName class is used to sort Employee instances according to the name field. In this class, inside the compare() method sorting mechanism is implemented. In compare() method we get two Employee instances and we have to return which object is greater.

public class EmpSortByName implements Comparator<Employee>{

    public int compare(Employee o1, Employee o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

Watch out: Here, String class’s compareTo() method is used in comparing the name fields (which are Strings).

Now to test this sorting mechanism, you must use the Collections.sort(List, Comparator) method instead of Collections.sort(List) method. Now change the TestEmployeeSort class as follows. See how the EmpSortByName comparator is used inside sort method.Watch out: Here, String class’s compareTo() method is used in comparing the name fields (which are Strings).

Now to test this sorting mechanism, you must use the Collections.sort(List, Comparator) method instead of Collections.sort(List) method. Now change the TestEmployeeSort class as follows. See how the EmpSortByName comparator is used inside sort method.

import java.util.*;

public class TestEmployeeSort {

    public static void main(String[] args) {

        List coll = Util.getEmployees();
        //Collections.sort(coll);
        //use Comparator implementation
        Collections.sort(coll, new EmpSortByName());
        printList(coll);
    }

    private static void printList(List<Employee> list) {
        System.out.println("EmpId\tName\tAge");
        for (Employee e: list) {
            System.out.println(e.getEmpId() + "\t" + e.getName() + "\t" + e.getAge());
        }
    }
}

Now the result would be as follows. Check whether the employees are sorted correctly by the name String field. You’ll see that these are sorted alphabetically.

EmpId Name Age

6 Bill 34
4 Clerk 16
5 Frank 28
1 Jorge 19
8 Lee 40
2 Mark 30
3 Michel 10
7 Simp 8

Sorting by empId field
Even the ordering by empId (previously done using Comparable) can be implemented using Comparator; following class
does that.

public class EmpSortByEmpId implements Comparator<Employee>{

    public int compare(Employee o1, Employee o2) {
        return o1.getEmpId().compareTo(o2.getEmpId());
    }
}

Explore further

Do not stop here. Work on the followings by yourselves and sharpen knowledge on these concepts.

  1. Sort employees using name, age, empId in this order (ie: when names are equal, try age and then next empId)
  2. Explore how & why equals() method and compare()/compareTo() methods must be consistence.

If you have any issues on these concepts; please add those in the comments section and we’ll get back to you.

free counters

Amazon Kindle …

Posted on Updated on

The Kindle is an electronic device for downloading, storing, and reading electronic books, known as e-books. It was developed by a company called Lab 126 which is a subsidiary of Amazon. The Kindle has instant wireless access to Amazon.com and their huge number of e-books (over 115,000 and growing). You can read newspapers, subscribe to magazines and blogs, and even play audio files such as MP3’s or audio books. The Kindle is compact and lightweight so it is easy to handle. Just think of it as a conveniently portable television for your books.

The Kindle uses a grey screen to display a page of text. To turn the page, you just press a button to go forward or backward. It can download compatible files over Amazon’s Whispernet directly to the Kindle by using the same network as Sprint EVDO. You don’t even need a computer to download and then transfer files like you do some MP3 players and other electronics. It also includes a web browser but you are limited to only Amazon or Wikipedia. You can add and read e-books from other sites, too, as long as they are compatible. So you can download stuff fromMobipocket or even in plain text. Amazon offers an email service that can convert files from HTML, jpeg, bmp, PDF, and others into the Kindle format. There is enough internal memory in the Kindle to be able to store around 200 e-books but you can get an SD memory card if you want to add more memory.

Although the wireless service is free, you have to buy the e-books in order to download them. For instance, a book on the latest New York Times bestsellers list can be purchased for $10. Other titles will cost you around $1.99, which is significantly cheaper than a printed book, even in paperback. Books that are public domain can be downloaded for free.

The Newest Kindle DX has

  • All New, High Contrast E-Ink Screen: graphite Kindle DX uses our all new, improved electronic ink display, with 50% better contrast for the clearest text and sharpest images
  • Beautiful Large Display: The 9.7″ diagonal E-ink screen is ideal for a broad range of reading material, including graphic-rich books, PDFs, newspapers, magazines, and blogs
  • Read in Sunlight with No Glare: Unlike backlit computer or LCD screens, Kindle DX’s display looks and reads like real paper, with no glare. Read as easily in bright sunlight as in your living room
  • Slim: Just over 1/3 of an inch, as thin as most magazines
  • Books In Under 60 Seconds: Get books delivered wirelessly in less than 60 seconds; no PC required
  • Free 3G Wireless: No monthly payments, no annual contracts. Download books anywhere, anytime
  • Long Battery Life: Read for up to 1 week on a single charge with wireless on. Turn wireless off and read for two to three weeks.
  • Carry Your Library: Holds up to 3,500 books, periodicals, and documents
  • Buy Once, Read Everywhere: Kindle books can be read on all your devices. Our Whispersync technology saves and synchronizes your Kindle library and last page read across your Kindle(s), PC, iPhone, Mac, iPad, Android device, and BlackBerry device
  • Share Meaningful Passages: Share your passion for reading with friends and family by posting meaningful passages to Twitter and Facebook directly from your Kindle
  • Global Coverage: Enjoy wireless coverage at home or abroad in over 100 countries. See details. Check wireless coverage map.
  • Built-In PDF Reader: Carry and read all of your personal and professional documents on the go. Now with Zoom capability to easily view small print and detailed tables or graphics
  • Auto-Rotating Screen: Display auto-rotates from portrait to landscape as you turn the device so you can view full-width maps, graphs, tables, and Web pages
  • Read-to-Me: With the text-to-speech feature, Kindle DX can read newspapers, magazines, blogs, and books out loud to you, unless the book’s rights holder made the feature unavailable
  • Large Selection: Over 670,000 books and the largest selection of the most popular books people want to read, including 109 of 112 New York Times® Best Sellers, plus U.S. and international newspapers, magazines, and blogs. For non-U.S. customers, content availability and pricing will vary.
  • Out-of-Copyright, Pre-1923 Books: Over 1.8 million free, out-of-copyright, pre-1923 books are available to read on Kindle, including titles such as The Adventures of Sherlock Holmes, Pride and Prejudice, and Treasure Island.
  • Low Book Prices: New York Times Best Sellers and New Releases from $9.99.
  • Free Book Samples: Download and read first chapters for free before you decide to buy.

 

Kindle History

  • Amazon released the Kindle First Generation on November 19, 2007, for US$399 and it sold out in five and a half hours. The device remained out of stock for five months until late April 2008.
  • On February 10, 2009, Amazon announced the Kindle 2. It became available for purchase on February 23, 2009. The Kindle 2 features, a text-to-speech option to read the text aloud, and 2 GB of internal memory of which 1.4 GB is user-accessible. Amazon estimates that the Kindle 2 will hold about 1500 non-illustrated books. Unlike the Kindle First Generation, Kindle 2 does not have a slot for SD memory cards, was slimmer than the original Kindle.
  • On October 7, 2009, Amazon announced an international version of the Kindle 2 that works in over 100 countries. It became available October 19, 2009. The international Kindle 2 is physically very similar to the U.S.-only model, although it uses a different mobile network standard.
  • Since January 19, 2010, the Kindle DX International ships in 100 countries. The Kindle DX comes with a 9-inch E Ink screen instead of the 6 inch normal Kindle screen
  • On July 1, 2010, Amazon released a new revision of the Kindle DX (3rd Generation Kindle DX). As well as dropping the price from $489 to $379, the new Kindle DX has an e-ink screen with 50% better contrast ratio and comes only in a “graphite” color. It is speculated the color change is to improve contrast ratio perception even further, as some users found the previous white casing highlighted the fact that the e-ink background is gray and not white.
Image of Kindle DX
Kindle DX
Image of 6 inch Kindle 3
Kindle (Latest Generation)
Image of 6 inch Kindle 2
Kindle (2nd Generation)
Image of first generation Kindle
Kindle (1st Generation)

free counters

HTC unveils two Desire smartphones

Posted on

Smartphone maker HTC unveiled two new Android-based phones, along with an updated version of its own user interface, HTC Sense, and a new online service called HTCSense.com, to let users manage their phones from a computer.

The two phones are the HTC Desire, with a 4.3-inch display, and the powerful 1GHz Qualcomm Snapdragon processor; and the Desire Z, with a full slide-out QWERTY keyboard and a slightly less powerful Qualcomm chip. Both run Android 2.2, the Froyo version of the OS.

The Desire HD also has an 8 megapixel camera, which can record 720p HD video, and dual-LED flash. It supports Dolby Mobile with SRS virtual surround sound.

The HTC Desire and the HTC Desire Z smartphones
Two Android-based smartphones from HTC, the Desire, left, and the Desire Z, also include the HTCSense.com service, which consumers can use to manage their phones from a PC.

The Desire Z is said to resemble the just-available T-Mobile G2 Androidsmartphone. It runs runs an 800MHz Qualcomm processor. Its 5 megapixel camera also records 720p HD video. One big difference is the flip-out QWERTY keyboard. The keyboard was designed for “for people with long fingernails and chubby thumbs,” according to a story by UK tech news Web site Electricpig, which was present at the announcement. Pricing was not announced.

Both phones can connect to HTCSense.com, where users can locate a lost or stolen phone, sound the ringer, and remotely lock it. Through the Web site, users can add images, plug-ins and sounds to their phone.

The updated HTC Sense UI, which sits atop Android, includes a battery of new features, including a unified in-box. Maps can now be pre-cached, reducing the wait times in loading and zooming. Another addition is HTC Fast Boot, which cuts the time it takes to complete the phone’s power-up sequence to 10 seconds or less from about 45 seconds, according to reports.

The phones will be available in Europe and Asia starting next month. The Desire Z will ship in North America later this year, HTC said. See more details on HTC official site.

free counters