Friday, June 8, 2012

Write a program to list files in the current working directory depending upon a given pattern.?


♣♣♣ WRITE A PROGRAM TO LIST FILES IN THE CURRENT WORKING DIRECTORY DEPENDING UPON A GIVEN PATTERN. ♣♣♣


  import java.lang.*;
  import java.io.*;

  class pro15
{
  public static void main(String ar[]) throws IOException
        {
                                    InputStreamReader a=new InputStreamReader(System.in);
                                    BufferedReader b=new BufferedReader(a);
                                    System.out.print("\n\tEnter the Directory Name : ");
            String s=b.readLine();
            File f=new File(s);
            if(f.exists())
            {
                        if(f.isDirectory())
                        {
                        String []f1=f.list();
                        int i;
                                    for(i=0;i
                                    {
                                    String tp=f1[i];
                                    if(tp.endsWith("java"))
                                    System.out.println("\n\t\t"+tp);
                                    if(tp.endsWith("class"))
                                    System.out.println("\n\t\t"+tp);
                                    }
                        }
             }
        }
}


OUTPUT :-
                                   
                        Enter The Directory Name : d:\

                                    Test.java
                                    Test.class
                                    Abc.java
                                    Abc.class
                                    Applet2.java

Write a program to get a file name at run time and display number of lines and words in that file.?


♣♣♣ WRITE A PROGRAM TO GET A FILE NAME AT RUN TIME AND DISPLAY NUMBER OF LINES AND WORDS IN THAT FILE. ♣♣♣


import java.lang.*;
import java.io.*;

class pro14
{
        public static void main(String ar[]) throws IOException
        {
        InputStreamReader a=new InputStreamReader(System.in);
        BufferedReader b=new BufferedReader(a);
        int lines=0,words=0;
        System.out.print("\n\tEnter The File Name : ");
        String s=b.readLine();
        File f=new File(s);
        InputStreamReader in=new InputStreamReader(new FileInputStream(f));
                        StreamTokenizer st=new StreamTokenizer(in);
                                                                        st.wordChars(0,255);
                                        st.whitespaceChars(0,' ');
                                        st.eolIsSignificant(true);
                                        while(st.nextToken()!= st.TT_EOF)
                                        {
                                                if(st.ttype==st.TT_EOL)
                                                            lines++;
                                                if(st.ttype==st.TT_WORD)
                                                    words++;
                                        }
             System.out.println("\n\tNumber of Lines : "+lines+"\n\t Number of words : "+words);

        }
}


OUTPUT :-
                                   
                        Enter The File Name : c:\test.txt
                       
                        Number of Lines : 0
                        Number of Words : 2

                        Enter The File Name : c:\hook.log
                       
                        Number of Lines : 238
                        Number of Words : 357

Write a program to copy a file to another file using java.io package classes. Get the file names at run time and if the target file existed then ask confirmation to overwrite and take necessary actions.?


♣♣♣ WRITE A PROGRAM TO COPY A FILE TO ANOTHER FILE USING JAVA.IO PACKAGE CLASSES. GET THE FILE NAMES AT RUN TIME AND IF THE TARGET FILE EXISTED THEN ASK CONFIRMATION TO OVERWRITE AND TAKE NECESSARY ACTIONS. ♣♣♣

import java.lang.*;
import java.io.*;

class pro13
{
        public static void main(String ar[]) throws IOException
        {
               InputStreamReader a =new InputStreamReader(System.in);
               BufferedReader b=new BufferedReader(a);
                int i;
                FileInputStream fin=null;
                FileOutputStream fout=null;
                String filename=null;
                try
                {
                        System.out.print("\n\tEnter the file name to copy to another file : ");
                        filename=b.readLine();
                        try
                        {
                                fin=new FileInputStream(filename);
                        }
                        catch(Exception e)
                        {
                                System.out.println("\n\tFile Not Found");
                                return;
                        }
                        try
                        {
                                System.out.print("\n\tEnter the file name to copy : ");
                                filename=b.readLine();
                                fout=new FileOutputStream(filename);
                        }
                        catch(Exception e)
                        {}
                }
                catch(Exception e)
                {}
                try
                {
                        do
                        {
                                i=fin.read();
                                if(i!=-1)
                                fout.write(i);
                        }while(i!=-1);
                       
System.out.println("\n\tFile copied to "+filename+" Sucessfully");
                }
                catch(IOException e)
                {
                        System.out.println("\n\tFile Error");
                }
                fin.close();
                fout.close();
        }
}


OUTPUT :-
                                   
