Admission Number Java Program | ISC Computer Science 2015 Theory

A class Admission contains the admission numbers of 100 students. Some of the data members/member functions are given below:

Class name: Admission

Data member/instance variable:
adno[]: integer array to store admission numbers

Member functions/methods:
Admission(): constructor to initialize the array elements
void fillArray(): to accept the elements of the array in ascending order
int binSearch(int l, int u, int v): to search for a particular admission number (v) using binary search and recursive technique and returns 1 if found otherwise returns -1

Specify the class Admission giving details of the constructor, void fillArray() and int binSearch(int, int, int). Define the main() function to create an object and call the functions accordingly to enable the task.

import java.util.Scanner;
class Admission{
    int adno[];
    public Admission(){
        adno = new int[100];
    }
    public void fillArray(){
        Scanner in = new Scanner(System.in);
        outer:
        for(int i = 0; i < adno.length;){
            boolean valid = true;
            System.out.print("Admission number " + (i + 1) + ": ");
            int x = Integer.parseInt(in.nextLine());
            for(int j = 0; j < i; j++){
                if(x < adno[j]){
                    valid = false;
                    break;
                }
            }
            if(valid){
                adno[i] = x;
                i++;
            }
        }
    }
    public int binSearch(int l, int u, int v){
        if(l > u)
            return -1;
        int m = (l + u) / 2;
        if(v == adno[m])
            return 1;
        if(v < adno[m])
            return binSearch(l, m - 1, v);
        return binSearch(m + 1, u, v);
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        Admission obj = new Admission();
        obj.fillArray();
        System.out.print("Admission number to be searched: ");
        int key = Integer.parseInt(in.nextLine());
        int result = obj.binSearch(0, 99, key);
        if(result == 1)
            System.out.println(key + " found!");
        else
            System.out.println(key + " not found.");
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *