0% found this document useful (0 votes)
6 views30 pages

OOP Lab (ELCE)

The document provides Java programs for various algorithms including Sequential Search, Binary Search, Selection Sort, and Insertion Sort, along with their respective algorithms and outputs. It also includes implementations of Stack and Queue data structures using classes and objects, detailing operations like Push, Pop, Enqueue, and Dequeue. Additionally, it describes an Employee class with inheritance for different employee types, calculating and displaying their pay slips based on Basic Pay and other allowances.

Uploaded by

reminas.09
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)
6 views30 pages

OOP Lab (ELCE)

The document provides Java programs for various algorithms including Sequential Search, Binary Search, Selection Sort, and Insertion Sort, along with their respective algorithms and outputs. It also includes implementations of Stack and Queue data structures using classes and objects, detailing operations like Push, Pop, Enqueue, and Dequeue. Additionally, it describes an Employee class with inheritance for different employee types, calculating and displaying their pay slips based on Basic Pay and other allowances.

Uploaded by

reminas.09
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

1.

Solve problems by using sequential search, binary search, and quadratic sorting
algorithms (selection, insertion)

1. a. Sequential Search

Aim
To write a Java program to perform Sequential Search to find a given element in an array.
Algorithm

Step 1: Start the program.

Step 2: Read array elements and the key element to be searched.

Step 3: Compare the key with each element of the array sequentially.

Step 4: If the element is found, display its position.

Step 5: If the element is not found, display “Element not found”.

Step 6: Stop the program

Program

import [Link];

public class SequentialSearch

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

int a[] = {10, 20, 30, 40, 50};

int key;

[Link]("Array Elements:");

for (int x : a)

[Link](x + " ");


[Link]("\nEnter element to search: ");

key = [Link]();

int pos = -1;

for (int i = 0; i < [Link]; i++)

if (a[i] == key)

pos = i;

break;

if (pos != -1)

[Link]("Element found at position: " + pos);

else

[Link]("Element not found");

Output
Array Elements:
10 20 30 40 50
Enter element to search: 40
Element found at position: 3

1. b. Binary Search

Aim
To write a Java program to perform Binary Search on a sorted array.

Algorithm
Step 1: Start the program.

Step 2: Read the sorted array and the key element.

Step 3: Set low = 0 and high = n–1.

Step 4: Find mid = (low + high) / 2.

Step 5: Compare the key with a[mid].

Step 6: If equal, display position.

Step 7: If key < a[mid], set high = mid – 1.

Step 8: If key > a[mid], set low = mid + 1.

Step 9: Repeat until low > high.

Step 10: Stop the program.

Program

import [Link];

public class BinarySearch

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

int a[] = {5, 10, 15, 20, 25, 30};

int key;

[Link]("Sorted Array:");

for (int x : a)

[Link](x + " ");

[Link]("\nEnter element to search: ");

key = [Link]();
int low = 0, high = [Link] - 1;

int mid;

boolean found = false;

while (low <= high)

mid = (low + high) / 2;

if (a[mid] == key)

[Link]("Element found at position: " + mid);

found = true;

break;

else if (key < a[mid])

high = mid - 1;

else

low = mid + 1;

if (!found)

[Link]("Element not found");

}
Output
Sorted Array:
5 10 15 20 25 30
Enter element to search: 20
Element found at position: 3

1. c. Selection Sort

Aim
To write a Java program to sort elements using Selection Sort technique.

Algorithm

 Start the program.

 Select the smallest element from the array.

 Swap it with the first position.

 Repeat the process for the remaining elements.

 Stop the program.

Program
public class SelectionSort {
public static void main(String[] args) {

int a[] = {64, 25, 12, 22, 11};

[Link]("Original Array: ");


for (int x : a)
[Link](x + " ");

for (int i = 0; i < [Link] - 1; i++) {


int min = i;
for (int j = i + 1; j < [Link]; j++) {
if (a[j] < a[min])
min = j;
}

int temp = a[min];


a[min] = a[i];
a[i] = temp;
}

[Link]("\nSorted Array: ");


for (int x : a)
[Link](x + " ");
}
}

Output
Original Array: 64 25 12 22 11
Sorted Array: 11 12 22 25 64

1. d. Insertion Sort

Aim
To write a Java program to sort elements using Insertion Sort technique.

Algorithm

