import java.util.Scanner;
public class BinSearch
{ public static void main(String args[])
{ int c, first, last, middle, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
System.out.println(search + "found at location " + (middle + 1) + ".");
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
System.out.println(search + " is not present in the list.\n");
}
}
Tag: 11
Linear Search / Sequential Search
to check if an element is present in the given list, we compare key element (search element) with every element in the list. If the number is found then display success otherwise the list doesn’t contain the element, that we are searching displays Search Unsuccessful.
import java.io.*;
public class LinearSearch{
public static void main(String args[]) throws IOException{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(read);
int a[]= new int[20];
int i, size, item, flag=0;
System.out.println(“Enter the size of the array:”);
size=Integer.parseInt(br.readLine());
System.out.println(“Enter the elements of the array:”);
for (i=0;i<size;i++){
a[i]=Integer.parseInt(br.readLine());
}
System.out.println(“Element to search:”);
item=Integer.parseInt(br.readLine());
for(i=0;i<size;i++){
if(item==a[i])
{
flag=1;
break;
} }
if(flag==1)
System.out.println(item+”found in position”+(i+1));
else
System.out.println(“Search Unsuccessful”);
}}
Output:
Enter the size of the array:
5
Enter the elements of the array:
45
25
41
42
56
Element to search:
41
41 found in position 3
Recursive method
A method which calls itself from its body is known as recursive method.
Eg:
public int fact( int n)
{
if(n==1)
return 1;
else
return (n * fact(n-1));
}
Design a class to overload a function compare()
Design a class Perform to overload a function as follows:
(i) void compare (int, int) – to compare two integer values and print the greater of the two integers.
(ii) void compare(char, char) – to compare the numeric value of two characters and print with the higher numeric value.
(iii)void compare(String, String) – to compare the length of the two strings and print the longer of the two.
Answer:
class Perform
{
void compare(int m, int n)
{
if(m > n)
System.out.println(“The first number is greater”+m);
else
System.out.println(“The second number is greater”+n);
}
void compare(char c, char d)
{
if(c > d)
System.out.println(“The first character is greater and the numeric value”+(int)c);
else
System.out.println(“The second character is greater and the numeric value”+(int)d);
}
void compare(String s1, String s2)
{
if(s1.compareTo(s2) > 0)
System.out.println(“The first string is longer”+s1);
else
System.out.println(“The second string is longer”+s2);
}
}
Program to check an alphabet using method
System.out.println(“Enter a character : “);
char chr=(char)br.read();
checkVowel(chr);
}
Ternary operator ?:
Question:
Rewrite by ternary operator :
if(x%2 == 0)
System.out.print(“EVEN”);
else
System.out.print(“ODD”);
Answer :
System.out.print((x%2==0 ? ”EVEN” : ”ODD”));
Analyze the given code fragment
Analyze the given program segment and answer the following questions :
(i) Write the output of the code snippet.
(ii) How many times does the body of the loop gets executed?
for(int a=5; a<=20; a+=5)
{
if(a%3 == 0)
break;
else
if(a%5 == 0)
System.out.println(a);
continue;
}
answer :
(i) 5 10
(ii) 3 times
Working :
When a = 5,
if(a%3 == 0) is false
if(a%5 == 0) is true
so it prints ‘5’
Loop executed 1 time.
When a = 10,
if(a%3 == 0) is false
if(a%5 == 0) is true
so it prints ‘10’
Loop executed 2 times.
When a = 15,
if(a%3 == 0) is true
so, break is encountered and loop terminates.
Loop executed 3 times.
ISC Computer Science Practical – Marking pattern
ISC Computer science — practical paper(30 marks)
Marking procedure for practical paper
Total Marks : 30
Algorithm : 3 marks
Program : 5 marks
Comments/Mnemonics code : 2 marks
Printed code that compiled correctly : 2 marks
Run the code and get correct output : 5 marks
Viva – 3 marks
Programs done in lab class throughout the year : 10 marks ( 5 from external examiner and 5 mark from your computer teacher)