Monday, June 24, 2013

Don't know which map/collection to use?

This post will summarize about collection framework in a convenient way. This is continuation of the previous post about Collection Framework in Java

This diagram give you the basic understanding of when to use which collection.




This diagram give you the hierarchy of Collection framework with its ordering and sorting scenario.  








If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...

Sunday, June 23, 2013

Basic flow in Oracle Server Architecture

Oracle database is a set of files. It's exists until we delete them intentionally. There is no practical limits to the number of these files, and therefore no practical limits to the size of a database.We can access database only through Oracle instance.

What is Oracle Instance is one of the basic question?
In Simple term, we can say that oracle instance is a set of processes and memory structure : it exits on CPU and in the memory of the server node, and its existence is temporary.
When an instance is started, Oracle database allocate a memory structure area called SGA (System Global Area) and starts one or more background processes.

How instance is look like?
image is taken from oracle site.


An instance can be started and stopped.An instance begins when it is created with the STARTUP command and ends when it is terminated.
During this period, an instance can associate itself with one and only one database.
Users of the database establish session against the instance, and the instance then manages all the access to database. Without instance, there is no way to interact with database. An instance with oracle database makes up an Oracle server.


The processing model implemented by Oracle Server is that of client-server processing, often called as two-tier.
In Client-Server model, the generation of the user interface and much of the application logic is separated from the management of the data. An application developed using SQL, this means that the client tier generate the SQL queries and the server tier executes them.
This is a basic client-server split, with a local area network between two sides. Oracle use Oracle proprietary protocol, Oracle Net  as communication protocol between user process and server process.

Client tier consists of 2 components:

  • the users
  • the user processes.

Server tier consists of 3 components:

  • server process the execute the sql
  • the instance
  • database itself

Interaction :


  1. Each user process interact with user process.
  2. Each user process interact with server process, usually across a local area network.
  3. The server process interact with instance, and instance with database.

A session is user process communication with server process. There will be usually one user process per user and one server process per user process.
The user and server process that make the session  are launched on demand by users and terminated when no longer required: this is call log-on and log-off cycle.
The instance process and memory structure are launched by the DBA and persist until DBA intentionlly terminate them: this is called database start-up and shut-down cycle.



Never forget that all the communication with an oracle server follows the client-server model. Even if the user process is running on the same machine as the server, the client-server split  is still
enforced and the network protocol  are still used for the communication between the two processes.



Wednesday, June 12, 2013

Selection Sort Algorithm in Java

Selection sort is a in-place sorting algorithm. Works well small files.It starts by comparing the entire list for the lowest item and moves it to the #1 position. It then compares the rest of the list for the next-lowest item and places it in the #2 position and so on until all items are in the required order.

Steps to perform selection sort
  1. Get a hand of unsorted card.
  2. Set a marker for unsorted section at the front of hand
  3. Repeat 4 to 7 steps until one card is left in the unsorted selection
  4. Compare all unsorted card
  5. Select the smallest unsorted card
  6. Swap this card with the first card in the unsorted section
  7. Move the marker.
  8. Stop

Let take a deck of unsorted card (step 1).
7 8 5 2 4 6 3 
Now set a marker at the front of unsorted card (step 2).
|7 8 5 2 4 6 3

Compare all the card from 7 to 3 (step)
Find the smallest shorted card (step 4) i.e., 2
So we swap this card with the first unsorted card i.e., 2 is swap with 7
|2 8 5 7 4 6 3 
Move the marker (step 8)
2|8 5 7 4 6 3 

Similarly, we repeat the step
Find the smallest unsorted card i.e., 3 and replace with first unsorted card 8
2|3 5 7 4 6 8
Move the marker
2 3|5 7 4 6 8

Find the smallest unsorted card i.e., 4 and replace with first unsorted card 5
2 3|4 7 5 6 8
Move the marker
2 3 4|7 5 6 8

Find the smallest unsorted card i.e., 5 and replace with first unsorted card 7
2 3 4|5 7 6 8
Move the marker
2 3 4 5|7 6 8

Find the smallest unsorted card i.e., 6 and replace with first unsorted card 7
2 3 4 5
|6 7 8
Move the marker
2 3 4 5 6|7 8

Find the smallest unsorted card i.e., 7 and replace with first unsorted card 7
2 3 4 5 6|7 8
Move the marker
2 3 4 5 6 7|8

Now we have one unsorted card means algorithm is complete.

Demo:



Performance :
  • Worst Case Complexity : O(n^2)
  • Average Case ComplexityO(n^2) 
  • Best Case Complexity : O(n^2)


If you know anyone who has started learning Java, why not help them out! Just share this post with them. 
Thanks for studying today!...