Step 1: Start the program.

Step 2: Take the next element and insert it in the proper position in the sorted part of the
array.

Step 3: Repeat until all elements are sorted.

Step 4: Stop the program.


Program

public class InsertionSort {

public static void main(String[] args) {

int a[] = {12, 11, 13, 5, 6};

[Link]("Original Array: ");

for (int x : a)

[Link](x + " ");

for (int i = 1; i < [Link]; i++) {

int key = a[i];

int j = i - 1;

while (j >= 0 && a[j] > key) {

a[j + 1] = a[j];

j--;

a[j + 1] = key;

[Link]("\nSorted Array: ");

for (int x : a)

[Link](x + " ");


}

Output

Original Array: 12 11 13 5 6
Sorted Array: 5 6 11 12 13

Result:
. Ex. No 2 Develop stack and queue data structures using classes and objects.

2. a Stack using classes and objects

AIM
To write a Java program to implement Stack data structure using classes and objects with operations
Push and Pop.

Algorithm

Step 1: Start the program.

Step 2: Create a class Stack with data members: array, top.

Step 3: Define methods push() and pop().

Step 4: In push(), check overflow condition and insert element.

Step 5: In pop(), check underflow condition and remove element.

Step 6: Display the stack elements.

Step 7: Stop the program.

Program

class Stack

int top = -1;

int size = 5;

int stack[] = new int[size];

void push(int item)

if (top == size - 1)

[Link]("Stack Overflow");
}

else

stack[++top] = item;

[Link](item + " pushed into stack");

void pop()

if (top == -1)

[Link]("Stack Underflow");

else

[Link](stack[top--] + " popped from stack");

void display()

if (top == -1)

[Link]("Stack is empty");
}

else

[Link]("Stack elements: ");

for (int i = top; i >= 0; i--) {

[Link](stack[i] + " ");

[Link]();

public class StackDemo

public static void main(String[] args)

Stack s = new Stack();

[Link](10);

[Link](20);

[Link](30);

[Link]();

[Link]();

[Link]();

}
Output

10 pushed into stack

20 pushed into stack

30 pushed into stack

Stack elements: 30 20 10

30 popped from stack

Stack elements: 20 10

2. b. QUEUE using classes and objects

Aim

To write a Java program to implement Queue data structure using classes and objects with
operations Enqueue and Dequeue.

Algorithm

Step 1: Start the program.

Step 2: Create a class Queue with data members: array, front, rear.

Step 3: Define methods enqueue() and dequeue().

Step 4: In enqueue(), check overflow condition and insert element.

Step 5: In dequeue(), check underflow condition and delete element.

Step 6: Display the queue elements.

Step 7: Stop the program.

Program

class Queue

int size = 5;

int front = 0, rear = -1;


int queue[] = new int[size];

void enqueue(int item)

if (rear == size - 1)

[Link]("Queue Overflow");

else

queue[++rear] = item;

[Link](item + " inserted into queue");

void dequeue()

if (front > rear)

[Link]("Queue Underflow");

else

[Link](queue[front++] + " removed from queue");

}
}

void display()

if (front > rear)

[Link]("Queue is empty");

else

[Link]("Queue elements: ");

