Wednesday, 12 August 2015

Java Program to check whether open parenthesis is closed properly or not


              Java Program to check whether open parenthesis is closed properly or not

Many times we want to do a validation on some formula or some string where we need to check whether open parenthesis is closed properly or not.

for ex:- String strFormula=(1+2);

We are going to implement this using stack where we will push the '(' open parenthesis character as soon as it encounters and we will pop out as soon as we encounters closed parenthesis.


 import java.util.Stack;  
 /**  
  *   
  */  
 /**  
  * @author Dixit  
  *   
  */  
 public class ValidateParenthesis {  
      public static void main(String args[]) {  
           String strFormulaCondition = "(1+2)";  
           boolean result = checkForparenthesis(strFormulaCondition);  
           System.out.println("Result:-" + result);  
           String strNewFormulaCondition = "((1+2)";  
           System.out.println("Result:-"  
                     + checkForparenthesis(strNewFormulaCondition));  
      }  
      /**  
       * It checks whether open parenthesis is closed properly or not in report  
       * formula notation  
       *   
       * @param strReportFormula  
       */  
      public static boolean checkForparenthesis(String strReportFormula) {  
           Stack<Integer> stk = new Stack<Integer>();  
           for (int i = 0; i < strReportFormula.length(); i++) {  
                char ch = strReportFormula.charAt(i);  
                if (ch == '(')  
                     stk.push(i);  
                else if (ch == ')') {  
                     try {  
                          int p = stk.pop() + 1;  
                     } catch (Exception e) {  
                     }  
                }  
           }  
           if (!stk.isEmpty()) {  
                return false;  
           } else {  
                return true;  
           }  
      }  
 }  


Enjoy programming:)



Sunday, 9 August 2015

Java program to Swap Two Numbers



There are different ways with which you can implement this program.Its very common question asked in interviews about swapping of two numbers so you must know all the way to do this program.

Here is the implementation below:


 /**  
  *   
  */  
 /**  
  * @author Dixit  
  *   
  */  
 public class SwapNumbers {  
      public static void main(String args[]) {  
           int a = 10;  
           int b = 20;  
           swapNumbersUsingTemporaryVariable(a, b);  
           swapNumbersWithoutUsingTemporaryVariable(a, b);  
           swapNumbersUsingXOROperator(a, b);  
           swapNumbersUsingMuliplicationAndDivisionOperator(a, b);  
      }  
      private static void swapNumbersUsingMuliplicationAndDivisionOperator(int a,  
                int b) {  
           System.out.println("***swapNumbersUsingMuliplicationAndDivisionOperator***\n");  
           System.out.println("Before swap:");  
           System.out.println("a value: " + a);  
           System.out.println("b value: " + b);  
           b = a * b;  
           a = b / a;  
           b = b / a;  
           System.out.println("After swap:");  
           System.out.println("a value: " + a);  
           System.out.println("b value: " + b);  
      }  
      private static void swapNumbersUsingXOROperator(int a, int b) {  
           System.out.println("\n***swapNumbersUsingXOROperator***\n");  
           System.out.println("Before swap:");  
           System.out.println("a value: " + a);  
           System.out.println("b value: " + b);  
           a = a ^ b;  
           b = a ^ b;  
           a = a ^ b;  
           System.out.println("After swap:");  
           System.out.println("a value: " + a);  
           System.out.println("b value: " + b);  
      }  
      private static void swapNumbersWithoutUsingTemporaryVariable(int a, int b) {  
           System.out.println("\n***swapNumbersWithoutUsingTemporaryVariable***\n");  
           System.out.println("Before swap:");  
           System.out.println("a value: " + a);  
           System.out.println("b value: " + b);  
           a = a + b;  
           b = a - b;  
           a = a - b;  
           System.out.println("After swap:");  
           System.out.println("a value: " + a);  
           System.out.println("b value: " + b);  
      }  
      private static void swapNumbersUsingTemporaryVariable(int a, int b) {  
           System.out.println("\n***swapNumbersUsingTemporaryVariable***\n");  
           int temp;  
           System.out.println("Before swap:");  
           System.out.println("a value: " + a);  
           System.out.println("b value: " + b);  
           temp = a;  
           a = b;  
           b = temp;  
           System.out.println("After swap:");  
           System.out.println("a value: " + a);  
           System.out.println("b value: " + b);  
      }  
 }  


Enjoy Coding. :)