Monday, September 13, 2010

A classic recursive algorithm

Here is a classic recursive algorithm.  The mathematical formula for factorial:
  * 1!=1
  * 2!=2 (2*1)
  * 3!=6 (3*2*1)
  * 4!=24 (4*3*2*1)
  * 5!=120 (5*4*3*2*1)
  * and so on

This can be described in pseudo-code as:
fact(x) ::= if ( x == 1 ) then 1 else x + fact(x-1)

In Java code it would look like this:

1    public int fact(int x) {
2        if ( x== 1 )
3            return 1;
4        else
5            return x * fact(x-1);
6    }

It is recursive because the function has a call to itself (see line 5).  The most important part of creating a recursive algorithm is to determine the halt condition.  In the case of the fact function the halt condition is when x==1.  If you fail to code a halt condition (or if the condition has a bug) then you will likely run into a infinite loop which in Java would result in a StackOverflowError.

Saturday, September 11, 2010

Difference Between JSF and Servlet/Jsp

1) JSF is a framework
A framework is just a set of components that allows a developer to build applications more quickly.  A framework provides APIs and classes that the developer uses to build application code.  Any package that fits this description can be called a framework.  As such, the Servlet and JSP packages are frameworks just as JSF is a framework that is built on top of Servlet/JSP frameworks.


2) JSF supports RAD but Servlet/JSP does not
RAD is a buzzword ,butany managers think that any RAD framework will be the next silver bullet that will solve all of there development problems.  This is never true.  Every framework can improve developer productivity after that developer has become fluent in that framework.  If a developer does not get trained in the framework (or technology) then that person will not see any productivity gains by adopted the framework.  This is true of any new technology or framework.

Servlets were the first Java web technology but it was difficult to build HTML views in Java code so Sun created JSPs to make the development of views faster.  After that several independent organizations developed frameworks on top of Servlet/JSP, such as Struts, to make webapp development easier and faster.  Sun took the best ideas and created JSF.  Like these other frameworks JSF is great for some types of applications but not so great for others.
JSF is very good at handling simple forms, tables and even dynamic tree structures. 
JSF also has aspects that hinder rapid development.  In particular there is a lot of configuration (XML files) that need to be generated and maintained.

3) JSF is event-driven but Servlet/JSP does not
This is true.  In practice, JSF event model turns into the standard request/response model.
Servlet/JSP architecture can support fine-grained screen events by using Ajax calls to the server.


4) JSF supports MVC but Servlet/JSP does not
JSF is no better at supporting MVC than a simpler Servlet/JSP architecture.  The MVC pattern takes discipline with any GUI framework. 
JSF is only different in that the controller code is implemented in a regular Java class and not in a Servlet Java class.  That is the only difference.


6) JSF provides a well-defined life cycle model but Servlet/JSP does not
This is true and it is one of the greatest benefits of an architecture based upon JSF.
A Servlet/JSP architecture must build its own life cycle.  This is usually quite easy but it does take effort and discipline.

7) JSF life cycle model supports validation but Servlet/JSP does not
This is true, but there is nothing about a Servlet/JSP architecture that prevents validation;

8) JSF has built-in support for I18N but Servlet/JSP does not
While it is true that JSF has built-in support for internationalization (abbreviated as I18N), it is not exactly true to say that Servlet/JSP does not.  JSTL libraries includes I18N support.

Wednesday, September 8, 2010

The Template Method

Reference Site : http://en.wikipedia.org/wiki/Template_method_pattern
Reference Book:Gang of Four - Design Patterns, Elements of Reusable Object Oriented Software

Topic Name:Template Method (Design Pattern )
Introduction :
1-The template method pattern is a design pattern.

2-Template Method is part of the Behavioral Patterns(prescribe the way objects interact with each other).

 3-Template Method is used to represent Parent and child relation ship,it force to subclass to implement the abstract method and also it's provided the blue Print or out line to the subclass.

Description of the problem:
I have a AbstractGame.This class is represent the Game Play between Two team .it have getWinner() method .which is give the winner between  two team on the basic of the score of individual .Let me give the proto type of getWinner()

Proto Type:public Team getWinner();
We have two  other type of the  class and they are  extends the AbstractGame class.Classes are following
1-StratingGame  it represent the starting game(Direct show the relation ship between two team )
2-IntermediateGame it represent the intermediate game or winner of previously game
On the basic of individual Characterised both class implement the getWinner() algorithm differently way .
getWinner() algorithm is the same for both types of games but the algorithm for determining team1 and team2 is different: static for starting games and the winner's of the previous games for intermediate games.
Solution Of this Problem: Using Template we are solve this problem

                                                  TM-pattern-problem

TM-pattern-solution
Example:
Supper Class is AbstractGame
-------------------------------------------------------------------
public abstract AbstractGame {
protected Integer team1Score;
protected Integer team2Score;
//abstract method
public abstract String getTeam1();
public abstract String getTeam2();
//"blueprint"
//Template Method
public String getWinner(){
if(team1Score>team2Score){
return getTeam1();
}
else {
return getTeam2();
}
}
---------------------------------------------------------------------
 StratingGame
---------------------------------------------------------------------
public StratingGame extends AbstractGame {
private String team1;
private String team2;
public String getTeam1(){
return team1;
}
public String getTeam2(){
return team2;
}
}
---------------------------------------------------------------------
Intermediate Game
--------------------------------------------------------------------
public class IntermediateGame extends AbstractGame {
private AbstractGame game1;

private AbstractGame game2;
//
// Accessor methods
//
public AbstractGame getGame1() {
return game1;
}
public void setGame1(AbstractGame game1) {
this.game1 = game1;
}
public AbstractGame getGame2() {
return game2;
}
public void setGame2(AbstractGame game2) {
this.game2 = game2;
}
@Override
public Team getTeam1(){
return getGame1().getWinner();
}
@Override
public Team getTeam2() {
return getGame2().getWinner();
}
}
-----------------------------------------------------------------------------------------------------------

Thursday, September 2, 2010

How to Reverse String

package com.example;

import java.util.Scanner;

public class Backward {
   public static void main (String[] Args)
   {
       Scanner in=new Scanner(System.in);
       System.out.print("Enter a Name to Reverse:");
       String name = in.nextLine();
       char and;
       String out="";
       int l=name.length();
       int i;
       for (i=l-1; i>=0; i--)
       {
           and=name.charAt(i);
           System.out.println(and);
           out=out+and;
       }
       System.out.println("The Output is:" + out);
   }
}