Popular Posts

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, February 22, 2012

Matrix Chain Multiplication

Code:


import java.util.Scanner;


public class MatricsChainMul {
    public static void main(String[] args) {
        int m[][];
        int s[][];
        int mat_count;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter how many no of matrices to calculate");
        mat_count = scan.nextInt();
        mat_count++;
        int r[] = new int[mat_count];
        int c[] = new int[mat_count];
        m = new int[mat_count][mat_count];
        s = new int[mat_count][mat_count];
   
        for(int i = 1;i < mat_count;i++){
            System.out.println("Enter "+i+" matrix rows and cols");
            r[i] = scan.nextInt();
            c[i] = scan.nextInt();
        }
       
       
        c[0] = r[1];
       
        for(int i = 1; i < mat_count;i++){
            for(int j = 1; j < mat_count; j++)
            m[i][j] = 0;
        }
       
       
        int count = mat_count - 1;
        int a = 1;
        int temp = 0;
       
        while(count > 0){
            for(int i = 1; i < count; i++){
                int j = i + a;
                for(int k = i; k < j; k++){
                    temp = m [i][k] + m[k+1][j] + (c[i-1] * c[k] * c[j]);
                    if(m[i][j] == 0 || m[i][j] > temp){
                        m[i][j] = temp;
                        s[i][j] = k;
                    }
                }
               
            }
                count--;
                a++;
        }
        System.out.println("\n------------M Matrix------------");
        print(m, mat_count - 1);
        System.out.println("\n------------S Matrix------------");
        print(s, mat_count-1);
       
    }   
   
   
    static void print(int mat[][], int c) {
       
        int row = 1;
        int col = c;
        int i = row,j= col;
        while(true){
            System.out.print(mat[i][j]+"\t");
            if(j == col){
                System.out.println();
                j = col - i;
                i = row;
            }else{
                i++;
                j++;
            }
            if(i == col)
                break;
           
        }
       
    }
   

}


Largest Common Subsequence

Code:


public class LCS {
    static String str1, str2;
    static int m, n, c[][];
    static char b[][];
   
    public static void main(String[] args) {
   
    if(args.length != 2){
        System.out.println("It should take Two Strings");
        System.exit(0);
    }
    str1 = args[0];
    str2 = args[1];
    m = str1.length();
    n = str2.length();
    m++;
    n++;
    c = new int[m][n];
    b = new char[m][n];
    c[0][0] = 0;
   
    for(int i = 1; i < m ; i++)
        c[i][0] = 0;
   
    for(int j = 0; j < n; j++)
        c[0][j] = 0;
   
    for(int i = 1; i < m ; i++) {
        for(int j = 1; j < n; j++) {
        if(str1.charAt(i-1) == str2.charAt(j-1)) {
            c[i][j] = c[i-1][j -1] + 1;
            b[i][j] = 'S';
        } else if(c[i-1][j] >= c[i][j-1]) {
            c[i][j] = c[i-1][j];
            b[i][j] = 'U';
        } else {
            c[i][j] = c[i][j-1];
            b[i][j] = 'D';
        }
        }
    }
    System.out.println("-----------------C Array------------");
   
    for(int i = 0; i < m ; i++) {
        for(int j = 0; j < n; j++) {
        System.out.print(c[i][j] + " ");
        }
       
        System.out.println();
       
    }
   
    System.out.println("-----------------B Array------------");
   
    for(int i = 1; i < m ; i++) {
        for(int j = 1; j < n; j++) {
        System.out.print(b[i][j] + " ");
        }
       
        System.out.println();
       
    }
   
    print_LCS(b,m-1,n-1);   
   
    }
   
   
    static void print_LCS(char b[][],int m ,int n){
    if(m == 0 || n == 0)
        return;
    if(b[m][n] == 'S') {
        print_LCS(b, m-1, n-1);
        System.out.print(str1.charAt(m-1));
    } else if (b[m][n] == 'U') {
        print_LCS(b, m-1, n);
    } else {
        print_LCS(b, m, n-1);
    }
   
    }
}

Monday, February 13, 2012

Simple Chatting Program in Java

   This article explain how to write simple chatting GUI program in java using Socket. I use MigLayout for Layout Manager. It is a simple third party java layout , you can download it from miglayout-3.7-swing.jar or It is available with my project Files.

System Setup:
   Install JDK 1.4 or above JDK versions in your Windows or Linux Systems.
   

Running APPS:
Runnig Server  side System:
  Download  Server files for server side communication.

  In Windows:

  • Open command prompt.
  • Goes to the directory (folder) where you save the project and type the below commands.
  • javac -cp ;miglayout-3.7-swing.jar ServerGUI.java 
       for comipling the project.
  • java -cp ;miglayout-3.7-swing.jar ServerGUI for Running project.


In Linux:
  • Open terminal window.
  • Goes to the directory (folder) where you save the project and type the below commands.
  • export CLASSPATH =${CLASSPATH}:(path of the jar file with .jar extension).
  • javac  ServerGUI.java   for compilation.
  • java  ServerGUI   for Runing the project.


 It asks  your name, After enter  your name ,System waiting for client request, If any request received from client, The server accept and shows the frame for communication.


Runnig Client side System:
      Download  Client  files for server side communication. 

   Make sure you run Server App in one system in the Network before run Client App.
Commands:
  In Windows:
  • Open command prompt.
  • Goes to the directory (folder) where you save the project and type the below commands.
  • javac -cp ;miglayout-3.7-swing.jar ClientGUI.java   for compilation.
  •  java -cp ;miglayout-3.7-swing.jar ClientGUI   for Runing the project.



In Linux:
  • Open terminal window.
  • Goes to the directory (folder) where you save the project and type the below commands.
  • export CLASSPATH =${CLASSPATH}:(path of the jar file with .jar extension).
  • javac  ClientGUI.java   for compilation.
  • java  ClientGUI  for Runing the project.

    It asks the server IP address and your name, After enter  IP address and your name the frame appear on the screen, Now you are ready to chat.

Testing in a Single System in Network:
   It is also possible to test the Server and Client Apps in the same System using the above procedures,
If you connected in an Network.

Testing in a Single System with out  Network Connection:
 Normally there is a IP Address 127.0.0.1 for Loop back connection, use this IP address .