Gadgets

Thursday, 17 December 2015

Can we access private constructor in other package

How to access private constructor in other package:
Yes we can access by using reflection classes, we can create instance for private constructors.
To get the class.
Class c=Class.forName("test.Test");
To get all the constructors (public, protected, default, private) in the form of array from the class
Constructor con[]=c.getDeclaredConstructors();
con[0] is indicates first constructor, con[1] indicates second constructor and so an
if setAccessible(true) then all the constructors make as public
if setAccessible(false) then we can’t access all the constructors, we access only public constructors(depending on access specifier rules).
To create object for the class
con[0].newInstance(); 
Test.java
========
package test;
public class Test {
               private Test()
               {
                               System.out.println("private constructor...");                              
               }              
}
Client.java
=========
package core;
import java.lang.reflect.Constructor;
public class Client {
               public static void main(String[] arg)
               {
                               try{
                                              //to get the class
                                              Class c=Class.forName("test.Test");
                                              //to access all the constructors in the array form from the class
                                              Constructor con[]=c.getDeclaredConstructors();
                                              //to make constructor as public
                                              con[0].setAccessible(true);
                                              //to create object
                                              con[0].newInstance();
                               }catch(Exception ex){}
               }
}
/*
Output:
private constructor...
*/

Wednesday, 16 December 2015

First Spring application

Example of spring application
Creating first spring application in eclipse, here we have clear steps to create spring application
1. Create the java project
2. Add spring capabilities
3. Create the class file(POJO class)
4. Create the xml file
5. Create the Driver class
Create New Project:
Go to File menu - New - project - Java Project. Write the Project name and click Finish
Add Spring jar files:
Right click on project – Build Path – configure Build Path – select Libraries tab – Add External JARs


Create POJO class:
Test.java
=======
package test;
public class  Test
{
               public void hello()
               {
                               System.out.println("My first Spring Project");
               }
}

Create xml File:
Open org.springframework.beans-3.0.1.RELEASE-A.jar file with winJAR archiver
 and open folder org\springframework\beans\factory\xml
 in xml folder open spring-beans.dtd file
 in spring-beans.dtd file copy line 35 - 37 below code
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">




spring.xml
========
package resources;
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean class="test.Test" id="t">
</bean>
</beans>
Create Driver Class:
Client.java
========
package test;
class Client{
               public static void main(String[] args)
               {
                               //find xml file
                              Resource r=new ClassPathResource("resources/spring.xml");
                               //load xml file into container
                               BeanFactory factory=new XmlBeanFactory(r);
                               //creat test class object
                               Object o=factory.getBean("t");
                               //type casting object
                               Test t=(Test)o;
                               t.hello();
               }
}
/*
Output:
My first Spring Project
*/



Monday, 14 December 2015

Check the given string is palindrome or not

Check the given string is palindrome or not:
If we reverse a string that reverse string and original string both are equal then that string is palindrome.
Example:
Original string: java
Reverse string: avaj
Both are not equal so not palindrome.
Original string: madam
Reverse string: madam
Both are equal so palindrome.
length():- length() is final method, it is applicable for String Objects. length() method returns number of characters present in the String.
chatAt() method:-
Returns the char value at the specified index. An index ranges from 0 to length()-1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.
import java.util.Scanner;
public class PalindromeTest {
    public static void main(String[] arg){
        String name,rev="";
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Name :");
        name=sc.nextLine();        
        System.out.println("length of the String : "+name.length());
        int i=name.length();//i=string length
        for(int j=i-1;j>=0;j--){
           rev=rev+name.charAt(j);
        }
        System.out.println("reverse string is : "+rev);     
    }
}
/*
Output:-
Enter Name :
java
length of the String : 4
reverse string is : avaj
*/
import java.util.Scanner;
public class PalindromeTest {
    public static void main(String[] arg){
        String name,rev="";
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Name :");
        name=sc.nextLine();        
        System.out.println("length of the String : "+name.length());
        int i=name.length();//i=string length
        for(int j=i-1;j>=0;j--){
           rev=rev+name.charAt(j);
        }
        System.out.println("reverse string is : "+rev);
        if(name.equals(rev)){
            /* check given string and reverse string are equal or not*/
          System.out.println("palindrome ");  
        }
        else{
            System.out.println("not palindrome ");  
        }
    }
}
/*
Output-1:-
Enter Name :
java
length of the String : 4
reverse string is : avaj
not palindrome 
Output-2:-
Enter Name :
madam
length of the String : 5
reverse string is : madam
palindrome
*/



