Tuesday, December 27, 2011

Java program that illustrates runtime polymorphism


// AIM : Java program that illustrates runtime polymorphism

import java.io.*;

abstract class animal
{
        //abstract method
        public abstract void move();
}
class man extends animal
{
        public void move()
        {
                System.out.println("Walks on his two legs..");
        }
}
class horse extends man
{
        public void move()
        {
                System.out.println("Runs on it's 4 legs..");
        }
}
class crocodile extends horse
{
        public void move()
        {
                System.out.println("Crawls on its belly..");
        }
}



class  poly
{
        public static void main(String[] args)
{
                System.out.print("\nRuntime polymorphism\n\n");
                animal a;
                a=new man();
                a.move(); //behaviour changed as man
                a=new crocodile();
                a.move(); //behaviour changed as crocodile
                a=new horse();
                a.move(); //behaviour changed as horse
}
}




WAJProgram to multiply two matrix elements if compatible and find transpose the matrix elements .


// AIM : program to multiply two matrix elements if compatible
//       and find transpose the matrix elements .

import java.io.*;

class  matmul
{
public static void main(String[] args) throws IOException
{
                int mat1[][]={{1,2,3},{4,5,6},{7,8,9}};
int mat2[][]={{1,2,3},{4,5,6},{7,8,9}};
                int mat3[][],tmat[][],i,j,k;
mat3=new int[3][3];
                tmat=new int[3][3];
                //comaptibility or length test
                if(mat1.length!=mat2.length)
                {
                System.out.println("Matrix not compatible...");
                System.exit(0);
                }
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
for(k=0;k<=2;k++)
{
mat3[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}

System.out.println("After Matrix Multiplicatin......");

for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
System.out.print(mat3[i][j]+"\t");
}
System.out.print("\n");
}

                /* transpose of matrix */
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
                        tmat[i][j]=mat3[j][i];
}
}
                System.out.println("After Transpose..");

for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
                        System.out.print(tmat[i][j]+"\t");
}
System.out.print("\n");
}

}
}




Thursday, December 15, 2011

Write a Java Program to read 5 strings and sort the strings in ascending order


// AIM :  Write a Java Program to read 5 strings and sort the strings in ascending order 


import java.io.*;


class strsort
{
       public static void main(String arg[]) throws IOException
        {

int i,j,len;
String s[]=new String[5],swp;

DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter  5 strings ?");
for(i=0;i<=4;i++)
s[i]=in.readLine();


for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
if(s[i].compareTo(s[j])>0)
{
swp=s[i];
s[i]=s[j];
s[j]=swp;
}

}
System.out.println("After sort....");

for(i=0;i<=4;i++)
  System.out.println(s[i]);


        }
}

Write a Java Program to test the given string is palindrome or not


//AIM:  Write a Java Program to test the given string is palindrome or not

import java.io.*;

class pal
{
       public static void main(String arg[]) throws IOException
        {

int i,j,len;
String s;

DataInputStream in=new DataInputStream(System.in);

System.out.print("Enter a string ?");
s=in.readLine();
len=s.length();

                        System.out.println("Len="+len);
                       
                        for(i=0,j=len-1;i<(len/2);i++,j--)
{
if(s.charAt(i)!=s.charAt(j))
break;
}

                        if(i==(len/2))
  System.out.println("Pal string");
else
  System.out.println("Not a Pal string");

        }
}

Friday, December 9, 2011

Write a java program to generate fibonacci nos up to the nth value


Aim : Write a java program to generate fibonacci nos up to the nth value

//importing io classes
import java.io.*;



class  Fibonacci
{
public static void main(String[] args) throws IOException
{
DataInputStream in=new DataInputStream(System.in);
String s;
int a=1,b=1,c=a+b;
int n;

System.out.print("Enter nth value?");
s=in.readLine();
n=Integer.parseInt(s);
                System.out.print(a+","+b+",");
while(c<=n)
{
                        System.out.print(c+",");
a=b;
b=c;
c=a+b;
}
}
}

Write a java Program to print Fibonacci up to n (10) terms.


Aim : Write a java Program to print Fibonacci up to  n (10) terms.

//importing io classes
import java.io.*;


class  Fibonacci_rec
{
public int fibo(int n)
{
if (n == 1)
return 1;
else if (n == 2)
return 1;
else
return fibo(n-1) + fibo(n-2);
}
/*
This method will do exactly that, it will invoke the two previous versions of itself,
and those invoked will invoke their previous selves and so on until one of them reaches
f(2) or f(1). It's easy to use this method in a loop to print out many Fibonacci numbers:
*/


public static void main(String[] args)
{

Fibonacci_rec f=new Fibonacci_rec();
for (int i=1; i<10; i++)
System.out.println(f.fibo(i));


}
}