Monday, June 10, 2013

Bubble Sort Algorithm in Java

Bubble Sort is the simplest sorting algorithm. It works by iterating the input array from the first element to last, comparing each pair of elements and swapping them if needed.
It continues its sort until no swaps are needed. The algorithm got its name the way smaller elements “bubble” to the top of the list.


Generally, insertion sort has better performance than bubble sort.
The only advantage of bubble sort over other implementation is that it can detect whether the input is already sorted or not. Also known as comparison sort.

Let take an array of input:
1 5 4 2 8
In first loop, it will iterate the entire element from 0th index to 4th index
Pass 1:

1 5 4 2 8 -> 1 5 4 2 8  no swap because 1>5 fails
1 5 4 2 8 -> 1 4 5 2 8 
no swap because 5>4 fails
1 4 5 2 8 -> 1 4 2 5
no swap because 5>2 fails
1 4 2 5 8 -> 1 4 2 5 8 no swap

Pass 2

1 4 2 5 8 -> 1 4 2 5 8 no swap
1 4 2 5 8 -> 1 2 4 5 8 swap occur because 4>2
1 2 4 5 8 -> 1 2 4 5 8 no swap

Pass 3:

1 2 4 5 8 -> 1 2 4 5 8 no swap
1 2 4 5 8 -> 1 2 4 5 8 no swap

Pass 4:

1 2 4 5 8 -> 1 2 4 5 8 no swap

Output:
1 2 4 5 8

If you see above, when the array is already sorted it will iterate over the array that why this algorithm complexity is O(n^2) even in best case.

We can improve it by using one Boolean flag. When there is no swap means array is  already sorted, then we can skip the remaining process.

This modified version improved the best cast of bubble sort to O(n).


Performance :

  • Worst Case Complexity : O(n^2)
  • Best Case Complexity (improved) : O(n) 
  • Average Case Complexity : O(n)

If you know anyone who has started learning Java, why not help them out! Just share this post with them. 
Thanks for studying today!...

Sunday, June 9, 2013

Insertion Sort Algorithm in Java

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It’s much less efficient on large list.




  1. Get a hand of unsorted card.
  2. We divide the card as sorted and unsorted by placing a marker after the first card.
  3. Repeat steps 4 to 6 until unsorted section is empty
  4. Select the first unsorted card.
  5. Swap this card to left until it arrives at the correct sorted position.
  6. Advance the marker to the right one 


Let take a deck of unsorted card (step 1).
7 8 5 2 4 6 3
Now we divide this into two portion one is sorted and another is unsorted (step 2).
7|8 5 2 4 6 3
Here we select the first unsorted card i.e., 8 (step 4). Since 8 is greater than 7 so we do not need to swap the card (step 5).
Advance the marker to the right (step 6)
7 8|5 2 4 6 3
Now we select the next unsorted card i.e. 5. Since 5 is less than 8, so we swap the card
7 5|8 2 4 6 3
Since 5 is still less than 7 we swap again until it is corrected sorted position.
5 7|8 2 4 6 3
Advance the marker.
5 7 8|2 4 6 3
Now we put 2 in the right position.
5 7 2|8 4 6 3
5 2 7|8 4 6 3
2 5 7|8 4 6 3
Advance the marker
2 5 7 8 |4 6 3
Now we put 4 in the right position.
2 5 7 4 |8 6 3
2 5 4 7 |8 6 3
2 4 5 7 |8 6 3
Advance the marker
2 4 5 7 8|6 3
Now we put 6 in the right position.
2 4 5 7 6|8 3
2 4 5 6 7|8 3
Advance the marker
2 4 5 6 7 8|3
Now we put 3 in the right position.
2 4 5 6 7 3|8
2 4 5 6 3 7|8
2 4 5 3 6 7|8
2 4 3 5 6 7|8
2 3 4 5 6 7|8
Advance the marker
2 3 4 5 6 7 8|Here is out sorted output.

Demo : 6 5 3 1 8 7 2 4 
Improved Version : If element are already sorted to its left means no swap is required then we can simply comes out of the loop.


Performance :
  • Worst Case Complexity : O(n^2) The simplest worst case input is an array sorted in reverse order. In these cases every iteration of the inner loop will scan and shift the entire sorted subsection of the array before inserting the next element
  • Average Case Complexity : O(n^2) that why which makes insertion sort impractical for sorting large arrays.
  • Best Case Complexity : O(n) When is array is already sorted. During each iteration, the first remaining element of the input is only compared with the right-most element of the sorted subsection of the array.


Q. For insertion sort, the number of entries we must index through when there are n elements in the array is
Ans : n-1


