Friday, June 7, 2013

Final Keyword in JAVA

  • A java variable can be declared using the keyword final. Then the final variable can be assigned only once.
  • A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialise it.
  • Java classes declared as final cannot be extended. Restricting inheritance!
  • Methods declared as final cannot be overridden. In methods private is equal to final, but in variables it is not.
  • final parameters – values of the parameters cannot be changed after initialization. Do a small java exercise to find out the implications of final parameters in method overriding.
  • Java local classes can only reference local variables and parameters that are declared as final.
  • A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance.

Wednesday, March 13, 2013

PolyAlphabetic_cipher in Java


import java.util.*;
public class PolyAlphabetic
{
char alphabets[][]=new char[26][26];
Scanner s,s1;
String key,plntxt,enctxt,ciptxt,dectxt;

public PolyAlphabetic()
{
assign();
s=new Scanner(System.in);
s1=new Scanner(System.in);
System.out.println("Press 1 to Encrypt a plain text \nPress 2 to Decrypt a cipher text ");
int input=s1.nextInt();
if(input==1)
{
System.out.print("Enter the key :");
key=s.nextLine();
System.out.print("Enter the text to be encrypt :");
plntxt=s.nextLine();
encrypt(plntxt, key);
}

if(input==2)
{
System.out.print("Enter the key :");
key=s.nextLine();
System.out.print("Enter the text to be decrypt :");
ciptxt=s.nextLine();
decrypt(ciptxt, key);
}

//print();
}
public void assign()
{
for(int i=0;i<26;i++)
{
for(int j=0;j<26;j++)
{
alphabets[i][j]=(char)(((i+j)%26)+97);
}
}
}
public void print()
{
for(int i=0;i<26;i++)
{
for(int j=0;j<26;j++)
{
System.out.print(alphabets[i][j]+" ");
}
System.out.println();
}
}

public void encrypt(String text, String k)
{
String str="";
for(int i=0;i<text.length();i++)
{
str=str+alphabets[(int)(k.charAt(i%k.length()))-97][(int)text.charAt(i)-97];

}
enctxt=str;
System.out.println("Encrypted text is "+enctxt);
}

public void decrypt(String text, String k)
{
String str="";
for(int i=0;i<text.length();i++)
{
for(int j=0;j<26;j++)
{
if(alphabets[(int)(k.charAt(i%k.length()))-97][j]==text.charAt(i))
str=str+alphabets[0][j];
}
}
dectxt=str;
System.out.println("Decrypted text is "+dectxt);
}
public static void main(String args[])
{
PolyAlphabetic pa=new PolyAlphabetic();
}
}

Sunday, February 10, 2013

Matrix sorting by row wise



class Sort_Matrix{
public static void main (String args[]){
 int [][] M ={ {1,2,3,4},{2,3,4,5},
                      {5,6,7,8},{6,7,8,5},{3,7,9,3} };
 for(int r=0;r<5;r++){
   for(int c=0;c<4;c++){
     System.out.print(M[r][c]+"\t ");
    }
   System.out.println();
  }
  for(int r=0;r<5;r++){
  int maxEle=M[r][0];
   for(int c=1;c<4;c++){
   if(M[r][c]>maxEle){
    int temp=maxEle;
    maxEle=M[r][c];
    M[r][c]=temp;
    }
   }
   System.out.println(
"Maximum row "+(r+1)+"element is= "+maxEle);
   }
  }
}

Addition of Two matrix


we are going to calculate the sum of two matrix and containing its rows and columns. See below for better understanding to this: 
In this program we are going to calculate the sum of two matrix. To make this program, we need to declare two dimensional array of type integer.
Now we need to make a matrix out of it. To make the matrix we will use the for loop. By making use of the for loop the rows and column will get divide. This process will be performed again for creating the second matrix. After getting both the matrix with us, we need to sum both the matrix. The both matrix will be added by using the for loop with C[r][c]= A[r][c]+B[r][c]. The output will be displayed by using the print() method.


