Friday, 25 October 2013

Binary search using recursion

class binsearchprog
{
    public void main()
    {
        int ar[]={12,23,32,34,45};
        int i,lb=0,ub=4,ser;
        ser=12;
        int f=binsearch(ar,ser,lb,ub);
        if(f == 1)
            System.out.println("Ele found....");
        else
            System.out.println("Ele not found...");
    }

    int binsearch(int ar[],int ser,int lb,int ub)
    {
        int mb=(lb+ub)/2,i=0;
        if(lb>ub)
            return 0;
        if(ser>ar[mb])
        {
            lb=mb+1;
            return binsearch(ar,ser,lb,ub);
        }
        else if(ser<ar[mb])
        {
            ub=mb-1;
            return binsearch(ar,ser,lb,ub);
        }
        else
        return 1;
    }
}

Thursday, 24 October 2013

Login and Logout time

The manager of a company wants to analyse the machine usage from the records to find the utilization of the machine. He wants to know how long the each user used the machine. When the user wants to use the machine he must login to the machine and after finishing the work he must log off the machine.
Each log record consists of:

User Identification number
Login time and date
Logout time and date

Time consists of
Hours
Minutes

Date consists of
Day
Month

You may assume all logins and logouts are in the same year and there are 100 users at most. The time format is 24 hour machine hours and minutes.
Design a program to find:
• To find the duration for which each user has logged.
• Output all records along with the duration. You may assume no user will login for more than 48 hours.

Test your program for the following data values and some random data values.

Sample data:
INPUT :
Number of users : 3
USER LOGIN LOGOUT
IDENTIFICATION TIME & DATE TIME & DATE

149         20:10 20-12 2:50        21-12
173        12:30   20-12 12:30 21-12
142       16:20  20-12 16:30 20-12

OUTPUT :

USER LOGIN LOGOUT DURATION
IDENTIFICATION TIME & DATE TIME & DATE HOURS:MINS
149 20:10 20-12 2:50 21-12 6:40
173 12:30 20-12 12:30 21-12 24:00
142 16:20 20-12 16:30 20-12 00:0

The user who logged in for the longest duration:-
173 12:30 20-12 12:30 21-12 24:00

Answer 3.
import java.io.*;
class Date
{
 //Data members
 public int day;
 public int month;
 //Constructor
 public Date()
 {
     day=0;
     month=0;
 }
 //Function to input date 
 public void Input()
 throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter day ::");
       day=Integer.parseInt(br.readLine());
       System.out.println("Enter month ::");
       month=Integer.parseInt(br.readLine());
    }
  //function to display date
  public void display()
  {
      System.out.print(day+"-"+month);
   }
}
import java.io.*;
class Time
{
 //Data members
 public int hour;
 public int minute;
 //Constructor
 public Time()
 {
     hour=0;
     minute=0;
 }
 //Function to input time
 public void Input()
 throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter hour ::");
       hour=Integer.parseInt(br.readLine());
       System.out.println("Enter minute ::");
       minute=Integer.parseInt(br.readLine());
    }
  //function to display time
  public void display()
  {
      System.out.print(hour+":"+minute);
   }
}
import java.io.*;
class Q3_2004
{
 //Data members
 public Date logindt;
 public Date logoutdt;
 public Time logintm;
 public Time logouttm;
 private int userid;
 //Constructor
 Q3_2004()
 {
     userid=0;
     logindt=new Date();
     logoutdt=new Date();
     logintm=new Time();
     logouttm=new Time();
  }
  //Function to input data
  public void Input()
  throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter user identification number ::");
       userid=Integer.parseInt(br.readLine());
       System.out.println("Enter login time::");
       logintm.Input();
       System.out.println("Enter login date::");
       logindt.Input();
       System.out.println("Enter logout time::");
       logouttm.Input();
       System.out.println("Enter logout date::");
       logoutdt.Input();       
    }
   //Function to find duration
   public Time Duration()
   {
       Time duration=new Time();
       if(logintm.minute > logouttm.minute)
         duration.minute=(60-logintm.minute)+logouttm.minute;
       else
         duration.minute = logouttm.minute-logintm.minute;
       duration.hour=(24-logintm.hour)+logouttm.hour;
       return duration;
    }
  //Function to display
  public void Display()
  {
      System.out.print(userid+"\t\t"+logintm.hour+":"+logintm.minute+"\t"+logindt.day+"-"+logindt.month+"\t\t"+logouttm.hour+":"+logouttm.minute+"\t"+logoutdt.day+"-"+logoutdt.month);
  }
    
  //Function to find duration
  public void findduration()
  throws IOException
  {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       int n,i,longhour=0;
       
       Q3_2004 longobj=new Q3_2004();
            
       System.out.println("Number of users :;");
       n=Integer.parseInt(br.readLine());
       Time longdur =new Time();
       for(i=1;i<=n;i++)
       {
           System.out.println("\nEnter details for user "+i);
           Q3_2004 obj=new Q3_2004();
           obj.Input();
           obj.Display();
           Time dur=new Time();
           dur=obj.Duration();
           System.out.print("\t\t");
           dur.display();
           System.out.println("\n");
           
           if(dur.hour>longhour)
           {
               longhour=dur.hour;
               longobj=obj;
               longdur=dur;
            }
        }
       
        System.out.println("\nThe user logged in for the longest duration ::");
        longobj.Display();
        System.out.print("\t\t");
        longdur.display();
    }
}
import java.io.*;
class Q3_2004main
{
  public static void main(String args[])
  throws IOException
  {
      Q3_2004 obj=new Q3_2004();
      obj.findduration();
    }
}               

