OOPS - All Lab Programs - Print
OOPS - All Lab Programs - Print
1. a. Sequential Search
Program:
class Linear {
public static int search(int arr[], int x)
{
int n = arr.length;
for (int i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
public static void main(String args[])
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int result = search(arr, x);
if (result == -1)
System.out.print("Element is not present in Array");
else
System.out.print("Element is Present" + " at index " + result);
}
}
1. b. Binary Search
Program:
public class Binary {
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}
else if ( arr[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}
else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
Page 1 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
}
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 40;
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}
1. c. Selection Sort
Program:
public class Selection {
public static void selectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
public static void main(String a[]){
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
selectionSort(arr1);
System.out.println("After Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
}
}
1. d. Insertion Sort
Program:
public class Insertion {
public static void insertionSort(int array[]) {
int n = array.length;
Page 2 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
}
}
public static void main(String a[]){
int[] arr1 = {9,14,3,2,43,11,58,22};
System.out.println("Before Insertion Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
insertionSort(arr1);
System.out.println("After Insertion Sort");
for(int i:arr1){
System.out.print(i+" ");
}
}
}
Exe – 2 - Stack and Queue data structures using classes and objects
a. Stack - data structures using classes and objects
Program:
public class Stack {
private int arr[];
private int top;
private int capacity;
Stack(int size) {
arr = new int[size];
capacity = size;
top = -1;
}
public void push(int x) {
if (isFull()) {
System.out.println("Stack OverFlow");
System.exit(1);
}
System.out.println("Inserting " + x);
arr[++top] = x;
}
public int pop() {
if (isEmpty()) {
Page 3 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
System.out.println("STACK EMPTY");
System.exit(1);
}
System.out.print("\nPopped Item of the Stack: "+arr[top]);
return arr[top--];
}
public int getSize() {
return top + 1;
}
public Boolean isEmpty() {
return top == -1;
}
public Boolean isFull() {
return top == capacity - 1;
}
public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(5);
stack.push(10);
stack.push(15);
stack.push(20);
stack.push(25);
System.out.print("Stack: ");
stack.printStack();
stack.pop();
System.out.print("\nAfter popping out Stack: ");
stack.printStack();
}
}
import java.util.*;
abstract class Shape
{
inta,b;
abstract public void printArea();
}
class Rect extends Shape
{
public intarea_Rect;
public void printArea()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the Length and Breadth of Rectangle: ");
Page 10 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
a=s.nextInt();
b=s.nextInt();
area_Rect=a*b;
System.out.println("Length of Rectangle: "+a +" and Breadth of Rectangle: "+b);
System.out.println("The Area of Rectangle is: "+area_Rect);
}
}
class Tri extends Shape
{
double area_Tri;
public void printArea()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the Base and Height of Triangle: ");
a=s.nextInt();
b=s.nextInt();
System.out.println("Base of Triangle: "+a +" and Height of Triangle: "+b);
area_Tri=(0.5*a*b);
System.out.println("The Area of Triangle is: "+area_Tri);
}
}
class Cir extends Shape
{
double area_Cir;
public void printArea()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the Radius of Circle: ");
a=s.nextInt();
area_Cir=(3.14*a*a);
System.out.println("Radius of Circle: "+a);
System.out.println("The Area of Circle is: "+area_Cir);
}
}
public class AbstractShape
{
public static void main(String[] args)
{
Rect r=new Rect();
System.out.println("Abstract Class Output:");
System.out.println("**********************");
r.printArea();
Tri t=new Tri();
t.printArea();
Cir r1=new Cir();
r1.printArea();
} }
Page 11 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
Program:
Shape.java
interface Shape
{
int unit1=0;
int unit2=0;
void printArea();
}
Int.java:
import java.util.*;
class Rect implements Shape
{
public int area_Rect,a,b;
public void printArea()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the Length and Breadth of Rectangle: ");
a=s.nextInt();
b=s.nextInt();
area_Rect=a*b;
System.out.println("Length of Rectangle: "+a +" and Breadth of Rectangle: "+b);
Page 12 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
double area_Cir,a;
public void printArea()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the Radius of Circle: ");
a=s.nextInt();
area_Cir=(3.14*a*a);
System.out.println("Radius of Circle: "+a);
System.out.println("The Area of Circle is: "+area_Cir);
}
}
public class Int
{
public static void main(String[] a)
{
System.out.println("Interface Output:");
System.out.println("****************");
Shape sh;
Rect r=new Rect();
sh=r;
sh.printArea();
Tri t=new Tri();
sh=t;
sh.printArea();
Cir c=new Cir();
sh=c;
sh.printArea();
}
}