[go: up one dir, main page]

0% found this document useful (0 votes)
2 views3 pages

Exercise 2

The document contains three Java programming exercises: implementing a binary search algorithm, a bubble sort algorithm, and using StringBuffer for string manipulation. Each section includes the aim, code implementation, and sample output for the respective programs. The exercises demonstrate fundamental programming concepts in Java, such as searching, sorting, and string handling.

Uploaded by

buppalaiah.svist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Exercise 2

The document contains three Java programming exercises: implementing a binary search algorithm, a bubble sort algorithm, and using StringBuffer for string manipulation. Each section includes the aim, code implementation, and sample output for the respective programs. The exercises demonstrate fundamental programming concepts in Java, such as searching, sorting, and string handling.

Uploaded by

buppalaiah.svist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

l O M oA R c P S D | 3 6 5 7 7 5 2 3

EXERCISE - 2
a) Implementation of Binary search mechanism
AIM: To write a JAVA program to search for an element in a given list of elements using
binary search mechanism

import java.util.Scanner;
class binarysearchdemo
{
public static void main(String args[])
{
int n, i, num,first, last, middle;
int a[ ]=new int[20];
Scanner s = new Scanner(System.in);
System.out.println("Enter total number of elements:");
n = s.nextInt();
System.out.println("Enter elements in sorted order:");
for (i = 0; i < n; i++)
a[i] = s.nextInt();
System.out.println("Enter the search value:");
num = s.nextInt();
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( a[middle] < num )
first = middle + 1;
else if ( a[middle] == num )
{
System.out.println("number found");
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if ( first > last )
System.out.println( " Number is not found");
}
}

OOP Through JAVA LAB KKVPRASAD@CSE


l O M oA R c P S D | 3 6 5 7 7 5 2 3

OUTPUT :

Enter total number of elements:


5
Enter elements:
24689
Enter the search value:
8
number found

b) Bubble sort
AIM: To write a JAVA program to sort for an element in a given list of elements using
bubble sort

import java.util.Scanner;
class bubbledemo
{
public static void main(String args[])
{
int n, i,j, temp;
int a[ ]=new int[20];
Scanner s = new Scanner(System.in);
System.out.println("Enter total number of elements:");
n = s.nextInt();
System.out.println("Enter elements:");
for (i = 0; i < n; i++)
a[i] = s.nextInt();
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("The sorted elements are:");
for(i=0;i<n;i++)
System.out.print("\t"+a[i]);
}
}

OOP Through JAVA LAB KKVPRASAD@CSE


l O M oA R c P S D | 3 6 5 7 7 5 2 3

OUT-PUT:

Enter total number of elements:


10
Enter elements:
3257689140
The sorted elements are:
0123456789

c) Implementing StringBuffer
AIM: To write a JAVA program using StringBuffer to delete, remove character

class stringbufferdemo
{
public static void main(String[] args)
{
StringBuffer sb1 = new StringBuffer("Hello World");
sb1.delete(0,6);
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer("Some Content");
System.out.println(sb2);
sb2.delete(0, sb2.length());
System.out.println(sb2);
StringBuffer sb3 = new StringBuffer("Hello World");
sb3.deleteCharAt(0);
System.out.println(sb3);
}
}
OUT-PUT:
World
Some Content
Hello World

OOP Through JAVA LAB KKVPRASAD@CSE

You might also like