Encoding

The Computer Department of the Agency of International Espionage is trying to decode intercepted messages. The agency’s spies have determined that the enemy encodes messages by first converting all characters to their ASCII values and then reversing the string.
For example consider A_z ( the underscore is just to highlight the space. The ASCII values of A, <space>, z are 65, 32 and 122 respectively. Concatenate them to get 6532122, then reverse this to get 2212356 as the coded message.
Write a program which reads a coded message and decodes it. The coded message will not exceed 200 characters. It will contain only alphabets (A..Z and a..z) and spaces. ASCII values of A…Z are 65 to 90 and those of a..z are 97 to 122.


import java.io.*;
class Q2_2004
{
    //Data Members
    public String msg;
    public String ans;
    public String rev;
    public int len;
    public String newans;
       
    //Methods
    //Default Constructor
    Q2_2004()
    {
        msg="";
        ans="";
        rev="";
        len=0;
        newans="";
    }
   
    //Function to input the encoded message
    public void Input()
    throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       
        int i,len1;
        System.out.println("Enter the encoded message");
        msg=br.readLine();
        len1=msg.length();
        for(i=(len1-1);i>=0;i--)
           rev+=msg.charAt(i);
        len=rev.length();
    }
   
    //Function to extract the digits from the reverse number and decode it
    public void Decode()
    throws IOException
    {
        Input();
        int lwrnum[]={97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122};
        char lwrcas[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        int uprnum[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90};
        char uprcas[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
       
        int i,j;
        int digit;
        int number=0;
        for(i=0;i<len;i++)
        {
            digit=Integer.parseInt(rev.substring(i,i+1));
            number=number*10+digit;
           
            if(number>=65 && number <=90)
            {
               for(j=0;j<26;j++)
               {
                   if(number==uprnum[j])
                     ans+=uprcas[j];
                }//j
                number=0;
            }//if
            else
            if(number>=97 && number <=122)
            {
               for(j=0;j<26;j++)
               {
                   if(number==lwrnum[j])
                     ans+=lwrcas[j];
                }//j
                number=0;
            }//if
            else
            if(number==32)
            {
              ans+=" ";
              number=0;
            }
         }//i
         ans=" "+ans;
         ans+=" ";
         len=ans.length();
        
         char ltr;
         for(i=0;i<len;i++)
         {
             ltr=ans.charAt(i);
             if(ltr!=' ')
               newans+=ltr;
             else
             if(ltr==' ')
             {
                 newans+=ltr;
                 ltr=ans.charAt(i+1);
                 for(j=0;j<26;j++)
                 {
                     if(ltr==lwrcas[j])
                        ltr=uprcas[j];
                 }
                 newans+=ltr;
                 i++;
             }//if
         }
    }
   
    //Function to Display the string
    public void Display()
    throws IOException
    {
        Decode();
        System.out.print("The decoded message is :: "+newans);
    }
}
                       
import java.io.*;
class Q2_2004main
{
    public void main()
    throws IOException
    {
        Q2_2004 SM=new Q2_2004();
        SM.Display();
    }
}

 Sample Output
Enter the encoded message
2312179862310199501872379231018117927

The decoded message is ::  Have A Nice Day  

File Handling

A file contains a list of names and telephone numbers in the following form:-
AJIT                                        281050
ANIL                                       462890
RISHIKESH                            524352

The names contain only one word and the names and telephone numbers are separated by
white spaces. Write a interactive program to   :-
  • Create the above file
  • Display the contents of two columns
  • Search a telephone number for a given name( Assure no duplication of name)
  • Exit the program.

The program must create the program for the first time.
Test your program for the following data:-

AJIT                                        281050
ANIL                                       462890
RISHIKESH                            524352
JAVAGAL                              345678
MONGIA                                234561
RAHUL                                   765433
ROBIN                                    543221
SACHIN                                  765433
SAURAV                                8765908
VENKATESH                         7654322

Answer 3
import java.io.*;
class Names_Telephones
{
    public static void main(String args[])
    throws IOException
    {
        String filename="Directory.Txt";
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);
        int i,n;
        String search;
       
        System.out.println("How many students");
        n=Integer.parseInt(br.readLine());
        String name,phone;
       
        FileWriter fw=new FileWriter(filename);
        BufferedWriter bw=new BufferedWriter(fw);
        PrintWriter outfile=new PrintWriter(bw);
       
        System.out.println("Enter "+n+" records one by one");
       
        //Writing Records into file
        for(i=1;i<=n;i++)
        {
            System.out.println("Name::");
            name=br.readLine();
            outfile.println(name);
           
            System.out.println("Phone Number::");
            phone=br.readLine();
            outfile.println(phone);
        }
        outfile.close();
       
        //Reading Records into file
        FileReader readfile=new FileReader(filename);
        BufferedReader infile=new BufferedReader(readfile);
       
        System.out.println("Enter name to search");
        search=br.readLine();
       
        while((name=infile.readLine())!=null)
        {
            phone=infile.readLine();
            if(name.compareToIgnoreCase(search)==0)
            {
                System.out.println("Name ::"+name+ " Phone number :: "+phone);
            }
        }
      infile.close();
    }

}

Diamond String

Write a program to print a rectangle with a diamond shape gap exactly at the center of the
Rectangle, using an input string with odd number of characters, not exceeding 20.
For Example:-
INPUT                         :-         HAPPY-HAPPY
OUTPUT:
Enter a word of odd number of characters
HAPPY-HAPPY

HAPPY-HAPPY
HAPPY HAPPY
HAPP       APPY
HAP            PPY
HA                PY
H                     Y
HA                PY
HAP            PPY
HAPP       APPY
HAPPY HAPPY
HAPPY-HAPPY

Answer 1.
import java.io.*;
class Diamond_String
{
    //Data Member  
    public String word;
    public int len;
    public int spc;
   
    //Methods
    //Default Constructor
    Diamond_String()
    {
        word="";
        len=0;
        spc=0;
    }
       
        //Function to Input String
        public void Input()
        throws IOException
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            do
            {
                System.out.println("Enter a word of odd number of characters");
                word=br.readLine();
                word=word.toUpperCase();
                len=word.length();
                if(len>=20)
                System.out.println("Word is more than 20 characters.....Try again");
                if(len%2==0)
                System.out.println("Word is of even characters.....Try again");
            }
            while((len%2)==0);
        }
         
         //Function to Display first half of String
         public void Display1()
         {
            int x,i,j,k;
            spc=1;  
            x=(len/2)-1;
            k=0;
            System.out.println(word);
            for(i=1;i<=(len/2);i++)
            {
                //Printing left hand side
                for(j=0;j<=x;j++)
                    System.out.print(word.charAt(j));
                x--;
           
                //Printing spaces
                for(j=1;j<=spc;j++)
                    System.out.print(" ");
                spc+=2;
           
                //Printing right hand side
                for(j=k;j<(len/2);j++)
                    System.out.print(word.charAt(j));
                k++;
               
                System.out.println();
            }
        }
       
        //Function to Display second half of String
        public void Display2()
        {
            int i,j,k,x;
            spc-=4;
            k=(len/2)-2;
            x=1;
           
            for(i=1;i<=((len/2)-1);i++)
            {
                //Printing left hand side
                for(j=0;j<=x;j++)
                    System.out.print(word.charAt(j));
                x++;
                    
                //Printing spaces
                for(j=spc;j>=1;j--)
                    System.out.print(" ");
                spc-=2;
           
                //Printing right hand side
                for(j=k;j<(len/2);j++)
                    System.out.print(word.charAt(j));
                k--;
                System.out.println();
            }
           
            System.out.println(word);   
               
           
        }
    }
       
  import java.io.*;
class Diamond_Example
{
    public void main()
    throws IOException
    {
        Diamond_String DS=new Diamond_String();
        DS.Input();
        DS.Display1();
        DS.Display2();
    }

}