Saturday, January 14, 2012

3b String Class and String Buffer Calss


import java.io.*;

public class stringBuffer
  {
    public static void main(String[] args) throws Exception
     {
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       String str;
       try
{
   //String

   String s=new String("Welcome");

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

   //concat
     s=s.concat(" to BDU");
                System.out.println("New String   "+ s);

                //charat
                System.out.println("Character at 2nd position: "+s.charAt(2));

                //Substring

                System.out.println("Sub String: "+ s.substring(0,5));

   //String Buffer

                StringBuffer strbuf=new StringBuffer();
       
                //append
           strbuf.append("Hello");
             strbuf.append("World");          
                System.out.println(strbuf);
   
                //insert
                strbuf.insert(5,"_Java ");        
                System.out.println(strbuf);
   
                //reverse
                strbuf.reverse();
                System.out.print("Reversed string : ");
                System.out.println(strbuf);        
                strbuf.reverse();
                System.out.println(strbuf);        
   
                //setCharAt
                strbuf.setCharAt(5,' ');
                System.out.println(strbuf);        
   
                //charAt
                System.out.print("Character at 6th position : ");
                System.out.println(strbuf.charAt(6));  

                //substring
                System.out.print("Substring from position 3 to 6 : ");
       System.out.println(strbuf.substring(3,7));
         
                //deleteCharAt
       strbuf.deleteCharAt(3);
               System.out.println(strbuf);        
   
               //capacity
        System.out.print("Capacity of StringBuffer object : ");
       System.out.println(strbuf.capacity());    
   
          }
       catch(StringIndexOutOfBoundsException e)
         {
                System.out.println(e.getMessage());
         }
     }
  }

3a Inheritance

Will come Soon ;)

2c String Tokenizer Class


import java.util.StringTokenizer;
class StringToken
  {
    static String in = "Welcome to CSBDU";
    public static void main(String args[])
     {
StringTokenizer st = new StringTokenizer(in);
while(st.hasMoreTokens())
{
String t = st.nextToken();
System.out.println(t);
}
     }
 }

4a Vector Class


// Vector Class Example

import java.util.*;
public class VectorDemo
{
  public static void main(String[] args)
  {
    Vector v = new Vector();
    int i = 10;
    Integer j = new Integer(20);
    String s = "Welcome";
    v.add(i);
    v.add(j);
    v.add(s);
    System.out.println("the elements of vector: " + v);
    System.out.println("The size of vector are: " + v.size());
    System.out.println("The elements at position 2 is: " + v.elementAt(2));
    System.out.println("The first element of vector is: " + v.firstElement());
    System.out.println("The last element of vector is: " + v.lastElement());
    v.removeElementAt(2);
    Enumeration e=v.elements();
    System.out.println("The elements of vector: " + v);
    while(e.hasMoreElements())
    {
      System.out.println("The elements are: " + e.nextElement());
    }
  }
}


2b Matrix Multiplication


import java.io.*;
import java.util.*;
public class Matrix
{
    public static void main(String a[]) throws Exception
    {
         int r;
         int c;
         int r1;
         int c1;
         int[][] matrix_A;
        int[][] matrix_B;
        int[][] matrix_ans;

        Scanner cin=new Scanner(System.in);
        System.out.println("Enter the number of rows in matrix A : ");
        r=cin.nextInt();
        System.out.println("Enter the number of columns in matrix A : ");
        c=cin.nextInt();
        System.out.println("Enter the number of rows in matrix B : ");
        r1= cin.nextInt();
        System.out.println("Enter the number of columns in matrix B : ");
        c1=cin.nextInt();
     
        if(c==r1)
        {
            matrix_A=new int[r][c];
            matrix_B=new int[r1][c1];
            matrix_ans=new int[r][c1];
            int x,y;
            x=matrix_A.length;
            y=matrix_B.length;
          System.out.println("Enter the elements of matrix A : ");
             for(int i=0;i<r;i++)
                 for(int j=0;j<c;j++)
                 {
                    matrix_A[i][j]=cin.nextInt();
                 }
          System.out.println("Enter the elements of matrix B : ");
            for(int i=0;i<r1;i++)
                 for(int j=0;j<c1;j++)
                 {
                    matrix_B[i][j]=cin.nextInt();
                 }
   for(int i = 0; i < x; i++)
    {
      for(int j = 0; j < y; j++)
       {
         for(int k = 0; k < y; k++)
           {
              matrix_ans[i][j] += matrix_A[i][k]*matrix_B[k][j];
           }
       }
    }
  for(int i = 0; i < x; i++)
    {
      for(int j = 0; j < y; j++)
       {
         System.out.print(" "+matrix_ans[i][j]);
       }
      System.out.println();
    }
             }
     
        else
        {
        System.out.println("Cant multiply");
        }
    }
}

2a Prime Nos.


import java.io.*;

class PrimeNumber
  {
    public static void main(String[] args) throws Exception
     {
int i;
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter number:");
        int num = Integer.parseInt(bf.readLine());
        System.out.println("Prime number: ");
        for (i=1; i < num; i++ )
         {
           int j;
           for (j=2; j<i; j++)
            {
        int n = i%j;
       if (n==0)
                 {
                    break;
                 }
            }
           if(i == j)
            {
        System.out.print("  "+i);
            }
        }
     }
  }  

1b Non-Recursive Fibonacci


import java.io.*;

public class FibonnaciSeries
  {

    public void generateSeries(int num)
     {

       int f1, f2 = 0, f3 = 1;

       System.out.println("fib(0) = " + f2);

       for (int i = 1; i <= num; i++)
        {
  System.out.println("fib(" + i + ") = " + f3);
  f1 = f2;
  f2 = f3;
  f3 = f1 + f2;
}
     }

    public static void main(String[] args) throws Exception
     {
        System.out.println("Fibonnaci Series");
System.out.println("Enter n value");
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int n;
n=Integer.parseInt(b.readLine());
        FibonnaciSeries fb = new FibonnaciSeries();
fb.generateSeries(n);
     }

  }