Sunday, 13 December 2015

find length of the string in java

To find length of the string we use length() method
length():- length() is final method, it is applicable for String Objects. Length() method returns number of characters present in the String.
import java.util.Scanner;
public class PalindromeTest {
    public static void main(String[] arg){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Name :");
        String name=sc.nextLine();
        System.out.println("length of the String : "+name.length());  
                               /*
                               length() is final method, it is useful to find the length of the string
                               */
    }
}
/*
Enter Name :
java
length of the String : 4

*/



length vs length() in java

length:- length is final variable, it is applicable for arrays and it returns the size of an array
Example:
int[ ] a={10,20,30};
System.out.print(a.length);
Here the above array size is 3.
In this case if we give length() instead of a.length then we will get compile time error.
length():- length() is final method, it is applicable for String Objects. Length() method returns number of characters present in the String.
Example:
String s="java";
System.out.print(s.length());
Here the above String length is 4(j,a,v,a)
Example:
String[ ] s={"java","html5","css"};
System.out.println(s.length);//3
System.out.println(s.length());//error
System.out.println(s[0].length());//4
System.out.println(s[0].length);//error
In the above example
‘s’ is an array and s[0] is String
s.length and s[0].length() are true 
Note: In multi dimensional arrays length variable represents only base size but not total size of an array.
int[ ] a=new int[2][3];
System.out.println(a.length);//2
System.out.println(a[0].length);//3
If we want find total size of multi dimensional array
System.out.println(a[0].length+a[1].length);//3+3=6

Count Even numbers from 1 to n

Here in this program I showed to count the how many even numbers from 1 to n/given number.
Even numbers from 0 to 20: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20
Total even numbers from 1 to 20 :11
import java.util.Scanner;
public class EvenNumbers {
    public static void main(String[] arg){
        int number,count=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any number :");
        number=sc.nextInt();
        System.out.println("Even Numbers are");
        for(int i=0;i<=number;i++){
          if(i%2==0){
              System.out.print(i+" ");
              count++;
          } 
        }
       System.out.println();
       System.out.println("total even numbers from 1 to "+number+" :"+count);
    }   
}
/*
Output:
Enter any number :
20
Even Numbers are
2 4 6 8 10 12 14 16 18 20
total even number from 1 to 20 :10
*/


Friday, 11 December 2015

Continue statement in java

Continue statement:
Continue statement is used when we want to skip the remaining code in the block then we use continue statement.
public class ContinueTest {
    public static void main(String arg[]){        
        for(int i=1;i<=10;i++){
            if(i<5){
                System.out.println("before continue : "+i);
                continue;
            }
        System.out.println("after continue : "+i);   
       }
    }
}



Thursday, 10 December 2015

Armstrong number in java

A number that is equal to the sum of cube of its digits
Example:
123=(1*1*1)+(2*2*2)+(3*3*3)
123 != 36 is not equal so it is not Armstrong
371=(3*3*3)+(7*7*7)+(1*1*1)
371==371 is equal, so it is Armstrong
import java.util.Scanner;
public class Armstrong {
    public static void main(String[] arg){
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Number");
        int i=sc.nextInt();
        int j=i;
        int a,b=0;
        while(i>0){
            a=i%10;
            b=b+(a*a*a);
            i=i/10;
        }
        if(b==j){
            System.out.println(j+" is Armstrong number");
        }
        else{
            System.out.println(j+" is not Armstrong number");
        }
    }   
}
/*
Output1:
Enter Number
123
123 is not Armstrong number
Output2:
Enter Number
371
371 is Armstrong number

*/