Page 666 - Computer science 868 Class 12
P. 666

Methods/Member functions
                   Amicable (int xx,int yy)                 :    parameterised constructor to initialise the data members y and y to xx
                                                               and yy
                   int sumDivisor(int n,int d)              :    returns the sum of the factors ‘d’ of number ‘n’ (excluding itself) using
                                                               recursive technique
                     void isAmicable()                      :    checks whether the given numbers are Amicable  numbers or not  by
                                                               invoking the function  sumDivisor(int,int)  and displays the result with an
                                                               appropriate message
                   Specify the class Amicable giving details of the constructor( ), int sumDivisor(int n,int d) and void isAmicable(). Define a main()
                   function to create an object and call the functions accordingly to enable the task.
               Ans.  class Amicable
                   { int x,y;
                     Amicable(int xx,int yy)//constructor
                     { x=xx;
                       y=yy;
                     }
                     int sumDivisor(int n,int d)
                     {
                       if(d==n) // base case
                         return 0;
                       else if(n%d==0) // if factor
                         return d+sumDivisor(n,d+1);
                       else
                          return sumDivisor(n,d+1);
                       }
                       void isAmicable()
                       { if(sumDivisor(x,1)==y && sumDivisor(y,1)==x) //checking
                           System.out.println(x+","+y+" are amicable numbers");
                         else
                            System.out.println(x+","+y+" are not amicable numbers");
                           }
                       public static void main(int x1,int y1)
                       { Amicable ob=new Amicable(x1,y1);
                         ob.isAmicable();
                       }}
              Question 7.                                                                                           [10]
                   A class called Pack is defined to delete the duplicate numbers from an sorted array . The details of the class are given below:
                   Class name                               :    Pack
                   Data Members
                   arr[]                                    :   stores integer numbers
                   size                                     :   integer to store array size
                   Member Functions
                   Pack(int s)                              :   parameterised constructor to initialise size=s
                   void inputArray()                        :   enter ‘size’ numbers in the array in sorted order
                   Pack delDuplicate(Pack P)                :    deletes  the  duplicate  elements from array  object P  and returns the
                                                               resulting array object
                    void dispArray()                        :   displays the array
                     Specify the class Pack giving details of the constructors, void inputArray(),Pack delDuplicate(Pack P)  and void dispArray().). Define
                   a main() function to create an object and call the other functions accordingly to enable the task.
               Ans.  import java.util.*;
                   class Pack
                   { int arr[],size; //member data
                     Pack(int s)//constructor
                     { size=s;
                       arr=new int[size];
                     }


                664664  Touchpad Computer Science-XII
   661   662   663   664   665   666   667   668   669   670   671