Page 584 - Computer science 868 Class 12
P. 584

Program 7      Binary Addition using Object Passing
                               Design a class binadd in which two binary numbers are taken and added using Object Passing
                               Technique. A main class is created to call the class Binary_add giving details of the constructor
                               and methods and to find total time. The following data members are taken as inputs for this
                               object passing program.
                               Data Members

                               int pos                      :   To store binary number position
                               int num                      :   To store binary number
                               int ar[]                     :   To store the digits of binary number in array
                               Member Functions
                               binadd()                     :   To initialise pos and num to 0
                               void accept()                :   Take binary number from user
                               binadd addition( binadd t2)   :    Add  and  object t2 with  current object and  return  the
                                                               result
                               void display()               :   To display the binary numbers

                 1       import java.util.*;

                 2       class binadd
                 3       {  // member data

                 4       int num,pos;
                 5       int ar[]=new int[10];

                 6       Scanner sc=new Scanner(System.in);
                 7       binadd()

                 8       {
                 9       pos=num=0;

                10       }
                11       void input()

                12       {
                13       System.out.println("ENTER BINARY NUMBER");

                14       num=sc.nextInt();
                15       }

                16       binadd add(binadd ob2)
                17       {
                18       binadd ob1=new binadd();

                19       int big,small,d1=0,d2=0,sum=0,carry=0,s=0,pos=0;

                20       if(this.num>ob2.num) // finding bigger number
                21       {
                22       big=this.num;



                582582  Touchpad Computer Science-XII
   579   580   581   582   583   584   585   586   587   588   589