A class Admission numbers of 100 students . Some of the data members / member function are given
below :
Class name : admission
Data members /Instance variable :
Adno[]: Integer array to store admission numbers
Member function /methods:
Admission():constructor to initialize the array elements
Voidfillarray():to accept the element of the array in ascending order
Intbinsearch(int I,int u ,int v): to search for a particular admission number (v), using binary search
and recursive technique and return 1 if found otherwise returns -1
Answers:
import java.util.*;
class Admission
{
int Adno[]=new int[100];
static Scanner sc = new Scanner(System.in);
Admission() // Default constructor
{
for(int i=0; i<100; i++)
{
Adno[i]=0;
}
}
void fillArray()throws Exception // Function to accept elements in ascending order
{
for(int i=0; i<100; i++)
{
System.out.print("Enter Admission no of student "+(i+1)+": ");
Adno[i] = sc.nextInt();
}
/*Sorting the array in ascending order */
int temp=0;
for(int i=0; i<99; i++)
{
for(int j=i+1; j<100; j++)
{
if(Adno[i]>Adno[j])
{
temp = Adno[i];
Adno[i] = Adno[j];
Adno[j] = temp;
}
}
}
}
int binSearch(int l, int u, int v) // Recursive function implementing binary search
{
int mid = (l + u)/2;
if(u < l) // condition if the search is unsuccessful
{
return -1;
}
if(v==Adno[mid]) // condition if the search is successful
{
return 1;
}
else if(v>Adno[mid])
{
return binSearch(mid+1,u,v);
}
else
{
return binSearch(l,mid-1,v);
}
}
public static void main(String args[])throws Exception
{
Admission ob = new Admission();
System.out.println("Enter Admission number in ascending order");
ob.fillArray();
System.out.print("Enter an Admission number to search : ");
int v = sc.nextInt();
int f = ob.binSearch(0,99,v);
System.out.println("*****************************");
if(f == 1)
{
System.out.println("Admission Number found");
}
else
{
System.out.println("Admission Number Not found");
}
}
}
0 Comments