Search This Blog

Tuesday, December 30, 2014

SOA Question 3

Oracle Mediator is a service component (Oracle SOA Suite) that provides mediation capabilities. Which of the given options is NOT true?

a) Routing
b) Transformation
c) Filtering
d) Cannot modify routing rules at runtime
e) Validation 

SOA Question 2

Web services are loosely coupled and self-contained units of functionality.

a) True
b) False

Sunday, December 28, 2014

SOA Question - 1

Which statement is TRUE about SOA (Service Oriented Architecture)?

a) It is an Oracle product
b) It is a Microsoft product
c) It is an IBM product
d) It is independent of any vendor, technology, or product
e) It is a programming language
f)  It is a modeling language like UML

Tuesday, March 4, 2014

Which Constructors are called?

public class First {
    public First(){
        super();
        System.out.println("First... No Actually Second");
    }
}

public class Second extends First {
    public Second(){
        super();
        System.out.println("Second...No third");
    }
    public static void main(String[] args) {
        Second second= new Second();
    }
}

Will this code run? Answer to that is YES.

OUTPUT : 

First... No Actually Second
Second...No third

Why highlighted statement super() in First class does not give an error?

As we create an object of Subclass'Second', constructors are called in the order of creation of classes. Second() makes call to First() before it executes the instructions in Second(). First() makes a call to its Super class constructor Object()  which is Universal Super class of all Java Objects.

Saturday, February 22, 2014

Date and Time --- Simple Arithmetic

/* Program to add and subtract days and Hours. It can be applied for other fields of date and time too */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;
import java.util.Calendar;


public class DateTimeUtility {
    public static void main(String[] args) {
        int addDays = 4;
        int subtractDays = 10;
        int addHours=3;
        int subtractHours=26;
     
        Calendar cal = Calendar.getInstance();
        //System.out.println(cal);
        //Current date
        System.out.println("Today's date : " + (cal.get(Calendar.MONTH) + 1) +
                "/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));
   
        //Current Time
        System.out.println("Current Time  : " + (cal.get(Calendar.HOUR) ) +
                ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND));

        //Adding days
        cal.add(Calendar.DATE, addDays);
        System.out.println("Date (after): " + (cal.get(Calendar.MONTH) + 1) +
                "/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));
        cal = Calendar.getInstance();

        //subtracting days
        cal.add(Calendar.DATE, -subtractDays);
        System.out.println("Date (before): " + (cal.get(Calendar.MONTH) + 1) +
               "/" + cal.get(Calendar.DATE) + "/" + cal.get(Calendar.YEAR));

       //Adding Hours
        cal.add(Calendar.HOUR, addHours);
        System.out.println("New Time after adding " + addHours +" Hours "+
                cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE) +
                ":" + cal.get(Calendar.SECOND));
        cal=Calendar.getInstance();

       //Subtracting Hours
        cal.add(Calendar.HOUR, -subtractHours);
        System.out.println("Time before " + subtractHours+" Hours "+
                (cal.get(Calendar.HOUR) ) + ":" + cal.get(Calendar.MINUTE) +
                ":" + cal.get(Calendar.SECOND));
    }
}

OUTPUT :

Today's date : 2/22/2014
Current Time  : 6:22:6
Date (after): 2/26/2014
Date (before): 2/12/2014
New Time after adding 3 Hours 9:22:6
Time before 26 Hours 4:22:6

Thursday, January 30, 2014

Something about Overloading...

Overloading allows to use same name of method for multiple methods provided signatures are different. Signatures can be different in number, in type or in order. Example below shows.

public class OverloadDemo {
    public int add(int x, int y){
        return x+y;
    }
    public int add(int x, int y, int z){
        return x+y+z;
    }

    public double add(int x, double y){
        return x+y;
    }

    public double add(double x, int y){
        return x+y;
    }

