Page 439 - Computer science 868 Class 12
P. 439

Methods/Member functions
                        Palindrome (int nn)             :  parameterised constructor to initialise the data member num = nn and rev to 0
                        int revnum(int i)               :  returns the reverse of the number num, using a recursive technique
                        void check()                    :  checks whether the given number is palindrome or not, by invoking the function
                                                           revnum(int) and displays the result with an appropriate message
                        Specify the class Palindrome giving details of the constructor(), int revnum(int) and void check(). Define a main() function to
                       create an object and call the functions accordingly to enable the task.
                    21.  A class called Abword is defined to print any name in abbreviated form as follows: AJAY GOPAL RAMESH KRISHNA will be printed
                       as A.G.R.KRISHNA
                        Class name                      :  Abword
                        Data Members
                        String nm                       :  to store name
                        String anm                      :  to store abbreviated name
                        Member Methods
                        void read()                     :  accepts any name in upper case
                        String print(String a)          :   using recursive technique prints name in abbreviated form as shown above and
                                                           return the result
                        void show()                     :  calls print(String) and prints it in abbreviated form
                        static void main()              :  creates object and calls other methods
                    22.  Design a class Maxnum to print the largest number in the array. Some of the members of the class are given below:
                        Data Members/Instance variables
                        Class name                      :  Maxnum
                        arr[]                           :  to store integer elements in an array
                        n                               :  integer to store the size of the array
                        max                             :  to store the largest number
                        Member Functions/Methods
                        Maxnum (int nn)                 :  constructor to initialise n = nn and declare the array
                        void fillarray( )               :  accepts ‘n’ numbers in array arr[]
                        int maximum(int i)              :   using  recursive technique, returns  the largest number from the array where
                                                           parameter ‘i’ denotes index position
                        void print()                    :  calls int maximum(int) and prints the array along with its largest number
                        Define the class Maxnum giving details of the constructor( ), void fillarray( ), int maximum(int ). Also define the main( ) function
                       to create an object and call the functions accordingly to enable the task.
                   23.  A spiral matrix of order n × n can be generated by storing natural numbers from 1 to n  in clockwise manner in the vacant
                                                                                             2
                       positions of an array. The process should start from first row. Then last column is filled from top to bottom followed by last row
                       from right to left ending with first column from bottom to top. After filling the boundary elements, the same process is repeated
                       for the inner cells of the array until all the places are filled up
                        A 4 × 4 Spiral matrix is given below.
                           1        2        3        4

                          12       13        14       5
                          11       16        15       6
                          10        9        8        7
                        A class called Spiral is defined to generate a spiral matrix in recursive technique. The class description is given below:
                        Class name                      :  Spiral
                        Data Members
                        int a[][]                       :  two dimensional matrix
                        int n                           :  size of the matrix
                        void accept()                   :  accept size and define the array
                        void generate(int rowpos, int colpos,


                                                                                                                       437
                                                                                                           Recursion   437
   434   435   436   437   438   439   440   441   442   443   444