                        Enter the file name to copy to another file : c:\test.txt
                       
                        Enter the file name to copy : c:\hello.txt

                        File copied to c:\hello.txt successfully

                       
                        Enter the file name to copy to another file : c:\tests.txt
                        File Not Found

Write a program to find the total memory, free memory and free memory after executing garbage collection gc(()).?


♣♣♣ WRITE A PROGRAM TO FIND THE TOTAL MEMORY, FREE MEMORY AND FREE MEMORY AFTER EXECUTING GARBAGE COLLECTION gc(()).♣♣♣

import java.io.*;

class pro12
        {
                public static void main(String ar[]) throws IOException
                {
                InputStreamReader a =new InputStreamReader(System.in);
                BufferedReader b=new BufferedReader(a);
               Runtime rn=Runtime.getRuntime();
               long mem1=0,mem2=0;
                           Integer arr[]=new Integer[1000];
                           System.out.println("\n\t\tTotal Memory is "+rn.totalMemory());
                           mem1=rn.freeMemory();
                           System.out.println("\n\tInital Free Memory is "+mem1);
                           rn.gc();
                           mem1=rn.freeMemory();
                           System.out.println("\n\tFree Memory after Garbage Collector "+mem1);
                           for(int i=0;i<1000;i++)
                           arr[i]=new Integer(i);
                           mem2=rn.freeMemory();
                           System.out.println("\n\tFree Memory after allocation "+mem2);
                           System.out.println("\n\tMemory used by alloction "+ (mem1-mem2));
                           for(int i=0;i<1000;i++)
                           arr[i]=null;
                           rn.gc();
                           mem2=rn.freeMemory();
            System.out.println("\n\tFree Memory after collecting discarded integers  "+mem2);
                }
        }

OUTPUT :-
                        Total Memory is 5177344
                        Initial Free Memory is 4949960
                        Free Memory after Garbage Collector 5039128
                        Free Memory after allocation 5020712
                        Memory used by allocation 18416
                        Free Memory after collecting discarded integer 5039128

Write a program to check all the math class functions.?


♣♣♣ WRITE A PROGRAM TO CHECK ALL THE MATH CLASS FUNCTIONS. ♣♣♣


import java.io.*;
class pro10
        {
                public static void main(String ar[]) throws IOException
                {
                       InputStreamReader a =new InputStreamReader(System.in);
                       BufferedReader b=new BufferedReader(a);
                       System.out.println("\n\n\t\t  ****** Mathematical Functions ***** \n");
                       System.out.println("\n\t\t-----------------------------------------\n");
                       System.out.println("\n\n\t\t\t<== Exponential Functions ==>\n");
                       System.out.println("\n\t\t\t-----------------------------\n");
                       System.out.println("\n\tExponential Value = "+Math.exp(3));
                       System.out.println("\n\tpower Value = "+Math.pow(2,2));
                       System.out.println("\n\n\t\t\t<== Trignometrical Functions ==>\n");
                       System.out.println("\n\t\t\t--------------------------------\n");
                       System.out.println("\n\t sin Value = "+Math.sin(30));
                       System.out.println("\n\t cos Value = "+Math.cos(30));
                       System.out.println("\n\t tan Value = "+Math.tan(30));
                       System.out.println("\n\t cot Value = "+Math.atan(30));
                       System.out.println("\n\t <== Ordinary Mathematical Functions ==>\n");
                       System.out.println("\n\t\t\t---------------------------------------\n");
                       System.out.println("\n\tAbsolute Value = "+Math.abs(-30));
                       System.out.println("\n\tRound Value = "+Math.abs(10.456));
                       System.out.println("\n\tCeil Value = "+Math.ceil(10.4));
                       System.out.println("\n\tFloor Value = "+Math.floor(10.456));
                       System.out.println("\n\tSquare Root Value = "+Math.sqrt(9));

                }
        }
OUTPUT :-
                        ****** Mathematical Functions *****
                   -------------------------------------------------------
<== Exponential Functions ==>
                        ---------------------------------------
            Exponential Value = 20.085536923187668
            Power Value  = 4.0
                        <== Trignometrical Functions ==>
                        ------------------------------------------
            Sin Value = -0.9880316240928618
            Cos Value = 0.15425144988758405
            Tan Value =  -6.405331196646276
            Cot Value = 1.5374753309166493
                        <== Ordinary Mathematical Functions ==>
                        ----------------------------------------------------
            Absolute Value = 30
Round Value = 10.456
Floor Value    = 10.0
Square Root Value = 3.0

Write a program to check all the math class functions.?


