Monday, 30 November 2015

Method Overriding Example - Java Program

package com.javaproficiency;


class Y {

void display(){
System.out.println(" Y is called");
}

}


public class X extends Y{

void display(){
System.out.println(" X is called");
}


public static void main(String[] args) {

    Y y = new Y();
    X x = new X();
    Y obj = new X();
   
    y.display();
    x.display();
    obj.display();
   
   

}

}



Output:

 Y is called

 X is called

 X is called

How to Find Common Element In Two arrays - java program

package com.javaproficiency;

public class CommonElementOfTwoArray {

public static void main(String[] args) {

  int []arr = {10,15,20,25,40};
       int []brr ={5,18,20,25,30,10};
     
       for( int i=0 ; i < arr.length ; i++ ){
            for( int j=0 ; j < brr.length ; j++  ){
                if(arr[i] == brr[j])
                    System.out.println(arr[i]);
            }
       }

}

}


Output:

10
20
25








Thursday, 26 November 2015

How to count the number of occurrences of a character in a string in java

Count the number of occurrences of a character in a string is good programming questions . It is ask in colleges and schools mostly. This question also asked in many times in interviews. 

Method 1:

We can count number of occurrences of a character in a string with the help of indexOf() method.

package com.javaproficiency;

public class CountCharOccurance {
public static void main(String[] args) {
String str = "javaproficiencyexamplebyannu";
 
    String findStr = "a";
    int lastIndex = 0;
    int counter = 0;
 
    while (lastIndex != -1) {
 
     lastIndex = str.indexOf(findStr, lastIndex);
 
     if (lastIndex != -1) {
      counter++;
      lastIndex += findStr.length();
 
     }
    }
    System.out.println(counter);
}


}


Output: 

4


Method 2:

In this example we will use charAt() method of string for count number of occurrences of a character in a string .

package com.javaproficiency;

public class CountCharOccurancejava {
public static void main(String[] args) {
String s = "javaproficiencyexamplebyannu";
int counter = 0;
for( int i=0; i<s.length(); i++ ) {
   if( s.charAt(i) == 'a' ) {
       counter++;
   } 
}
System.out.println( counter );
}

}


Output:

4


How to print numbers from 1 to 10 without using loop?

We can print numbers 1 to 10 or 1 to 100 using recursion. Recursion is a good alternatives of loop. There we can use recursion to print numbers 1 to 10. We also use goto statement but in java we can not used goto statement. visit java keyword list http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html. So we will use here recursion.


package com.javaproficiency;

public class PrintNumber {

public static void main(String[] args) {

printNumberWithRecursion(1);

}

public static void printNumberWithRecursion(int n){

if (n <= 10) {
System.out.println(" number is ="+ n);
printNumberWithRecursion(n+1);
}

}

}


Output :

 number is =1
 number is =2
 number is =3
 number is =4
 number is =5
 number is =6
 number is =7
 number is =8
 number is =9
 number is =10

How to Sort the String using string Method In java

Java have a lot of method for string operations. We can sort a string by convert in to array of characters , now we will sort array after that we will make a new string from sorted array.Now our new string is in sorted order of characters.

package com.javaproficiency;

import java.util.Arrays;

public class StringSort {

public static void main(String[] args) {
String str = "xyabds";
char [] arr = str.toCharArray();
Arrays.sort(arr);
String sortedStr = new String(arr);
System.out.println(" sorted string = "+sortedStr);
}

}

Output:

 sorted string = abdsxy

Wednesday, 25 November 2015

Object Cloning in java example?

package com.javaproficiency;

public class Emp implements Cloneable {
//Object Cloning in java example?

int id;
String name;

public Emp(int id, String name) {
super();
this.id = id;
this.name = name;
}

public Emp clone() throws CloneNotSupportedException{

return (Emp ) super.clone();

}

public static void main(String[] args) {

Emp  emp = new Emp(10, "java");
Emp emp1 = null;
try {
emp1 = emp.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(" clone object is ");
System.out.println("id ="+ emp1.id+ " name ="+ emp1.name);

}

}


Output:

 clone object is
 id =10 name =java