Computer Science, asked by lavutevu, 5 months ago

A class search has been defined to find the
linear search method from an integer array
Class Name search
Data Members
arr() //int array of max size 50
limit //int array to store actual limit of array arr num // int variable to
store the number to search
Member Functions
search(int n) //constructor to assign n to limit void readarray() //to
read array arr[] up to the limit. void disparray() //to display array up
to the given limit
void inputsearchnum() //to read value of num which is to be
searched
int linsearch() //to search and return the index of the no to be searched. If
the number is found return the
index of the number otherwise return -1
void prores() //by invoking the function int linsearch() print the index of the
element to be searched,
otherwise output "SEARCH UNSUCCESSFUL"
Specify the class search giving details of all member functions. You have to
write the main function.​

Answers

Answered by CShivam828
1

Answer:

import java.util.*;

class search

{

   int arr[]=new int[50];

   int limit,num;

   Scanner s=new Scanner(System.in);

   search(int n)//constructor to assign n to limit

   {

       limit=n;

   }

   void readarray()//to read array arr[] up to the limit  

   {

       System.out.println("Enter elements of array");

       for(int i=0;i<limit;i++)

       {

           arr[i]=s.nextInt();

       }

   }

   void disparray()//to display array up to the given limit

   {

       for(int i=0;i<limit;i++)

       {

           System.out.println(arr[i]);

       }

   }

   void inputsearchnum()//to read value of num which is to be

   {

       System.out.println("Enter element to be searched");

       num=s.nextInt();

   }

   int linsearch()//to search and return the index of the no to be searched else return -1

   {

       for(int i=0;i<limit;i++)  

       {

           if(arr[i]==num)

           {

               return i;

           }

       }

       return -1;

   }

   void prnres()//if linsearch() returns -1 printing "Search Unsuccessful"

   {

       System.out.println("Search Unsuccessful");

   }

   public static void main()

   {

       Scanner sc=new Scanner(System.in);

       System.out.println("Enter limit of array");

       int l=sc.nextInt();

       search obj=new search(l);

       obj.readarray();

       obj.disparray();

       obj.inputsearchnum();

       int a=obj.linsearch();

       if(a==-1)

       {

           obj.prnres();

       }

   }

}

Explanation:

Similar questions