                           ♣♣♣ Write a program to check all the math class functions. ♣♣♣

import java.io.*;
import java.util.*;
import java.lang.*;
class pro9
{
            public static void main(String args[]) throws IOException
            {
                        InputStreamReader a =new InputStreamReader(System.in);
                        BufferedReader b =new BufferedReader(a);
                        Stack st=new Stack();
                        char ans='y';
                        while(ans=='y' || ans=='Y')
                        {
                        System.out.println("\n\tMENU FOR STACK OPERATION");
                        System.out.println("\n\t1.\t PUSH");
                        System.out.println("\n\t2.\t POP");
                        System.out.println("\n\t3.\t PEEK");
                        System.out.println("\n\t4.\t SEARCH");
                        System.out.println("\n\t5.\t DISPLAY");
                        System.out.println("\n\t6.\t EXIT");
                        System.out.print("\n\nEnter your choice : ");
                        int ch=Integer.parseInt(b.readLine());
                        switch(ch)
                        {
                                    case 1:
                                                System.out.print("Enter the total number of element to be push : ");
                                                int n=Integer.parseInt(b.readLine());
                                                for(int i=1;i<=n;i++)
                                                {
                                                            System.out.print("\n\tEnter the "+i+" Element : ");
                                                            int val=Integer.parseInt(b.readLine());
                                                            st.push(new Integer(val));
                                                }
                                                break;
                                    case 2:
                                                try
                                                {
                                                System.out.print("\n\tElement poped is  "+st.pop());
                                                }
                                                catch(Exception e)
                                                {
                                                System.out.println("\n\tStack is underflow........");
                                                }
                                                break;
                        case 3:
                                    try
                                    {
                            System.out.println("\n\tElement on the top of the Stack is "+(Integer)st.peek());
                                    }
                                    catch(Exception e)
                                    {
                                    System.out.print("\n\tStack is Empty");
                                    }
                                    break;
                        case 4:
                                    System.out.print("\n\tEnter the element to be searched : ");
                                    int num=Integer.parseInt(b.readLine());
                                    int sr=st.search(new Integer(num));
                                    if(sr==-1)
                                    System.out.println("\nElemet does not found in the stack");
                                    else
                                    System.out.println("\nElement found in the Stack at "+sr+"position");
                                    break;
                        case 5:
                                    System.out.println("Elemets of the stack are ");
                                    System.out.println(st);
                                    break;
                        case 6:
                                    System.exit(0);
                        default:
                                    System.out.print("Invalid Choice.......Please enter the correct choice");
                        }
                        System.out.print("Do you want to continue (Y/N) : ");
                        String s=b.readLine();
                        ans=s.charAt(0);
                        }
            }
}

OUTPUT :-

            MENU FOR STACK OPERATION
1.                                          PUSH
2.                                          POP
3.                                          PEEK
4.                                          SEARCH
5.                                          DISPLAY
6.                                          EXIT

Enter your choice : 1
Enter the total number of element to be push : 2
            Enter the 1 Element : 23
            Enter the 2 Element : 45
Do you want to continue(Y/N) : y

MENU FOR STACK OPERATION
1.                                          PUSH
2.                                          POP
3.                                          PEEK
4.                                          SEARCH
5.                                          DISPLAY
6.                                          EXIT

Enter your choice : 4
Enter the total element to be searched : 55
Element does not found in the stack
Do you want to continue(Y/N) : y



MENU FOR STACK OPERATION
1.                                          PUSH
2.                                          POP
3.                                          PEEK
4.                                          SEARCH
5.                                          DISPLAY
6.                                          EXIT

Enter your choice :5
            Elements of the stack are
            [23, 45]
Do you want to continue(Y/N) : n


Write a program to create object for TreeSet and Stack and use all methods.


♣♣♣ WRITE A PROGRAM TO CREATE OBJECT FOR TREESET AND STACK AND USE ALL METHODS. ♣♣♣


import java.io.*;
import java.util.*;
class pro8
{
            public static void main(String args[]) throws IOException
            {
                        InputStreamReader a =new InputStreamReader(System.in);
                        BufferedReader b =new BufferedReader(a);
                        TreeSet ts=new TreeSet();
                        System.out.print("\nEnter the total no. of Items : ");
                        int n=Integer.parseInt(b.readLine());
                        String str=null;
                        for(int cnt=1;cnt<=n;cnt++)
                        {
                                    System.out.print("\nEnter the "+cnt+" character : ");
                                    str=b.readLine();
                                    ts.add(str);
                        }
                        System.out.println("\n\tThe Item on the tree are : \t"+ts);
                        System.out.println("\n\tThe first item on the tree is : \t"+ts.first());
                        System.out.println("\n\tThe last item on the tree is : \t"+ts.last());
                        System.out.println("\n\tTotal no. of item on the tree are : \t"+ts.size());
                        System.out.print("\nEnter the element you want to delete : ");
                        String del=b.readLine();
                        ts.remove(del);
                        System.out.println("\n\tThe Item on the tree are : \t"+ts);
            }
}

OUTPUT :-

