Page 381 - computer science (868) class 11
P. 381

Program 11     Design a class BinSearch to search for a particular value in an array. Some of the members of
                                 the class are given below:                                                 [ISC 2020]
                                 Class                            :  BinSearch
                                 Data Members/Instance variables

                                 arr[ ]                           :  To store integer elements
                                 n                                :  Integer to store the size of the array
                                 Member Functions/Methods
                                 BinSearch(int nn)                :  Constructor to initialise n=nn and declare the array

                                 void fillarray( )                :  Accepts ‘n’  numbers in array arr[]
                                 void sort( )                     :  Sorts the array in ascending order
                                 int bin_search(int u, int l, int v)   :   Searches for the  value  ‘v’  using  binary  search and
                                                                    recursive technique and returns its location if found
                                                                    otherwise returns -1
                                 Define the class BinSearch giving details of the constructor( ), void fillarray( ), void sort( ) and
                                 int bin_search(int, int, int).
                                 Define the main( ) method to create an object and call the methods accordingly to enable
                                 the task.
                                 [Note that the Binary search is discussed in detail in the Array chapter 11 of this book. So, the
                                 concept is not repeated here.]

                   1      import java.util.*;W

                   2      class BinSearch {
                   3          int arr[];

                   4          int n;
                   5          Scanner sc=new Scanner(System.in);
                   6          BinSearch(int nn)  // constructor

                   7          {

                   8              n=nn;
                   9          }
                  10

                  11          void fillarray()
                  12          {

                  13              arr=new int[n];
                  14              System.out.println("Enter "+n + " elements");

                  15              for(int i =0;i<n;i++)  // accepting numbers
                  16                  arr[i]=sc.nextInt();

                  17          }
                  18



                                                                                                                       379
                                                                                                           Recursion   379
   376   377   378   379   380   381   382   383   384   385   386