CS3381 - Object Oriented Programming Lab
Exe – 1 - Sequential Search, Binary Search, and Selection Sorting, Insertion
Sorting Algorithms
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();
}
}
2. b. Queue - data structures using classes and objects
Program:
public class Queue {
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;
Queue() {
front = -1;
rear = -1;
}
boolean isFull() {
Page 4 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}
boolean isEmpty() {
if (front == -1)
return true;
else
return false;
}
void enQueue(int element) {
if (isFull()) {
System.out.println("Queue is full");
}
else {
if (front == -1) {
front = 0;
}
rear++;
items[rear] = element;
System.out.println("Insert " + element);
}
}
int deQueue() {
int element;
if (isEmpty()) {
System.out.println("Queue is empty");
return (-1);
}
else {
element = items[front];
if (front >= rear) {
front = -1;
rear = -1;
}
else {
front++;
}
System.out.println("\n");
System.out.println("Deleted Item: " + element);
return (element);
}
}
void display() {
int i;
if (isEmpty()) {
Page 5 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
System.out.println("Empty Queue");
}
else {
System.out.println("\nFront index ----- " + front);
System.out.print("Queue Items ----- ");
for (i = front; i <= rear; i++)
System.out.print(items[i] + " ");
System.out.println("\nRear index ----- " + rear);
}
}
public static void main(String[] args) {
Queue q = new Queue();
q.deQueue();
q.enQueue(10);
q.enQueue(20);
q.enQueue(30);
q.enQueue(40);
q.enQueue(50);
q.enQueue(60);
q.display();
q.deQueue();
q.display();
q.deQueue();
q.display();
}
}
Exe – 3 - Inheritance Classes
import java.util.*;
class Employee
{
int empid;
long mobileno;
String name,address,mailid;
Scanner get=new Scanner(System.in);
void getdata()
{
System.out.println("enter the employee name:");
name=get.nextLine();
System.out.println("enter mail id:");
mailid=get.nextLine();
System.out.println("enter the employee's address:");
address=get.nextLine();
System.out.println("enter the employee id:");
empid=get.nextInt();
System.out.println("enter mobile number:");
mobileno=get.nextLong();
}
Page 6 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
void display()
{
System.out.println("employee name:"+name);
System.out.println("employee id:"+empid);
System.out.println("mail id:"+mailid);
System.out.println("Address:"+address);
System.out.println("mobile number:"+mobileno);
}
}
class Programmer extends Employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getprogrammer()
{
System.out.println("enter the basic pay:");
bp=get.nextDouble();
}
void calculateprog()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("********PAYSLIP FOR PROGRAMMER**********");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
}
class Asstprofessor extends Employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getasst()
{
System.out.println("enter the basic pay:");
bp=get.nextDouble();
}
void calculateasst()
{
da=(0.97*bp);
hra=(0.10*bp);
Page 7 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("********PAYSLIP FOR ASSISTANT
PROFESSOR**********");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
}
class Associateprofessor extends Employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getassociate()
{
System.out.println("enter the basic pay:");
bp=get.nextDouble();
}
void calculateassociate()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("********PAYSLIP FOR ASSOCIATE
PROFESSOR**********");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
}
class Professor extends Employee
{
double salary,bp,da,hra,pf,club,net,gross;
void getprofessor()
{
Page 8 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
System.out.println("enter the basic pay:");
bp=get.nextDouble();
}
void calculateprofessor()
{
da=(0.97*bp);
hra=(0.10*bp);
pf=(0.12*bp);
club=(0.1*bp);
gross=(bp+da+hra);
net=(gross-pf-club);
System.out.println("********PAYSLIP FOR PROFESSOR**********");
System.out.println("Basic Pay:Rs"+bp);
System.out.println("DA:Rs"+da);
System.out.println("HRA:Rs"+hra);
System.out.println("PF:Rs"+pf);
System.out.println("CLUB:Rs"+club);
System.out.println("GROSS PAY:Rs"+gross);
System.out.println("NET PAY:Rs"+net);
}
}
class Salary
{
public static void main(String[]args)
{
int choice,cont;
do
{
System.out.println("Payroll");
System.out.println("1.PROGRAMMER\t 2.ASSISTANT
PROFESSOR\t3.ASSOCIATE PROFESSOR\t4.PROFESSOR\t");
Scanner c=new Scanner(System.in);
choice=c.nextInt();
switch(choice)
{
case 1:
{
Programmer p=new Programmer();
p.getdata();
p.getprogrammer();
p.display();
p.calculateprog();
break;
}
case 2:
{
Asstprofessor asst=new Asstprofessor();
Page 9 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
asst.getdata();
asst.getasst();
asst.display();
asst.calculateasst();
break;
}
case 3:
{
Associateprofessor asso=new Associateprofessor();
asso.getdata();
asso.getassociate();
asso.display();
asso.calculateassociate();
break;
}
case 4:
{
Professor prof=new Professor();
prof.getdata();
prof.getprofessor();
prof.display();
prof.calculateprofessor();
break;
}
}
System.out.println("do you want to continue 0 to quit and 1 to continue");
cont=c.nextInt();
}while(cont==1);
}
}
Exe - 4 - Program to Calculate Area Using Abstract Class
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
Exe – 5 - Program to Calculate Area Using Interface
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);
System.out.println("The Area of Rectangle is: "+area_Rect);
}
}
class Tri implements Shape
{
double area_Tri,a,b;
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 implements Shape
{
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();
}
}
Exe - 6. Exception handling and creation of user defined exceptions
(a)PROGRAM
import java.util.Scanner;
class NegativeAmtException extends Exception {
String msg;
NegativeAmtException(String msg) {
this.msg=msg; }
public String toString() {
return msg; }}
public class UserException {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("Enter Amount:");
int a=s.nextInt();
try {
if(a<0) {
Page 13 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
throw new NegativeAmtException("Invalid Amount"); }
System.out.println("Amount Deposited"); }
catch(NegativeAmtException e) {
System.out.println(e); } } }
(b)PROGRAM
class MyException extends Exception{
String str1;
MyException(String str2)
{
str1=str2;
}
public String toString()
{
return ("MyException Occurred: "+str1) ;
} }
class MyException
{
public static void main(String args[])
{
try
{
System.out.println("Starting of try block");
throw new MyException("This is My error Message");
}
catch(MyException exp)
{
System.out.println("Catch Block") ;
System.out.println(exp) ;
} } }
Exe - 7 - Program to Implement Multithreaded Application
import java.util.*;
class Even implements Runnable
{
public int x;
public Even(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is EVEN and Square of " + x + " is: " + x * x);
}
}
class Odd implements Runnable
Page 14 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
{
public int x;
public Odd(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x * x * x);
}
}
class Main extends Thread
{
public void run()
{
intnum = 0;
Random r = new Random();
try
{
for (inti = 0; i < 10; i++)
{
num = r.nextInt(100);
System.out.println("Main Thread and Generated Number is " + num);
if (num % 2 == 0)
{
Thread t1 = new Thread(new Even(num));
t1.start();
}
else
{
Thread t2 = new Thread(new Odd(num));
t2.start();
}
Thread.sleep(5000);
System.out.println("--------------------------------------");
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
public class MultiThread
{
public static void main(String[] args)
{
Page 15 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
Main m = new Main();
m.start();
}
}
Exe - 8 - Program for Displaying File Information
import java.io.*;
import java.util.*;
class MyFileOper
{
public static void main(String args[])
{
String filename;
Scanner s=new Scanner(System.in);
System.out.println("Enter the File Name: ");
filename=s.nextLine();
File f1=new File(filename);
System.out.println("*****************");
System.out.println("FILE INFORMATION");
System.out.println("*****************");
System.out.println("Name of the File: "+f1.getName());
System.out.println("Path of the File: "+f1.getPath());
System.out.println("PARENT: "+f1.getParent());
if(f1.exists())
System.out.println("File Exists ");
else
System.out.println("File does not Exists ");
if(f1.canRead())
System.out.println("File can be Read");
else
System.out.println("File can not be Read");
if(f1.canWrite())
System.out.println("Write Operation is Permitted");
else
System.out.println("Write Operation is not Permitted");
if(f1.isDirectory())
System.out.println("It is a Directory ");
else
System.out.println("Not a Directory");
if(f1.isFile())
System.out.println("It is a File ");
else
System.out.println("Not a File");
System.out.println("File Last Modified "+ f1.lastModified());
System.out.println("File Length: "+f1.length());
System.out.println("File Deleted "+f1.delete());
Page 16 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab
CS3381 - Object Oriented Programming Lab
}
}
Exe – 9 - Features of Generics Classes
class MyClass<T extends Comparable<T>>
{
T[] vals;
MyClass(T[] o)
{
vals = o;
}
public T min()
{
T v = vals[0];
for(int i=1; i < vals.length; i++)
if(vals[i].compareTo(v) < 0)
v = vals[i];
return v;
}
public T max()
{
T v = vals[0];
for(int i=1; i < vals.length;i++)
if(vals[i].compareTo(v) > 0)
v = vals[i];
return v;
}
}
class Gene
{
public static void main(String args[])
{
int i;
Integer inums[]={10,2,5,4,6,1};
Character chs[]={'v','p','s','a','n','h'};
Double d[]={20.2,45.4,71.6,88.3,54.6,10.4};
MyClass<Integer> iob = new MyClass<Integer>(inums);
MyClass<Character> cob = new MyClass<Character>(chs);
MyClass<Double>dob = new MyClass<Double>(d);
System.out.println("Max value in Int: " + iob.max());
System.out.println("Min value in Int: " + iob.min());
System.out.println("Max value in Char: " + cob.max());
System.out.println("Min value in Char: " + cob.min());
System.out.println("Max value in Double: " + dob.max());
System.out.println("Min value in Double: " + dob.min());
} }
Page 17 of 17
Dr. SACOE – IT Dept CS3381 - Object Oriented Programming Lab