Friday, June 19, 2009

Project Euler Problem 5

Problem 5

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?


public class ProblemFive {
public static void main(String[] args) {
// Any number will be always divisible by 1
// To be divisible by 2 it has to be an even number
// Number will be greater than or equal to at least 20
int start = 20;

while (true) {
int i = 3;

for (; i <= 20; ++i) {
if (0 != (start % i)) {
break;
}
}

if (i >= 20) {
break;
}

start += 2;
}

System.out.println(start);
}
}

Wednesday, June 17, 2009

RIA frameworks compared

Web based application having desktop like GUI is popularly known as RIA (Rich Internet Application). In old days only few solutions were available namely Java applet, Flash, Dynamic html, etc. Nowadays there are loads of frameworks which provide desktop like experience. These frameworks are based on AJAX (Asynchronous JavaScript + XML) technology.

The choices of RIA frameworks are huge and it's difficult to choose the most suitable for your need. The best part of open source technology is for every problem there are numerous solutions, but the worst part is choosing the right technology for your need.

I found these available frameworks to build RIA - Adobe Flex, extJS, Jboss Richfaces, IceFaces, Oracle ADF, JavaFX, Silverlight, GWT, IT Mill Toolkit, ZK, OpenLaszlo, BackBase, Echo, Morfik, Haxe, YUI, pyjamas, DWR, Prototype, Curl, SproutCore, Cappuccino, jQuery, etc.

