Friday 1 August 2014

Nest for-loop n-times, recursive for loops



It is easy to write the code to nest the for-loop n-times if you know the value of n. For example, if you know n=3, then the code is as follows:


for(int i=0; i<n; i++){
       for(int j=0; j<n; j++){
          for(int k=0; k<n; k++){
          }
       }
   }


But what if the value of n is just a variable? How to write code for this? It can be solved using recursive function. The basic code is as follows:

public void func(int count, int n){
    if( count == n){
       return;
    }
    for(int i=0; i<length; i++){
        func(count+1,n);
     }
 }

And you just need to call func(0,n); The basic idea here is to pass the count number as an argument to the next recursive call. And the next recursive call can determine if to finish or continue based on that value. Here we only pass the count number to the subsequent call. We can also pass other values in the arguments to the next recursive call and solve some quite complex problems! If we make some twists in the actual body of the function, this code can be even more powerful!


Permutation Problem


Problem: Print out all the permutations of some letters. 
Inputs: An array of characters inChars of length n, where n is a positive integer 
Outputs: All possible permutations of these characters. 
Solution 1:

 public void func(int count, boolean[] used, char[] chs, StringBuilder sb){
    if ( count == used.length){
       System.out.println(sb.toString());
    }
    for(int i=0; i<chs.length; i++){
       if( !used[i]){
         sb.append(chs[i]);
         used[i] = true;
         func(count+1, used, chs, sb);
         used[i] = false;
         sb.setLength(count);
       }
    }
 }
 
 public void solve(char[] inChars){
   boolean[] used = new boolean[inChars.length];
   StringBuilder sb = new StringBuilder();
   func(0, used, inChars, sb);
 }
 

Test Results: 
For inChars = {'a','b','c'}, the output is

   abc
   acb
   bac
   bca
   cab
   cba

Notes:
  1. The above code for permutation is a little different from the n-Queens problem. In the n-Queens problem, each subsequent loop still goes throug all the positions, whereas here, the subsequent loops only go through partial values. They use the used[] array to check if a value has already been used.


Wednesday 23 July 2014

How Thread.start() indirectly calling your run() method?



Signature of the Thread class is "public class Thread implements Runnable", which means that smart JAVA designers gave you a direct implementation of Runnable interface and called it as a Thread class. This Thread class has a reference variable called target (private Runnable target). Thread class contains several constructors, one such constructor can take your new Runnable implementation and point the target reference to your implementation class by using an init method inside the constructor ( private void init(ThreadGroup g, Runnable target, String name,long stackSize)). 

When you call start method on the Thread object, target reference will point to your object, and JVM (Note: call is from JVM not from Thread class) will call run method of the Thread object (this object), run method will simply call target.run() which is nothing but your run method implementation.

Friday 16 May 2014

My Note on Core Java.

1. Serializable Interface and Inheritance:-
   
     If a top level object implements Serializable  interface, then by default, all the child objects will be Serializable. 

Tuesday 1 April 2014

Different context objects in ADF

Context Object Name
Who is creating it
Life Cycle phase
Scope
Comments
FacesContext
Faces Servlets
At beginning of each request processing
Per Request
Created by Faces Servlets and contains all the per request state information.This static class provides the request context information for the
application instance and grants developers access to application-wide settings, messages
to be displayed to the end user, lifecycle methods, and the displayed view root.
ExternalContext
 ?
 ?
 ?
 Wraps some of the external servlet functionality and information,
such as the servlet context, cookie maps, the remote user, the request locale, page
redirect, and much more. If developers don’t find what they are looking for within
the set of methods exposed on the ExternalContext, they can use a call to
getResponse or to getRequest to return instances of HttpServletResponse
and HttpServletRequest objects. The ExternalContext object is accessible
from FacesContext.
ADFContext




BindingContext
 ADFBinding filter
At the beginning of the first request
 Once per session

SecurityContext
 JPS security filter
 ?
 ?

ControllerContext




Application




Wednesday 19 March 2014

What is ADF?

              Application Development Framework is a proprietary 4th Generation java programming Language from Oracle. ADF is a metadata driven language, meaning it is fully controlled by XML files. My blog on ADF is intended to developers having prior experience in java and other frameworks like Spring or Structs.

                If you want to learn ADF, you have to understand the basic of 4GL. The main goal of 4GL is to make programmers life easy by using a smart and intelligent IDE which is capable for doing most of the work for you. As a programmer, what you have to do is control the behavior of these business components generated by the IDE by using appropriate properties. Jdeveloper is the IDE used by Oracle for building ADF applications. Why Jdeveloper? The answer is very simple, Oracle want a smart and intelligent IDE for integrating the features of declarative development. Also one major advantage of ADF is to use Groovy to control the behavior of ADF components.

                When I started working on ADF with prior experience in other framework like Spring and Structs, most of the time I was searching for good blogs or documents explaining about the core internal details of the ADF framework and I failed to get those. So I thought of putting some details on ADF by using this blog and that will help other programmer like me who is coming from other java framework background.

                XML is a common language used by all the programming languages and it is a right choice for Oracle to choose this as one of the building block. We use Oracle JDeveloper to create xml files at the time of development and this xml documents are called design time documents or artifacts. ADF Framework uses this documents at runtime and generates appropriate business objects. So it is very important to understand the design time document and runtime object’s behavior when we are dealing with ADF.

                ADF have two core parts in any web application, i.e ADF BC or it is otherwise called ADFm and ADF View or it is otherwise called ADFv. ADFm is dealing with the business layer and ADFv is dealing with presentation layer of the web application. Understanding of both of these components is critical if you are going to develop the ADF web application. So this ADF tutorial blog consists of two parts ADFm and ADFv. We will cover other parts like ADF Web services and SOA at appropriate time.