public class addMatrix{
      int A[][];
      int B[][];
      int C[][];
      int row;
      int column;

public addMatrix(int r,int c){
row=r;
column=c;
      A=new int[r][c];
      B=new int[r][c];
      C=new int[r][c];

for(int i=0;i<r;i++){
    for(int j=0;j<c;j++){

A[i][j]=(int) Math.round(Math.random()*99+10);
B[i][j]=(int) Math.round(Math.random()*70+10);
}

}
}
public void addition(){
for(int i=0;i<row;i++){
   for(int j=0;j<column;j++){

C[i][j]=A[i][j]+B[i][j];
}
}
}
public void display(){

for(int i=0;i<row;i++){
  for(int j=0;j<column;j++){

System.out.print(C[i][j]+"\t");
}
System.out.println();
}

}

public static void main(String args[]){
addMatrix m=new addMatrix(4,5);
m.addition();
m.display();
}
}

Output is:

C:\Users\Hari\Desktop\java>java addMatrix
134     88      115     87      161
65      188     52      166     98
86      83      43      106     93
62      104     100     49      159


similarly substraction as given


Subtraction of two matrix in java

we are going to calculate the subtraction of two matrix and containing its rows and columns. See below for better understanding to this: 
In this program we are going to calculate the  subtraction  of two matrix. To make this program, we need to declare two dimensional array of type integer. Now we need to make a matrix out of it. To make the matrix we will use the for loop.
By making use of the for loop the rows and column will get divide. This process will be performed again for creating the second matrix. After getting both the matrix with us, we need to subtraction  both the matrix. The both matrix will be subtracted by using the for loop with C[r][c]= A[r][c]-B[r][c]. The output will be displayed by using the print() method.

Creating a Matrix


In this example  you will learn about array and matrix. An array is the collection of same data type values. If we create a variable of integer type then, the array of int can only store the int values. It can't store other than int data type.
Matrix: A matrix is a collection of data in rows and columns format.
In this program we are going to implement a matrix. To make a program over the two dimensional array,list of all we have to declare class named as 
Matrix , For displaying the matrix we need to its rows and column . Now, we use the for loop to print all the values stored in the array. At last use the main() method inside which we are going to declare the values of the multidimensional array which we are going to use.  The output will be displayed to the user by print() ,println() methods.





public class createMatrix{
    public static void main(String args[]){
              int Matrix[][]={{1,4,5,6},{2,4,5,6},{5,6,8,9},{2,4,7,9}};

for(int i=0;i<Matrix.length;i++){
     for(int j=0;j<Matrix[i].length;j++){
       System.out.print(Matrix[i][j]+"\t");
}
System.out.println();
}
}
}



or


public class createMatrix{
    public static void main(String args[]){
              int Matrix[][]=new int[4][4];

for(int i=0;i<4;i++){
     for(int j=0;j<4;j++){

Matrix[i][j]=(int) Math.round(Math.random()*89+11);
       System.out.print(Matrix[i][j]+"\t");
}
System.out.println();
}
}
}       



output for 1:
C:\Users\Hari\Desktop\java>java createMatrix
1       4       5       6
2       4       5       6
5       6       8       9
2       4       7       9


output for 2:
C:\Users\Hari\Desktop\java>java createMatrix
1       4       5       6
2       4       5       6
5       6       8       9
2       4       7       9
  

Thursday, February 7, 2013

N Queen Problem

public class NQueens { 
  
    int[] x; 
  
    public NQueens(int N) { 
        x = new int[N]; 

    } 
  
    public boolean canPlaceQueen(int r, int c) { 
       
        for (int i = 0; i < r; i++) { 
            if (x[i] == c || (Math.abs(i - r) == Math.abs(x[i] - c)) )  
            { 
                return false; 
            } 
        } 
        return true; 
    } 
  
    public void printQueens(int[] x) { 
        int N = x.length; 
        for (int i = 0; i < N; i++) { 
            for (int j = 0; j < N; j++) { 
                if (x[i] == j) { 
                    System.out.print("Q "); 
                } else { 
                    System.out.print("* "); 
                } 
            } 
            System.out.println(); 
        } 
        System.out.println(); 
    } 
  
    public void placeNqueens(int r, int n) { 
        
        for (int c = 0; c < n; c++) { 
            if (canPlaceQueen(r, c)) { 
                x[r] = c; 
                if (r == n - 1) { 
                   printQueens(x); 
                } else { 
                    placeNqueens(r + 1, n); 
                } 
            } 
        } 
    } 
  
    public void callplaceNqueens() { 
        placeNqueens(0, x.length); 
    } 
  
    public static void main(String args[]) { 
        NQueens Q = new NQueens(5); 
        Q.callplaceNqueens(); 

       
    } 
}