If you know anyone who has started learning Java, why not help them out! Just share this post with them. 
Thanks for studying today!...

Saturday, June 1, 2013

Upcasting and Downcasting in java

You can cast an object to another class type, but only if the current object type and the new class type are in the same hierarchy of derived classes, and one is a super-class of the other. Upcasting and downcasting are important part of java. The concept of Polymorphism can help you to better understand the meaning of upcasting and downcasting.

You can cast a reference to an object of a given class type upwards through its direct and indirect superclasses. For example, you could cast a reference to an object of type Spaniel directly to type Dog, type Animal, or type Object. You could write:

Spaniel aPet = new Spaniel(“Fang”);
Animal theAnimal = (Animal)aPet;
Dog theDog = (Dog)aPet;
Object theObject = (Object)aPet;

When you are assigning an object reference to a variable of a superclass type, you do not have to include the cast. You could write the assignment as:
Animal theAnimal = aPet;  // Cast the Spaniel to Animal
This would work just as well. The compiler is always prepared to insert a cast to a superclass type when necessary.

In simple term, we define polymorphism as " Having multiple forms" or " Having many forms". Consider the following. Ask yourself what is rectangle? Most would say its shape. A square,  circle and a triangle are also shapes. What I'm saying is that shape can take many forms or has multiple forms.

Polymorphism in Java
Class inheritance is not just about reusing classes that you have already defined as a basis for defining a new class. It also adds enormous flexibility to the way in which you can program your applications, with a mechanism called polymorphism. So what is polymorphism?

The word polymorphism generally means the ability to assume several different forms or shapes. In programming terms it means the ability of a single variable of a given type to be used to reference objects of different types and to automatically call the method that is specific to the type of object the variable references. This enables a single method call to behave differently, depending on the type of the object to which the call applies

First we create a base class shape. This base class implement a constructor that will accept 2 arguments and a method that will draw out shape.

We need to make out method draw() to behave polymorphically.

What we have done is created an array of the type Shape. Because Square and Circle are derived from Shape, we are able to put them in our array. What we are then doing is looping through all the elements of our array and calling draw for each of our types. Because we have overridden the draw method in each of our derived classes the output of our code is:
Draw a shape at 100,100
Draw a square at 200,200
Draw a circle at 300,300
If we did not override Draw in one of our derived classes, the base class implementation of Draw would be called

Upcasting and Downcasting
First, you must understand, that by casting you are not actually changing the object itself, you are just labeling it differentlyFor example, if you create a circle and upcast it to shape, then the object doesn't stop from being a circle. It's still a circle, but it's just treated as any other shape and it's circle properties are hidden until it's downcasted to a circle again.


Circle c=new Circle();
System.out.println(c);
Shape s=c;
System.out.println(s);

Output will be look like this :
Circle@12b6651
Circle@12b6651

As you see circle is still exactly the same after upcasting, it didn't change to shape, its just being labeled as shape. This is allowed because circle is a shape.
There is no need to do upcasting manually, its allowed to do.
Shape s = (Shape)new Circle();
same as
Shape s = new Circle();

But downcasting must always be done manually:
Circle c = new Circle();
Shape s=c;
Circle c1 = (Circle)s;// manual downcasting to circle

Why upcasting is automatic and downcasting is manual because upcasting never fail. If you group of different shapes and want to downcast all of them to circle, then there may be chance, that some of shape are rectangle and code fail by throwing ClassCastException.

Consider this example;
shape s=new shape();
shape s=new circle();

You can store circle as a shape, and you can downcast it to circle when appropriate but doing this might be a sign of bad design.  On the other hand, compiler will let you to cast "s" to a circle, but you'll get a runtime error because the instance isn't actually a circle.

When to Cast Objects
You will have cause to cast objects in both directions through a class hierarchy.

You will cast object in upwards:

  • whenever you execute methods polymorphically, you are storing objects in a variable of a base class type and calling methods in a derived class. This generally involves casting the derived class objects to the base class
  • You want to cast up through a hierarchy is to pass an object of several possible subclasses to a method. By specifying a parameter as a base class type, you have the flexibility to pass an object of any derived class to it. You could pass a Dog, Duck, or Cat object to a method as an argument for a parameter of type Animal.
You will cast object in downwards:

The reason you might want to cast down through a class hierarchy is to execute a method unique to a particular class. If the Duck class has a method layEgg(), for example, you can't call this using a variable of type Animal, even though it references a Duck object. As I said, casting downward through a class hierarchy always requires an explicit cast.
The object pointed to by aPet is first cast to type Duck. The result of the cast is then used to call the method layEgg(). If the object were not of type Duck, the cast would cause an exception to be thrown.


If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...