Sunday, 6 July 2014

Excetion Propagation (Stack Trace)

class Propagation
{
    void show() throws Exception
    {
        int a = 10/0;            //    unchecked exception
    }
    void disp() throws Exception
    {
        show();
    }
    public static void main(String...s)
    {
        Propagation ob = new Propagation();                // this concept is known as object propagation.
        try
        {
            ob.disp();
        }
        catch(Exception e)
        {
            System.out.println(e);
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

    }
}

Printing a Circular Matrix(Spiral Matrix)

import java.io.*;
class Circular
    {
        public static void main(String args[])throws IOException
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter the number of elements : ");
            int n=Integer.parseInt(br.readLine());

            int A[][]=new int[n][n];
            int k=1, c1=0, c2=n-1, r1=0, r2=n-1;

            while(k<=n*n)
                {
                    for(int i=c1;i<=c2;i++)
                    {
                            A[r1][i]=k++;
                    }

                    for(int j=r1+1;j<=r2;j++)
                    {
                            A[j][c2]=k++;
                    }

                    for(int i=c2-1;i>=c1;i--)
                    {
                            A[r2][i]=k++;
                    }

                    for(int j=r2-1;j>=r1+1;j--)
                    {
                            A[j][c1]=k++;
                    }

                 c1++;
                 c2--;
                 r1++;
                 r2--;
                }

            System.out.println("The Circular Matrix is:");
            for(int i=0;i<n;i++)
                {
                    for(int j=0;j<n;j++)
                        {
                                System.out.print(A[i][j]+ "\t");
                        }
                     System.out.println();
                }
        }
}

Printing boundary of a matrix

import java.io.*;

class boundary
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int r,c;
        System.out.println("Enter row:");
        r=Integer.parseInt(br.readLine());
        System.out.println("Enter column:");
        c=Integer.parseInt(br.readLine());
        int[][] ar=new int[r][c];
        System.out.println("Enter value in array:");
        for(int i=0; i<r; i++)
        {
            for(int j=0; j<r; j++)
            {
                ar[i][j]=Integer.parseInt(br.readLine());
            }
        }
        for(int i=0; i<r; i++)
        {
            for(int j=0; j<r; j++)
            {
                System.out.print(ar[i][j] +" ");
            }
               System.out.println("\n");
        }
        for(int i=0; i<r; i++)
        {
            for(int j=0; j<r; j++)
            {
                if(i==0||j==0)
                {
                    System.out.print(ar[i][j] +"  ");
                }
                else if(i==r-1 || j==c-1)
                {

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

Tuesday, 10 June 2014

Sorting A 2D matrix.

import java.lang.*;
import java.io.*;
class Matrixkr
{

public static void main(String args[])    throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);

int temp=0;
System.out.println("enter no of rows");
int r=Integer.parseInt(br.readLine());
System.out.println("enter no of columns");
int c=Integer.parseInt(br.readLine());
int ar[][]=new int[r][c];
System.out.println("enter the elements");
       for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
    ar[i][j]=Integer.parseInt(br.readLine());
}
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
    System.out.print(ar[i][j]+" ");
}
System.out.println();
}

for(int x=0;x<r;x++)
{
for(int y=0;y<c;y++)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(ar[i][j]>ar[x][y])
{
temp=ar[x][y];
ar[x][y]=ar[i][j];
ar[i][j]=temp;
}

}

}

}

}
System.out.println("after sorting");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(ar[i][j]+" ");
}
System.out.println();
}
}
}




Printing Pascal Triangle.

import java.util.Scanner;
class Pascal
{
static int factorial(int n)
{
  int sum = n;
  for(int k=n-1;k>=1;k--)
    sum*=k;
  if(sum!=0)
    return sum;
  else
    return 1;
}
public static void main(String...args)
{
  int s,lines;
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter no of lines:");
  lines = sc.nextInt();
  for(int i=0;i<lines;i++)
  {
    for(int j=lines;j>i;j--)
      System.out.print(" ");
    for(int k=0;k<=i;k++)
    {
      s = factorial(i)/(factorial(k)*factorial(i-k));
      System.out.print(s+" ");
    }
    System.out.println();
  }
}
}

Small example of Interface.

interface A
{
int a=10;
void show();
}
class Interface implements A
{
public void show()
{
System.out.println("Show in class");
}
public static void main(String...args)
{
A i = new Interface();
i.show();
}
}