Lab 12  Solutions,  ESC101, 2004-2005 Semester-II


1.

import java.io.*;
class Matrix
{
    int a[][];
    Matrix(int row ,int col)
    {
        a=new int [row][col];
         
    }
    public void input()throws IOException 
    {
        System.out.println("Enter your entries");
        System.out.println(" All rows are in ascending order");
        System.out.println(" All columns are in ascending order");
        InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
        for(int i=0;i<a.length;i++)
        {
            for(int j=0;j<a[0].length;j++)
            {
                System.out.print("Enter the entries: "); // prompt
                        String s = br.readLine();     
                a[i][j]=Integer.parseInt(s);
            }
        }
    }
    public void print()
    {     
        for(int i=0;i<a.length;i++)
        {
            for(int j=0;j<a[0].length;j++)
            {
                System.out.print(a[i][j]+"   ");
            }
            System.out.println();
        }
    }
     
    public boolean search(int num)
    {
        int pos = -1;
        for(int i=0;i<a.length;i++)
        {
             
            int beg = 0;
            int end = a.length-1;
            int mid = (int)((beg+end)/2);
            //if((a[beg]<num)&&(a[end]>num)){
                while (beg <= end)
                {
                    if(a[i][mid]>num)
                    {
                        end=mid-1;
                    }
                    if(a[i][mid]<num)
                    {
                        beg=mid+1;
                    }
                    if(a[i][mid]==num)
                    {
                        pos=mid;
                        System.out.println("Row: " + (i + 1) + "\tCol: " + (mid + 1));
                        break;
                    }
                    mid = (int)((beg+end)/2);
                }
                if(pos!=-1)
                {
                    break;
                }
        }
        if(pos==-1){ return false;}
        else       {return true;}
    }
    public static void main(String []args) throws IOException 
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
         
            System.out.print("Enter the length of row: "); // prompt
                String s = br.readLine();
            int row=Integer.parseInt(s);
        System.out.print("Enter the length of column: "); // prompt
                s = br.readLine();
            int col=Integer.parseInt(s);
        Matrix mat = new Matrix(row,col);
        mat.input();
        System.out.println("Your matrix is");
        mat.print();
        System.out.print("Enter the number to be searched: "); // prompt
                s = br.readLine();
        int num=Integer.parseInt(s);
        //int num=39;
         
        if(mat.search(num))
        {
            System.out.println("the number "+num+" is present");
        }
        else
        {
            System.out.println("the number "+num+" is absent");
        }
    }
}
 
 



2.

import java.io.*;
import java.util.*;
public class ReverseFile
{
    public void reversal(String InFile,String OutFile)throws
    IOException,FileNotFoundException
    {
        FileReader fin=new FileReader(InFile);
        BufferedReader br=new BufferedReader(fin);
        FileOutputStream ft=new FileOutputStream(OutFile);
        PrintStream pt=new PrintStream(ft);
        for(;;)
        {
            String s=br.readLine();
            if(s==null) break;
            StringTokenizer st=new StringTokenizer(s," \t\n\r\f",true);
            String rev="";
            for(;st.hasMoreTokens();)
            {
                String temp=st.nextToken();
                String tempRev="";
                for(int i=temp.length()-1;i>=0;i--)
                {
                    tempRev+=temp.charAt(i);
                }
                rev+=tempRev;
            }
            pt.println(rev);
        }
    }
    public static void main(String [] args)throws
    IOException,FileNotFoundException
    {
        ReverseFile f =new ReverseFile();
        f.reversal(args[0],args[1]);
    }
}