Wednesday, 6 January 2016

How to find string whether it is palindrome or not in java ??

package org.cmania;

public class PalindromeString
{
    public static void main(String[] args)
{
       String inputString="aabaa"; //String to be reversed    
           System.out.println(isPalindrome(inputString) ? inputString+ " is a palindrome." :  inputString+                                          "is not a palindrome.");    
       }
   
    /*
     * returns true if inputString is palindrome.
     */
   
    public static boolean isPalindrome(String inputString)
    {
        char ar[]=inputString.toCharArray();
        for(int i=0,j=ar.length-1; i<(ar.length/2); i++,j--){
               if(ar[i]!=ar[j])
                     return false;
        }
        return true;
   
}
   
  }

How to remove duplicate elements from sorted array in java ??

package org.cmania;

public class RemoveDuplicacyFromSortedArray
{
    public static void main(String[] args)
{
    int[] duplicateSortedAr = {1,3,7,8,8,9,14,16,16,17,17};
         int[] nonDuplicateSortedAr = removeDuplicates(duplicateSortedAr);
 
         System.out.print("Displaying contents of sorted array(with duplicate elements)  : ");
         for(int i=0;i<duplicateSortedAr.length;i++){
            System.out.print(duplicateSortedAr[i]+" ");
         }
       
         System.out.println();
         System.out.print("Displaying contents of sorted array(with non-duplicate elements) : ");
         for(int i=0;i<nonDuplicateSortedAr.length;i++){
            System.out.print(duplicateSortedAr[i]+" ");
         }
       
     }
   
     /**
      * This method returns sorted array with non duplicate elements
      */
   
     public static int[] removeDuplicates(int[] duplicateSortedAr)
     {
       
         int i=0, j=1;
 
         if(duplicateSortedAr.length < 2){ //means there is only one element in array.
          return duplicateSortedAr;
         }
       
         while(j < duplicateSortedAr.length){    
          if(duplicateSortedAr[j] == duplicateSortedAr[i])
              j++;
          else
              duplicateSortedAr[++i] = duplicateSortedAr[j++];        
         }
       
         int[] nonDuplicateSortedAr = new int[i+1];
         for(int x=0; x<nonDuplicateSortedAr.length; x++){
          nonDuplicateSortedAr[x] = duplicateSortedAr[x];
         }
       
         return nonDuplicateSortedAr;
     }
   
  }

How to find a number is binary or not in java ??

package org.cmania;

public class BinaryCollection
{
    public static void main(String[] args)
{

           int n=11001011;
           System.out.println(isBinaryNumber(n)? n+" is binary number." : n+" is not binary number.");
           n=11001110;
           System.out.println(isBinaryNumber(n)? n+" is binary number." : n+" is not binary number.");
           n=10000112;
           System.out.println(isBinaryNumber(n)? n+" is binary number." : n+" is not binary number.");
           n=11005110;
           System.out.println(isBinaryNumber(n)? n+" is binary number." : n+" is not binary number.");
    }
 
    /**
     * returns true if number is binary.
     */
    public static boolean isBinaryNumber(int n){    
        while(n != 0){
         if(n%10 > 1){
             return false;  //number containing any digit greater than 1 means its not binary.
         }
         n = n/10;  
        }
        return true;
    }  
 
}

How to find Prime number in java ??

package org.cmania;

public class PrimeTime
{
    public static void main(String[] args)
{
          int n=11;
              System.out.println(isPrimeNumber(n)? n+" is prime number." : n+" is not prime number.");               n=12;
             System.out.println(isPrimeNumber(n)? n+" is prime number." : n+" is not prime number.");               n=13;
             System.out.println(isPrimeNumber(n)? n+" is prime number." : n+" is not prime number.");               n=14;
             System.out.println(isPrimeNumber(n)? n+" is prime number." : n+" is not prime number.");
        }

          /** * returns true if number is prime using ternary operator. */


         public static boolean isPrimeNumber(int n)
           {
                for(int i=2;i<=Math.sqrt(n);i++)
                  {
                      if(n%i==0)
                       {
                            return false;
                        }
                   }
                return true; //means number wasn't divisible by any of the number, it's a prime number.
           }
 }

How to Reverse number in java ??

package org.cmania;

public class NumericReverse
{
    public static void main(String[] args)
{

int number=12345; //number to be reversed
         
          System.out.println("Original number: "+number); System.out.println("Reversed number: "+reverseNumber(number));
   }
  
   public static int reverseNumber(int number)
{ int reverse=0; int remainder; while(number>0){ remainder=number%10; number=number/10; reverse=reverse*10+remainder; } return reverse; }
}

Reverse String - keeping performance in mind (without StringBuffer) in java

package org.cmania;

public class ReverseString
{
    public static void main(String[] args)
{

String str="abcde"; //String to be reversed
       
                System.out.println("Original String: "+str);
                System.out.println("Reversed String: "+reverseString(str));        
        }

 /*
  * return reversed String with best performance.
  * Best case: O(n/2)- (1/2), when we have odd number of characters in string.
  * Average case: O(n/2) , generally when we have even number of characters in string.
  * Worst case: O(n/2).
  */

public static String reverseString(String str)
{
        char ar[]=str.toCharArray();
        char temp;
        for(int i=0,j=ar.length-1; i<(ar.length/2); i++,j--){
               temp=ar[i];
               ar[i]=ar[j];
               ar[j]=temp;
        }
        return new String(ar);
 }
}

How to check string contains numeric or alphabet in java string by using Regex(Regular Expression) ??

package org.cmania;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringNumericRegex {

public static void main(String[] args)
{
   Pattern pat = Pattern.compile(".*\\d.*");   // for digit detection only

// Pattern pat = Pattern.compile("[a-zA-Z]+");  for alphabetic detection

// Pattern pat = Pattern.compile("[a-zA-Z ]*\\d+.*");  for digit detection

// org.apache.commons.lang.StringUtils.isAlpha(str); for alphabetic detection

String str = "java8";

/**
* Except for splitting a string, you need to create a Matcher object from the Pattern object.
* The Matcher will do the actual work. The adv of having two separate classes is that
* you can create many Matcher objects from a single Pattern object, and thus apply the
* regular expression to many subject strings simultaneously.
*/

Matcher match = pat.matcher(str);

if (match.matches())
{
     System.out.println("String contains numeric values");
}
else
{
                     System.out.println("String doesn't contain any numberic values");
}


}

}

JSP interview questions and answers

Q1. What is JSP and why do we need it? JSP stands for JavaServer Pages. JSP is java server side technology to create dynamic web pages. J...