Page 384 - computer science (868) class 11
P. 384
Program 12 Design a class LSearch to search for a particular value in an array. Some of the members of
the class are given below:
Data Members/Instance variables:
arr[ ] : To store integer elements
n : Integer to store the size of the array
Member Functions/Methods
LSearch(int nn) : Constructor to initialise n=nn and declare the array
void fillarray( ) : Accepts ‘n’ numbers in array arr[]
int linear(int i, int v) : Searches for the value ‘v’ using linear search and
recursive technique and returns its location if found
otherwise returns -1
Define the class LSearch giving details of the constructor( ), void fillarray( ), and int linear(int, int).
Define the main( ) method to create an object and call the methods accordingly to enable
the task.
[Note that the Linear search is discussed in detail in the Array chapter 11 of this book. So, the
concept is not repeated here.]
1 import java.util.*;
2 class LSearch
3 {
4 int arr[];
5 int n;
6 Scanner sc=new Scanner(System.in);
7 LSearch(int nn) // constructor
8 {
9 n=nn;
10 }
11
12 void fillarray() // input
13 {
14 arr=new int[n];
15 System.out.println("Enter "+n + " elements");
16 for(int i =0;i<n;i++)
17 arr[i]=sc.nextInt();
18 }
19 int linear(int i, int v )
382382 Touchpad Computer Science-XI

