Page 431 - Computer science 868 Class 12
P. 431

void fillarry()                          :  Enter numbers in the array
                        void change(int l, int v, int nv)        :   Searches for value  'v' and  if  found, modifies it with 'nv' otherwise
                                                                    displays appropriate message using recursive technique
                        void display()                           :  displays the array
                        Define the class LSearch giving details of the constructor( ), void fillarray( ), void change( ) and void display().
                        Define class Modify giving details of the method written above. Also write the main method to create object and call other
                       methods.
                   Ans.  import java.util.*;
                       class Modify
                       {
                         int arr[], size;
                         Modify(int s) //Constructor
                         {
                           size = s;
                           arr = new int[size];
                         }
                         void fillarry() //input
                         {
                           Scanner sc = new Scanner(System.in);
                           System.out.println("Enter number");
                           for(int i=0, i<size; i++)
                           {
                              arr[i]=sc.nextInt();
                           }
                         }
                         void change(int l, int v, int nv)
                         {
                           if(l==size) //if not found base case
                              System.out.pointln(v+"not found");
                           else if(arr[l]==v) //if found base case
                           {
                              arr[l]=nv;
                              System.out.println("array modified");
                           }
                         else
                         {
                           change(l+1, v, nv);
                         } //recursive case
                       }
                       void display() //print
                       {
                         for(int i=0, i<l; i++)
                         {
                           System.out.print(arr[i]+ "");
                         }
                         }
                       Public static void main(int v int s int nva)
                       {
                           Modify ob = new Modify(s);
                           ob. display();


                                                                                                                       429
                                                                                                           Recursion   429
   426   427   428   429   430   431   432   433   434   435   436