C Programs Tutorials | IT Developer
IT Developer

Java Programs - Solved 2015 Theory Paper ISC Computer Science



Share with a Friend

Solved 2015 Theory Paper ISC Computer Science

Class 12 - ISC Computer Science Solved Theory Papers

Admission Recursive Binary Search Program - ISC 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 members/instance variables:
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.io.*; class Admission{ private int adno[]; public Admission(){ adno = new int[100]; } public void fillArray()throws IOException{ InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); System.out.println("Enter array elements:"); for(int i = 0; i < adno.length; i++) adno[i] = Integer.parseInt(br.readLine()); for(int i = 0; i < adno.length; i++){ for(int j = 0; j < adno.length - 1 - i; j++){ if(adno[j] > adno[j + 1]){ int temp = adno[j]; adno[j] = adno[j + 1]; adno[j + 1] = temp; } } } } public int binSearch(int l, int u, int v){ int mid = (l + u) / 2; if(v == adno[mid]) return 1; else if(l > u) return -1; else if(u < adno[mid]) return binSearch(l, mid - 1, v); else return binSearch(mid + 1, u, v); } public static void main(String args[]) throws IOException{ InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); Admission obj = new Admission(); obj.fillArray(); System.out.println("Enter value to search: "); int val = Integer.parseInt(br.readLine()); if(obj.binSearch(0, 99, val) == 1) System.out.println(val + " available."); else System.out.println(val + " unavailable."); } }

Output

 
OUTPUT :