A selection criterion needs to be defined to select a suitable framework. I choose these criteria -
  • Popularity of the framework - The framework should be popular in developer community. Popularity brings good publications, community support, improvements, etc.

  • Support - A framework should have a good support in terms of books, training facilities, mailing lists, etc.

  • Out-of-the-box GUI components - A framework should provide ready to use components to save time in building basic blocks.

  • Underlying technology - Underlying technology is also important to check the learning curve, effort required, developer expertise, etc.


  • After applying the above criteria, I am left with extJS, Flex, GWT and Silverlight. These frameworks provide rich look and feel and have loads of ready to use components.

    extJS
    It's a cross browser Java Script library for building RIA.

    Advantages
  • Huge set of rich widgets.

  • Good API documentation available.


  • Disadvantages
  • It's not free.

  • JavaScript knowledge required.

  • No good support for Web Service.

  • To do simple things, lot of JavaScript coding required.

  • CSS customization is not easy.

  • Firebug debugging is of no use as the generated html is bad.


  • Adobe Flex
    Flex is a highly productive, free open source framework for building and maintaining expressive web applications that deploy consistently on all major browsers, desktops, and operating systems.

    Advantages
  • Best look and feel.

  • Rich design and multimedia as its flash.

  • Easy to deploy.

  • Flex Builder IDE to develop UI using drag and drop.

  • Open source.

  • Web service integration is very easy.

  • Desktop application can be also developed.


  • Disadvantages
  • Flex Builder is not free.

  • Knowledge of MXML and ActionScript required.

  • Requires Plugin installation on the client side.

  • Browser history support not possible.

  • Rendering flash is still slow.

  • RPC calls are cumber some and need third party software like BlazeDS.


  • GWT (Google Web Toolkit)
    Writing web apps today is a tedious and error-prone process. Developers can spend 90% of their time working around browser quirks. In addition, building, reusing, and maintaining large JavaScript code bases and AJAX components can be difficult and fragile. Google Web Toolkit (GWT), especially when combined with the Google Plugin for Eclipse, eases this burden by allowing developers to quickly build and maintain complex yet highly performant JavaScript front-end applications in the Java programming language.

    Advantages
  • Developer need to know only Java. No JavaScript.

  • Huge choices of ready to use components.

  • Eclipse IDE support to develop.

  • Code can be debugged in Eclipse.

  • Eclipse hosted mode available for hot deployment.

  • Look and feel is good and can be easily customized using CSS.

  • Maven supported so builds are easy.

  • Junit test cases to test the code.

  • Easy Web Service configuration based on annotations.

  • Similar to Java Swing.

  • Loads of active mailing lists to seek solution.

  • Doesn't require Plugin installation on the client side.

  • GWT is free so no cost.

  • Browser history support works.


  • Disadvantages
  • Only for Java developers.

  • Compiling Java to JavaScript is very slow.

  • No drag and drop IDE available.

  • Desktop application cannot be developed.


  • Microsoft Silver light
    Microsoft Silver light is a free runtime that powers rich application experiences and delivers high quality, interactive video across multiple platforms and browsers, using the .NET framework.

    Advantages
  • .Net knowledge helps.

  • Visual Studio IDE support.

  • Desktop applications can be developed.


  • Disadvantages
  • Requires plugin installation on the client side.

  • Fairly new framework so wider support is not available.

  • Its not open source.
  • Monday, June 15, 2009

    Project Euler Problem 4

    Problem 4

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.

    Find the largest palindrome made from the product of two 3-digit numbers.

    The solution is to multiply each three digit number and check if its a palindrome or not.


    public class ProblemFour {
    public static void main(String[] args) {
    int palindrome = 9009;

    for (int i = 999; i > 99; --i) {
    for (int j = i; j > 99; --j) {
    int product = i * j;

    if (isPalindrome(String.valueOf(product)) &&
    (product > palindrome)) {
    palindrome = product;
    }
    }
    }

    System.out.println(palindrome);
    }

    public static boolean isPalindrome(String data) {
    char[] array = data.toCharArray();
    int reverseIndex = array.length - 1;
    int index = 0;

    while (index < reverseIndex) {
    if (array[index] != array[reverseIndex]) {
    return false;
    }

    ++index;
    --reverseIndex;
    }

    return true;
    }
    }

    Hibernate OneToMany annotation mapping using JoinTable

    Hibernate OneToMany annotation mapping using JoinTable

    Recently I explored Hibernate OneToMany annotation mapping using JoinTable.

    Consider an Airline entity having list of Airports. The mapping between the two is stored in a JoinTable airline_airports.

    CREATE TABLE airline(
    code char(2) PRIMARY KEY NOT NULL,
    name varchar(50) NOT NULL
    );

    CREATE TABLE airport(
    code char(3) PRIMARY KEY NOT NULL,
    name varchar(50) NOT NULL
    );

    CREATE TABLE airline_airport(
    airline_code char(2) NOT NULL,
    airport_code char(3) NOT NULL,
    CONSTRAINT PRIMARY PRIMARY KEY (airline_code,airport_code)
    );

    ALTER TABLE airline_airport ADD CONSTRAINT airline_airport_ibfk_1
    FOREIGN KEY (airline_code) REFERENCES airline(code) ON DELETE NO ACTION ON UPDATE NO ACTION;

    ALTER TABLE airline_airport ADD CONSTRAINT airline_airport_ibfk_2
    FOREIGN KEY (airport_code) REFERENCES airport(code) ON DELETE NO ACTION ON UPDATE NO ACTION;

    First of all we need to create Hibernate Entities for Airline and Airport using annotations. Notice the annotations defined for airports attribute of Airline class. This is where we are defining the relationship between Airline and Airport.

    @Entity
    @Table(name = "airline")
    public class Airline implements Serializable {
    @Id
    @Column(name = "code")
    private String code;
    @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinTable(name = "airline_airport", joinColumns = @JoinColumn(name = "airline_code")
    , inverseJoinColumns = @JoinColumn(name = "airport_code")
    )
    private List airports;

    public String getCode() {
    return code;
    }

    public void setCode(String code) {
    this.code = code;
    }

    public List getAirports() {
    return airports;
    }

    public void setAirports(List airports) {
    this.airports = airports;
    }
    }

    @Entity
    @Table(name = "airport")
    public class Airport implements Serializable {
    @Id
    @Column(name = "code")
    private String code;
    @Column(name = "name")
    private String name;

    public String getCode() {
    return code;
    }

    public void setCode(String code) {
    this.code = code;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }
    }

    Create an Airline object and add a list of Airlines. When you will save the airline object, hibernate will persist all the data in airline, airport and airline_airport table.

    If you don't use the cascade annotation then ii will not save the data in airport table. This means it will fail to insert record in airline_airport table if an airport is not already in the airport table.

    Monday, June 8, 2009

    Benefits of GWT

    After evaluating and reading various Rich Internet Application solutions, I have finally selected GWT and GWT-Ext.

    I developed a prototype using GWT 1.6.4, GWT-Ext 2.0.5, GWTRPC-spring 1.01 on Tomcat 6.0.18 with back end in Spring 2.5.6 and Hibernate 3.3.1.

    I kept the architecture simple and separate layers by developing UI in GWT and business logic in Spring beans. As you know the whole GWT UI runs in user browser and Spring beans running in Tomcat. GWT Async architecture is used to interact with Spring beans. GWT converts the Java code into Javascript at deploy time.

    I finalized GWT based on these parameters -

    • Only Java knowledge is required.

    • Team is accustomed to Eclipse so this was an advantage for us.

    • Ability to use Java Debugger in Eclipse is extremely helpful.

    • GWT Hosted mode in eclipse, so no compile and deploy required on every change.
    • Big developer community.

    • Lots of ready to use components.

    • Prior Java knowledge helpful.

    • Similar to swing and the team have worked on Swing in earlier project.

    • Look and feel is good.

    • Maven support is also available.

    • Can write Junit test cases.

    • Easy RPC configuration based on annotations.