            Enter the total no. of Items :4
Enter the 1 character : we
Enter the 2 character : are
Enter the 3 character : LogicCafe 
Enter the 4 character : Team

The Item on the tree are :                             [are,  LogicCafe , Team, we]
The first item on the tree is :            are
The last item on the tree is : we
Total no. of item on the tree are :    4
Enter the element you want to delete :  we
The Item on the tree are : [are,  LogicCafe , Team]

Write a program to sort existing array?


♣♣♣ WRITE A PROGRAM TO SORT EXISTING ARRAY. ♣♣♣


import java.io.*;
class pro7
{
            public static void main(String args[]) throws IOException
            {
            int cnt,last,i,j,temp;
            int arr[]=new int[10];
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter the total number of array : ");
            last=Integer.parseInt(b.readLine());
                        for(cnt=0;cnt
                        {         
                        System.out.print("Enter element"+(cnt+1)+" : ");
                        arr[cnt]=Integer.parseInt(b.readLine());        
                        }
                        for(i=1;i<=last;i++)
                        {
                                    for(j=1;j<=last;j++)
                                    {
                                                if(arr[j-1]
                                                {
                                                temp=arr[j-1];
                                                arr[j-1]=arr[j];
                                                arr[j]=temp;
                                                }
                                    }
                        }
                        System.out.println("Array after sorting is :");
                        for(cnt=0;cnt
                        {
                        System.out.print(arr[cnt]+"   ");         
                        }         
            }
}

OUTPUT :-

            Enter the total number of array : 5
Enter Element1 :23
Enter Element2 :12
Enter Element3 :45
Enter Element4 :33
Enter Element5 :21

Array after sorting is :
            45        33        23        21        12

Write a program to insert element in existing array.?


♣♣♣ WRITE A PROGRAM TO INSERT ELEMENT IN EXISTING ARRAY. ♣♣♣

import java.io.*;
class pro6
{
            public static void main(String args[]) throws IOException
            {
            int cnt,last,pos,num;
            int arr[]=new int[10];
            int newarr[]=new int[10];
            InputStreamReader a=new InputStreamReader(System.in);
            BufferedReader b=new BufferedReader(a);
            System.out.print("Enter the total number of array : ");
            last=Integer.parseInt(b.readLine());
                        for(cnt=0;cnt
                        {         
                        System.out.print("Enter element"+(cnt+1)+" : ");
                        arr[cnt]=Integer.parseInt(b.readLine());        
                        }
                     System.out.print("Enter the position to insert new element in an existing array :");
                        pos=Integer.parseInt(b.readLine());
                        System.out.print("Enter the element : ");
                        num=Integer.parseInt(b.readLine());
                        for(cnt=0;cnt
                        {         
                                    if(cnt+1==pos)
                                    newarr[cnt]=num;
                                    else
                                    newarr[cnt]=arr[cnt];
                        }
                        System.out.println("Array after inserting new element is :");
                        for(cnt=0;cnt
                        {
                        System.out.print(newarr[cnt]+"   ");  
                        }         
            }
}
OUTPUT :-

            Enter the total number of array : 5
Enter Element1 :23
Enter Element2 :12
Enter Element3 :45
Enter Element4 :33
Enter Element5 :21
Enter the position to insert new element in an existing array :2
Enter the element : 88
Array after inserting new element is :
            23        88        45        33        21