Wednesday, December 4, 2002

T R A N S P O S E A M A T R I X

package org.interview.test;

import java.util.Scanner;

/**
 * This code can be used to check if a matrix symmetric or not, 
 * just compare the matrix with it's transpose if they are same then it's 
 * symmetric otherwise non symmetric, also it's useful for calculating 
 * orthogonality of a matrix.
 * @author ADixit3034C
 *
 */
public class TransposeAMatrix {

public static void main(String args[])
  {
     int m, n, c, d;
 
     Scanner in = new Scanner(System.in);
     System.out.println("Enter the number of rows and columns of matrix");
     m = in.nextInt();
     n = in.nextInt();
 
     int matrix[][] = new int[m][n];
 
     System.out.println("Enter the elements of matrix");
 
     for ( c = 0 ; c < m ; c++ )
        for ( d = 0 ; d < n ; d++ )
           matrix[c][d] = in.nextInt();
 
     int transpose[][] = new int[n][m];
 
     for ( c = 0 ; c < m ; c++ )
     {
        for ( d = 0 ; d < n ; d++ )               
           transpose[d][c] = matrix[c][d];
     }
 
     System.out.println("Transpose of entered matrix:-");
 
     for ( c = 0 ; c < n ; c++ )
     {
        for ( d = 0 ; d < m ; d++ )
              System.out.print(transpose[c][d]+"\t");
 
        System.out.print("\n");
     }
  }
}

Tuesday, December 3, 2002

SWAP NUMBERS

package org.interview.test;

import java.util.Scanner;

public class SwapNumbers {

public static void main(String args[])
  {
swapNumberTemp();
swapNumWithOutTempVar();
  }

public static void swapNumWithOutTempVar () {
int x, y;
     System.out.println("Enter x and y");
     Scanner in = new Scanner(System.in);
 
     x = in.nextInt();
     y = in.nextInt();
 
     System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
 
     x = x + y;
     y = x - y;
     x = x - y;
 
     System.out.println("After Swapping\nx = "+x+"\ny = "+y);
}

public static void swapNumberTemp(){
     int x, y, temp;
     System.out.println("Enter x and y");
     Scanner in = new Scanner(System.in);
 
     x = in.nextInt();
     y = in.nextInt();
 
     System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
 
     temp = x;
     x = y;
     y = temp;
 
     System.out.println("After Swapping\nx = "+x+"\ny = "+y);
}

}

R E V E R S E A N U M B E R

package org.interview.test;

import java.util.Scanner;

public class ReverseANumber {

public static void main(String args[])
  {
     int n, reverse = 0;
 
     System.out.println("Enter the number to reverse");
     Scanner in = new Scanner(System.in);
     n = in.nextInt();
 
     while( n != 0 )
     {
         reverse = reverse * 10;
         reverse = reverse + n%10;
         n = n/10;
     }
 
     System.out.println("Reverse of entered number is "+reverse);
  }

}

P R I N T A L P H A B E T S

package org.interview.test;

public class PrintAlphabets {

public static void main(String [] args) throws Exception {
char x = 'a';

for (x ='a'; x <= 'z' ; x++) {
System.out.print(x);
}
System.out.println();

char y = 'a';
while (y <= 'z') {
System.out.print(y);
y++;
}

System.out.println();

char c = 'a';
do {
System.out.print(c);
c++;
} while (c <= 'z');
}
}

L I N E A R S E A R C H

package org.interview.test;

import java.util.Scanner;

/**
 * Above code locate first instance of element to found, you can modify it for multiple 
 * occurrence of same element and count how many times it occur in the list. 
 * Similarly you can find if an alphabet is present in a string.
 *
 */
public class LinearSearch {

public static void main (String [] args) throws Exception {
int c, n, search, array[];
 
   Scanner in = new Scanner(System.in);
   System.out.println("Enter number of elements");
   n = in.nextInt(); 
   array = new int[n];
 
   System.out.println("Enter " + n + " integers");
 
   for (c = 0; c < n; c++)
     array[c] = in.nextInt();
 
   System.out.println("Enter value to find");
   search = in.nextInt();
 
   for (c = 0; c < n; c++)
   {
     if (array[c] == search)     /* Searching element is present */
     {
        System.out.println(search + " is present at location " + (c + 1) + ".");
         break;
     }
  }
  if (c == n)  /* Searching element is absent */
     System.out.println(search + " is not present in array.");
}
}

Monday, December 2, 2002

S Y S T E M C O M M A N D S

package org.interview.test;

import java.net.InetAddress;
import java.util.Random;

public class SystemCommands {

public static void main (String [] args) throws Exception {
Runtime rt = Runtime.getRuntime();
//rt.exec("notepad");
//rt.exec("mspaint");

System.out.println("Free memory in JVM before Garbage Collection = "+rt.freeMemory());
   rt.gc();
   System.out.println("Free memory in JVM after Garbage Collection = "+rt.freeMemory());
     System.out.println(InetAddress.getLocalHost());
     
     int c;
     Random t = new Random();
  
     // random integers in [0, 100]
  
     for (c = 1; c <= 10; c++) {
       System.out.println(t.nextInt(100));
     }
     
}
}

P A L I N D R O M E

package org.interview.test;

import java.util.Scanner;

public class Palindrome {

public static void main(String [] args) throws Exception {
String original , reverse = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter:" );
original = sc.next();

int length = original.length();

System.out.println("Length:"+length);

for(int i =length-1; i >= 0; i--) {
reverse = reverse + original.charAt(i);
}

System.out.println("Reverse:"+ reverse);

if(original.equals(reverse)) {
System.out.println(original+" - is palindrome of - " + reverse);
}
}

}
= = = = = = = = = = = = = = = = = Reverse String recursive and iterative = = = = = = = = = = = = = = = = = = = = = 
package com.test;

import java.io.FileNotFoundException;
import java.io.IOException;

public class StringReverseExample {

    public static void main(String args[]) throws FileNotFoundException, IOException {

        //original string
        String str = "Sony is going to introduce Internet TV soon";
        System.out.println("Original String: " + str);

        //reversed string using Stringbuffer
        String reverseStr = new StringBuffer(str).reverse().toString();
        System.out.println("Reverse String in Java using StringBuffer: " + reverseStr);

        //iterative method to reverse String in Java
        reverseStr = reverse(str);
        System.out.println("Reverse String in Java using Iteration: " + reverseStr);

        //recursive method to reverse String in Java
        reverseStr = reverseRecursively(str);
        System.out.println("Reverse String in Java using Recursion: " + reverseStr);

    }

    public static String reverse(String str) {
        StringBuilder strBuilder = new StringBuilder();
        char[] strChars = str.toCharArray();

        for (int i = strChars.length - 1; i >= 0; i--) {
            strBuilder.append(strChars[i]);
        }

        return strBuilder.toString();
    }

    public static String reverseRecursively(String str) {

        //base case to handle one char string and empty string
        if (str.length() < 2) {
            return str;
        }

        return reverseRecursively(str.substring(1)) + str.charAt(0);

    }
}