    public static void main(String[] args) {
        OverloadDemo obj = new OverloadDemo();

        System.out.println(obj.add(10,20));
        System.out.println(obj.add(10,20,56));
        System.out.println(obj.add(10,20.9));
        System.out.println(obj.add(10.7,20));
    }

}

Output :

run:
30
86
30.9
30.7

It is simple. It is a user-friendly feature of Java and all Object-Oriented Programming languages.

Here is a small twist. See the code ...

public class AnotherOverload {
 
    int add (int x, int y){
        System.out.println("Int Overload");
        return x+y;
    }
    int add (short x, short y){
        System.out.println("Short Overload");
        return x+y;
    }
    long add(long x, long y){
        System.out.println("Long overload");
        return x+y;
    }
    double add(double x, double y){
        System.out.println("double overload");
        return x+y;
    }
    float add(float x, float y){
        System.out.println("double overload");
        return x+y;
    }
    public static void main(String[] args) {
        AnotherOverload ob = new AnotherOverload();
        System.out.println("sum of 10 + 20 is "+ob.add(10,20));
        System.out.println("sum of 10.1 + 20.1 is "+ob.add(10.1,20.2));
    }
}
Output:

Int Overload
sum of 10 + 20 is 30
double overload
sum of 10.1 + 20.1 is 30.299999999999997

There are two Questions over here :

10 and 20 are short type values according to the range, rather I can say it falls in the range of byte. But the method is called with an int parameters. Likewise 10.1 and 20.1 are float values but the call is for double. All non-real numeric literals by default are int and all real literals are double.

Here is another interesting scenario...

class IntOverload {
public static void main(String args[]) {
int x=10,y=20;
                short a=5, b=7;

IntOverload ob = new IntOverload();
System.out.println(ob.add(x,y));
        System.out.println(ob.add(a,b));
        
}
        
        short add(short a, short b){
            System.out.println("Short");
            return a+b;
        }

long add(long a, long b){
System.out.println("long");
return a+b;
}

float add(float a, float b){
System.out.println("float");
return a+b;
}

double add(double a, double b){
System.out.println("double");
return a+b;
}
}

This code does not compile and give an error :

When add method is called with explicit short type, it gives compile time error "Required :short, found:int".

All integer type numerics are promoted to int type at the time of computation. So the solution can be achieved by changing the method code. We use explicit cast... just a word of caution when overloading...

 short add(short a, short b){
            System.out.println("Short");
            return (short)(a+b);
        }

Enjoy coding... :)

Monday, January 27, 2014

Understanding final keyword part - 2 Just a small tidbit... 'njoy java

Now, here is an interesting scenario for you, a simple mind excercise.

Java understands final lil different from constant. Say I create a final array with the following code.

public class FinalArray1 {
    private final int[] arr = new int[5];
   
    public FinalArray1(){
        for(int i=0; i<arr.length;i++){
            arr[i]= (int)(Math.random()*25);
        }
    }
    public void printArray(){
        System.out.print("[ ");
        for(int i=0;i<arr.length;i++){
             System.out.print(arr[i]+" ");
        }
        System.out.println("]");
    }
    public void setNthElement(int index,int value){
        if(index<arr.length){
            arr[index]=value;
        }
    }
}

To test whether array is final or not, here is a Tester class:

public class TestFinalArray1 {
    public static void main(String[] args) {
        FinalArray1 arrayObj = new FinalArray1();
        arrayObj.printArray();
        arrayObj.setNthElement(2, (int)(Math.random()*100));
        arrayObj.printArray();
    }
}

On execution, we can see the data of 3rd element is changed.

run:
[ 11 15 11 10 11 ]
[ 11 15 30 10 11 ]
BUILD SUCCESSFUL (total time: 1 second)

So in what sense is it constant? let us take a look at this. We add newArray(int size) method to FinalArray1 class. 

public void newArray(int size){
        arr= new int[size];
    }



we can see an error on compile : "cannot assign a value to final variable arr". arr array reference variable is final and not the array elements.