Friday, May 24, 2013

Why new watches show 10:10 behind any reason is there

                                          When we  go to watch or clock shop,where all watches are showing 10:10.Can you any movement think why clocks are showing this time.Yes,for showing this time on watches behind one interesting reason is there.
                                         The hours hand at 10 and minutes hand at 2.The reason for this,read below statements
1. When we are enter into the watch shop,every one think about price of watch, brand, Warranty and other details.But never think why watches showing 10:10.

2. Behind it there is one beautiful and interesting reason is there.

3. Clock and watch makers to sale there watches in market,they need one logo for selling  their goods.They want logo to be simple and meaningful.

4. Then they trying keep watch hands on different number then observe that.10:10 place showing Smiley face.It is possible only on 10:10,not possible on any other numbers.

5. Keep this logo on watches sales their goods different way in market.But this is very interesting and hiding symbol.


Watch showing 10:10







Smiley Face

















Tuesday, May 21, 2013

Sort two input arrays in Ascending order and find the median of elements without using any predefined methods of Java

                                 Sorting of array is easy to sort the array in descending order.To sort the array elements  java gives the predefined methods.Sort two input array elements in ascending order without using any predefined methods of Java,this question asked in Cap-Gemini Coding Challenge Test. This program is some what difficult but i have a easy way to write this program.

 Actually Cap-Gemini given this as below:

In Bangalore, Cap-Gemini has departments.Department A has m employees with empNumber and Department B has n employees with empNumber. Combine this two departments into single department and sort the empNumbers  in ascending order. And find the median of employ.
Conditions:
All employees are even number then 
  pos1=(m+n)/2
pos2=(m+n)/2+1
avg=(pos1+pos2)/2
Odd numbers:
 pos=(m+n)/2

   
 Example:
 Input:
           Employ Numbers of Department A={12,6,5,2,3}
           Employ Numbers of Department B={9,5,13,10,1,11}

Output:
           Employees of Department of A following token numbers:12  6 5 2 3
         
           Employees of Department of B following token numbers: 9 5 13 10 1 11
         
           Total Employees=11
     
           Median=9
          
For program click on below link:


Sunday, May 19, 2013

Write a program to sort the integer number without using any predefined method of java

                     For this program we cannot use any predefined method of java to write this program.First we under stand question clearly. Directly not possible to sort integer number but we can sort integer array.

            In this program, first i take input integer value as String.Convert string value to character array and sort character array. Assign character array to String.Now convert String to integer.

            This is the process to sort integer value without using any predefined methods of java.

If you want see the click on below link:

Saturday, May 11, 2013

How many ways to create object for java class

                        If you want to create object for java class, generally our program has new keyword and constructor or default constructor.But we can create object in & ways.These are 
1. By using new keyword

2.By using newInstance() method

3.By using static factory method

4.By using instance factory method

5.By using factory pattern

6.By using cloning process

7.By using Deserilization

1.By using new keyword:
              We can create object for java class by using new keyword.It is a normal process.
  Example:
            String name=new String("RRV");
            Test t=new Test();

2. By using newInstance() method:
     
          We can create object for java class by using newInstance() method.Generally we can use new keyword for creating object but it checking java class at compile time that's why it is not useful in jdbc applications and other applications which taking java class files dynamically.For that purpose we use newInstance() method.

Example:
           Class c=Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
        JdbcOdbcDriver jod=(JdbcOdbcDriver)c.newInstance();

3. By using static factory method:
                Method of a java class that is capable of constructing and returing its own java class object is called as Static factory method.

Example: 
      Thread t=Thread.currentThread();
      Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");

Note:
         When java class contains only private constructors to create object of that class outside the class we take support od\f static factory method.
          Inabove  java.lang.Class is having only Private Constructor so we always use Class.forName() method as shown above create the object.

4.By using instance factory method:
                  In order to create a new object  for java class by using existing object and its data.

Example:
           String name=new String("RRV");
           String name1=name.concat("TechDiamond");

      In above example concat() method is using object "name" and its data while constructing "name1" object.

5. By using factory pattern:
             If method of one java class is returing object of other java class then it is called as Factory Pattern.

Example:
    StringBuffer name=new StringBuffer("RRVTechDiamond");
    String s=name.subString(6,3);
If method one java class wants to return another java class object then these both classes need not to be there in inheritance relationship.If they are visible to each other.

6.By using cloning process:
              The process of creating object of a java class having existing object data is called as cloning process.We can perform cloning only on clonable objects.
                                      
7.By using Deserialization process:
                  The process of reading data from a file and constructing new object having that data is called as Deserialization.





















Multi-Catch block

                           Multi -catch  block is the wonderful and assert for programmer.It is introduced in java 7 as part of Exception-Handling.
                           Up-to Java 6 we are using multiful catch block in java programs.The main disadvantage of using this approach is increases length of the code and permanence will be decreased.To overcome this Sun people introduced Multi-catch block in Java 7 version.
                         
Syntax for Multicatch block:

try
{
  ---
  ---
}
catch(ArithmeticException|IOException..........)
{
 ---
 ---
}

Example Program of Multicatch block:

import java.io.BufferReader;
import java.io.FileReader;
import java.io.IOException;

class Multicatch
{
  public static void main(String[] args)
{
   try(BufferReader br=new BufferReader(new FileReader("c:\\Data.txt")))
   {
      String line;
     Class.forName("oracle.jdbc.driver.OracleDriver");
     Connection     con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oradsn","scott","tiger");
}
catch(IOException | ClassNotFoundException | SQLException e)
{
  e.printStackTrace();
}
}
}
   
    

try-with-resources

                 Try-with-resources is wonderful feature of Java 7.This feature introduced by Sun people as a part of Exception Handling.Try-with-resources means if we use any resources with-in the try block,the resources are closed automatically after completing usage of Try block.
                    
                    Generally try block is used when program has risky code,that risky code is kept inside of try block. Up-to Java 6 version,any clean up code is kept inside finally block.So that length of the code will be increased at a time performance will be decreased.For this reason Sun people introduced try-with-resources in java 7 version related to Exception Handling.
                      
                        The main advantage of try-with-resources is all resources with are open as part of try block will be closed automatically and we are not require to close explicitly.

Syntax of Try-with-resources:

   try(R1;R2;R3:...)
   {
      ---
      ---
    }
   
    Where R1,R2,and R3 are resources should be separated with semicolon and autoclosable resources.A resource is said to Autoclosable if and only if the corresponding class implements java.lang.AutoClosable interface either directly or indirectly.

Important Point related to Try-with-resources:
      
1. All resource reference variables are implicitly final. Hence with in the try block we cannot perform reassignment.

2. Until Java 6 version try is always associated with catch or finally.But from Java 7 onward's we can take try without catch or finally.

3. By using try-with-resources,finally block will become Dummy and we are not require to write finally block explicitly because the resources will be closed automatically.

Example Program With try-with-resources:

import java.io.BufferReader;
import java.io.FileReader;
import java.io.IOException;

class TryExample
{
   public static void main(String[] args) throws IOException
     {
       try(BufferReader br=new BufferReader(new FileReader("c:\\Data.txt")))
        {
           String line;
           
           while((line=br.readLine())!=null)
              {
                  System.out.println(line);
               }//while
          }//try
      }//end main()
 }// end class
             In the above program i write try block without catch or finally blocks,
Java 7 onward it is possible and resources will be automatically closed.