Categories
Class 11 Class 12

Adhar Card Binary Search Program in Java

A class AdharCard contains the Adhar number and name of n number of persons. Some of the data members/member functions are given below:

Data Members:
adhar[]: long integer array to store the Adhar Numbers
nm[]: String array to store names of the persons.
Member Functions:
AdharCard(int): Parameterised constructor to store number of persons.
void input(): to enter Adhar Numbers and names of persons in ascending order of Adhar Number.
void display(): to display the Adhar Number and Name in the format:

Adhar No. Name
-----------   -------

boolean bsearch(int low, int high, long key): to search for a particular adhar number using the Binary Search Technique by recursive function. If found it returns true or false.

Specify the class giving details of all the member functions Adharcard(int), void input(), void display(), boolean bsearch(int, int, long).

Define the main() function to create an object and call the functions accordingly.

import java.io.*;
class AdharCard{
    long adhar[];
    String nm[];
    public AdharCard(int n){
        adhar = new long[n];
        nm = new String[n];
    }
    public void input()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        for(int i = 0; i < adhar.length; i++){
            System.out.print("Adhar Number: ");
            adhar[i] = Long.parseLong(br.readLine());
            System.out.print("Name: ");
            nm[i] = br.readLine();
        }
        for(int i = 0; i < adhar.length; i++){
            for(int j = 0; j < adhar.length - 1 - i; j++){
                if(adhar[j] > adhar[j + 1]){
                    long temp = adhar[j];
                    adhar[j] = adhar[j + 1];
                    adhar[j + 1] = temp;
                }
            }
        }
    }
    public void display(){
        System.out.println("Adhar No.\tName");
        for(int i = 0; i < adhar.length; i++)
            System.out.println(adhar[i] + "\t" + nm[i]);
    }
    public boolean bsearch(int low, int high, long key){
        int mid = (low + high) / 2;
        if(key == adhar[mid])
            return true;
        if(key < adhar[mid])
            return bsearch(low, mid - 1, key);
        if(key > adhar[mid])
            return bsearch(mid + 1, high, key);
        return false;
    }
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Number of elements: ");
        int n = Integer.parseInt(br.readLine());
        System.out.print("Element to be searched: ");
        long key = Long.parseLong(br.readLine());
        AdharCard obj = new AdharCard(n);
        obj.input();
        obj.display();
        if(obj.bsearch(0, n - 1, key))
            System.out.println("Found!");
        else
            System.out.println("Not Found!");
    }
}

4 replies on “Adhar Card Binary Search Program in Java”

Sir,a program is given which states that:
A class Palindrome contains a 2-dimensional integer array of order m x n where the maximum values for both ‘m’ & ‘n’ is 5.Design the class palindrome to fill the matrix with first (m x n) palindrome numbers starting from 11.The details of the members of the class are given below:
Class name:Palindrome
Data members/instance variables:
matrix[][]:stores the array elements
m:integer to store the number of rows
n:integer to store the number of columns
Methods:
Palindrome(int r,int c):parameterized constructor to initialize the data members m=r,n=c and p=11
void fill():fills the matrix with first (m x n) palindrome numbers starting from 11.
void display():displays the matrix,after filling it with palindrome numbers.
Include main() method to create an object of the class and call the methods accordingly to enable the task.

Sir,a program is given which states that:
A class Palindrome contains a 2-dimensional integer array of order m x n where the maximum values for both ‘m’ & ‘n’ is 5.Design the class palindrome to fill the matrix with first (m x n) palindrome numbers starting from 11.The details of the members of the class are given below:
Class name:Palindrome
Data members/instance variables:
matrix[][]:stores the array elements
m:integer to store the number of rows
n:integer to store the number of columns
Methods:
Palindrome(int r,int c):parameterized constructor to initialize the data members m=r,n=c and p=11
void fill():fills the matrix with first (m x n) palindrome numbers starting from 11.
void display():displays the matrix,after filling it with palindrome numbers.
Include main() method to create an object of the class and call the methods accordingly to enable the task.

Leave a Reply