for (int i = front; i <= rear; i++) {

[Link](queue[i] + " ");

[Link]();

public class QueueDemo

public static void main(String[] args)

Queue q = new Queue();


[Link](10);

[Link](20);

[Link](30);

[Link]();

[Link]();

[Link]();

Output

10 inserted into queue

20 inserted into queue

30 inserted into queue

Queue elements: 10 20 30

10 removed from queue

Queue elements: 20 30

Result
[Link]: 3
Develop a java application with an Employee class with Emp_name, Emp_id, Address,
Mail_id, Mobile_no as members. Inherit the classes, Programmer, Assistant Professor,
Associate Professor and Professor from employee class. Add Basic Pay (BP) as the member
of all the inherited classes with 97% of BP as DA, 10 % of BP as HRA, 12% of BP as PF, 0.1%
of BP for staff club funds. Generate pay slips for the employees with their gross and net salary.

Aim

To develop a Java application using inheritance to generate pay slips for different categories of
employees (Programmer, Assistant Professor, Associate Professor, Professor) by calculating their
gross salary and net salary based on Basic Pay.

Algorithm

Step 1: Start
Step 2: Define Employee class with common data members
Step 3: Define derived classes inheriting Employee
Step 4: Input employee details and basic pay
Step 5: Calculate DA, HRA, PF, Staff Fund
Step 6: Calculate Gross Salary and Net Salary
Step 7: Display Pay Slip
Step 8: Stop

PROGRAM

import [Link];

// Base Class

class Employee

String emp_name;

int emp_id;

String address;

String mail_id;

String mobile_no;

void getDetails()
{

Scanner sc = new Scanner([Link]);

[Link]("Enter Employee Name: ");

emp_name = [Link]();

[Link]("Enter Employee ID: ");

emp_id = [Link]();

[Link]();

[Link]("Enter Address: ");

address = [Link]();

[Link]("Enter Mail ID: ");

mail_id = [Link]();

[Link]("Enter Mobile No: ");

mobile_no = [Link]();

void displayDetails()

[Link]("Employee Name : " + emp_name);

[Link]("Employee ID : " + emp_id);

[Link]("Address : " + address);

[Link]("Mail ID : " + mail_id);


[Link]("Mobile No : " + mobile_no);

// Programmer Class

class Programmer extends Employee

double bp, da, hra, pf, staffFund, gross, net;

void calculateSalary() {

da = 0.97 * bp;

hra = 0.10 * bp;

pf = 0.12 * bp;

staffFund = 0.001 * bp;

gross = bp + da + hra;

net = gross - (pf + staffFund);

void displayPaySlip()

[Link]("\n--- Programmer Pay Slip ---");

displayDetails();

[Link]("Basic Pay : " + bp);

[Link]("DA : " + da);

[Link]("HRA : " + hra);


[Link]("PF : " + pf);

[Link]("Staff Fund : " + staffFund);

[Link]("Gross Salary : " + gross);

[Link]("Net Salary : " + net);

// Assistant Professor

class AssistantProfessor extends Employee

double bp, da, hra, pf, staffFund, gross, net;

void calculateSalary()

da = 0.97 * bp;

hra = 0.10 * bp;

pf = 0.12 * bp;

staffFund = 0.001 * bp;

gross = bp + da + hra;

net = gross - (pf + staffFund);

void displayPaySlip()

[Link]("\n--- Assistant Professor Pay Slip ---");


displayDetails();

[Link]("Basic Pay : " + bp);

[Link]("Gross Salary : " + gross);

[Link]("Net Salary : " + net);

// Associate Professor

class AssociateProfessor extends Employee

double bp, da, hra, pf, staffFund, gross, net;

void calculateSalary()

da = 0.97 * bp;

hra = 0.10 * bp;

pf = 0.12 * bp;

staffFund = 0.001 * bp;

gross = bp + da + hra;

net = gross - (pf + staffFund);

void displayPaySlip()

[Link]("\n--- Associate Professor Pay Slip ---");


displayDetails();

[Link]("Basic Pay : " + bp);

[Link]("Gross Salary : " + gross);

[Link]("Net Salary : " + net);

// Professor Class

class Professor extends Employee

double bp, da, hra, pf, staffFund, gross, net;

void calculateSalary()

da = 0.97 * bp;

hra = 0.10 * bp;

pf = 0.12 * bp;

staffFund = 0.001 * bp;

gross = bp + da + hra;

net = gross - (pf + staffFund);

void displayPaySlip()

[Link]("\n--- Professor Pay Slip ---");


displayDetails();

[Link]("Basic Pay : " + bp);

[Link]("Gross Salary : " + gross);

[Link]("Net Salary : " + net);

// Main Class

public class EmployeeSalary

public static void main(String[] args)

Scanner sc = new Scanner([Link]);

Programmer p = new Programmer();

[Link]();

[Link]("Enter Basic Pay: ");

[Link] = [Link]();

[Link]();

[Link]();

AssistantProfessor ap = new AssistantProfessor();

[Link]();

[Link]("Enter Basic Pay: ");

[Link] = [Link]();

[Link]();
[Link]();

AssociateProfessor asp = new AssociateProfessor();

[Link]();

[Link]("Enter Basic Pay: ");

[Link] = [Link]();

[Link]();

[Link]();

Professor prof = new Professor();

[Link]();

[Link]("Enter Basic Pay: ");

[Link] = [Link]();

[Link]();

[Link]();

OUTPUT

----- MENU -----

1. Programmer

2. Assistant Professor

3. Associate Professor

4. Professor

Enter your choice: 2

Enter Employee Name: Anitha


Enter Employee ID: 202

Enter Address: Trichy

Enter Mail ID: anitha@[Link]

Enter Mobile No: 9876543210

Enter Basic Pay: 40000

--------- Assistant Professor Pay Slip ---------

Employee Name : Anitha

Employee ID : 202

Address : Trichy

Mail ID : anitha@[Link]

Mobile No : 9876543210

Basic Pay : 40000.0

DA (97%) : 38800.0

HRA (10%) : 4000.0

PF (12%) : 4800.0

Staff Fund : 40.0

Gross Salary : 82800.0

Net Salary : 77960.0

Result

Thus, a Java application using inheritance was developed successfully to generate


employee pay slips and calculate gross and net salary.
Ex No : 4

Write a Java Program to create an abstract class named Shape that contains two integers and
an empty method named printArea(). Provide three classes named Rectangle, Triangle and
Circle such that each one of the classes extends the class Shape. Each one of the classes contains
only the method printArea( ) that prints the area of the given shape

AIM

To write a Java program to create an abstract class named Shape with two integers and an
abstract method printArea(), and to find the area of Rectangle, Triangle, and Circle by
implementing the method in derived classes.

Algorithm

Step 1: Start the program.

Step 2: Create an abstract class Shape with two integer variables and an abstract method
printArea().

Step 3: Create three classes Rectangle, Triangle, and Circle that extend the Shape class.

Step 4: Override the method printArea() in each class to compute the respective area.

Step 5: Create objects of Rectangle, Triangle, and Circle in the main class.

Step 6: Call the printArea() method using the respective objects.

Step 7: Stop the program.

Program

// Abstract class
abstract class Shape {
int a, b;

// Abstract method
abstract void printArea();
}

// Rectangle class
class Rectangle extends Shape {

Rectangle(int length, int breadth) {


a = length;
b = breadth;
}

void printArea() {
int area = a * b;
[Link]("Area of Rectangle = " + area);
}
}

// Triangle class
class Triangle extends Shape {

Triangle(int base, int height) {


a = base;
b = height;
}

void printArea() {
double area = 0.5 * a * b;
[Link]("Area of Triangle = " + area);
}
}

// Circle class
class Circle extends Shape {

Circle(int radius) {
a = radius;
}

void printArea() {
double area = 3.14 * a * a;
[Link]("Area of Circle = " + area);
}
}

// Main class
public class ShapeArea {
public static void main(String[] args) {

Rectangle r = new Rectangle(10, 5);


Triangle t = new Triangle(6, 4);
Circle c = new Circle(7);

[Link]();
[Link]();
[Link]();
}
}
Output

Area of Rectangle = 50
Area of Triangle = 12.0
Area of Circle = 153.86

Result

Thus, a Java program using an abstract class Shape and method overriding was successfully
implemented to calculate the areas of Rectangle, Triangle, and Circle.
[Link] : 5

AIM

To write a Java program using an interface Shape and calculate the area of Rectangle, Triangle,
and Circle.

ALGORITHM

Step 1: Start the program.

Step 2: Create an interface Shape with method printArea().

Step 3: Create classes Rectangle, Triangle, and Circle that implement the interface.

Step 4: Define printArea() in each class.

Step 5: Create objects and call printArea().

Step 6: Stop.

PROGRAM

interface Shape {

void printArea();

class Rectangle implements Shape {

int l = 10, b = 5;

public void printArea() {

[Link]("Area of Rectangle = " + (l * b));

class Triangle implements Shape {


int base = 6, height = 4;

public void printArea() {

[Link]("Area of Triangle = " + (0.5 * base * height));

class Circle implements Shape {

int r = 7;

public void printArea() {

[Link]("Area of Circle = " + (3.14 * r * r));

public class InterfaceShapeDemo {

public static void main(String[] args) {

Rectangle r = new Rectangle();

Triangle t = new Triangle();

Circle c = new Circle();

[Link]();

[Link]();

[Link]();

}
}

OUTPUT

Area of Rectangle = 50

Area of Triangle = 12.0

Area of Circle = 153.86

RESULT

Thus, the program using an interface was successfully executed to calculate the area of
Rectangle, Triangle, and Circle.

You might also like