OOPS Lab 2023-2024
OOPS Lab 2023-2024
E SIXTH SEMESTER
RECORD FOR
NAME:…….…..............................................……………………
REG NO:….……………………………….…….…………………
2023-204
Page
S.No Date Experiment Name Signature
No.
10
11
12
13
14
15
16
CS3381 Object Oriented Programming Laboratory
To build software development skills using java programming for real-world applications.
To understand and apply the concepts of classes, packages, interfaces, inheritance,
exception handling and file processing.
To develop applications using generic programming and event handling
LIST OF EXPERIMENTS:
1. Solve problems by using sequential search, binary search, and quadratic sorting algorithms
(selection, insertion).
2. Develop stack and queue data structures using classes and objects.
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.
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.
5. Solve the above problem using an interface.
6. Implement exception handling and creation of user defined exceptions.
7. Write a java program that implements a multi-threaded application that has three threads. First
thread generates a random integer every 1 second and if the value is even, the second thread
computes the square of the number and prints. If the value is odd, the third thread will print
the value of the cube of the number.
8. Write a program to perform file operations.
9. Develop applications to demonstrate the features of generics classes.
10. Develop applications using JavaFX controls, layouts and menus.
11. Develop a mini project for any application using Java concepts.
Date :
SEQUENTIAL SEARCH
AIM:
ALGORITHM:
/**
*
* @author Manikandan Ramamurthy - University College of Engineering Tindivanam
*/
class Linear {
RESULT:
Thus the Implementation of sequential search program is executed successfully.
Ex. No : 1(b)
Date :
BINARY SEARCH
AIM:
To develop a java program for Binary search using array.
ALGORITHM:
RESULT:
Thus the Implementation of Binary search program is executed successfully.
Ex. No : 1(c)
Date :
SELECTION SORTING
AIM:
ALGORITHM:
PROGRAM:
import java.util.Scanner;
/**
*
* @author Manikandan Ramamurthy - University College of Engineering Tindivanam
*/
public class SSort {
OUTPUT:
run:
Enter number of elements in the array:5
Enter 5 elements
55
44
33
22
11
elements in array
55 44 33 22 11
elements after sorting
11 22 33 44 55 BUILD SUCCESSFUL (total time: 7 seconds)
RESULT:
Date :
INSERTION SORTING
AIM:
ALGORITHM:
Sample Output:
import java.util.Scanner;
}
}
}
OUTPUT:
run:
Enter number of elements in the array:5
Enter 5 elements
8
9
1
5
6
elements in array
89156
elements after sorting
1 5 6 8 9 BUILD SUCCESSFUL (total time: 12 seconds)
RESULT:
Date :
STACK IMPLENETATION USING CLASS AND OBJECT
AIM:
To develop Java program for Stack Implementation using Class and Object.
ALGORITHM:
1. Create stack and Store elements in stack for push pop operation
2. Push the elements to the top of stack, before push element in stack should stack is not
full
3. Pop the elements from the stack should not be empty.
4. After the push and pop operation print the stack elements
5. Stop the process.
Sample Output:
Inserting 1
Inserting 2
Inserting 3
Stack: 1, 2, 3,
After popping out
1, 2,
PROGRAM:
/**
*
* @author Manikandan Ramamurthy - University College of Engineering Tindivanam
*/
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()) {
System.out.println("STACK EMPTY");
System.exit(1);
}
return arr[top--];
}
RESULT:
Thus the Implementation of Stack push, pop operation program is executed successfully.
Ex. No : 2(b)
Date :
QUEUE IMPLENETATION USING CLASS AND OBJECT
AIM:
To develop Java program for Queue Implementation using class and object.
ALGORITHM:
1. Enqueue: Adds an item from the back of the queue.
2. Dequeue: Removes an item from the front of the queue.
3. Front/Peek: Returns the value of the item in front of the queue without
dequeuing (removing) the item.
4. IsEmpty: Checks if the queue is empty.
5. IsFull: Checks if the queue is full.
6. Display: Prints all the items in the queue.
Sample Output:
Rear index-> 4
1 Deleted
Front index-> 1
Items ->
2345
Rear index-> 4
PROGRAM:
/**
*
* @author Manikandan Ramamurthy - University College of Engineering Tindivanam
*/
public class Queue {
int SIZE = 5;
int items[] = new int[SIZE];
int front, rear;
Queue() {
front = -1;
rear = -1;
}
boolean isFull() {
if (front == 0 && rear == SIZE - 1) {
return true;
}
return false;
}
boolean isEmpty() {
if (front == -1) {
return true;
} else {
return false;
}
}
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(element + " Deleted");
return (element);
}
}
void display() {
int i;
if (isEmpty()) {
System.out.println("Empty Queue");
} else {
System.out.println("\nFront index-> " + front);
System.out.println("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();
for (int i = 1; i < 6; i++) {
q.enQueue(i);
}
q.enQueue(6);
q.display();
q.deQueue();
q.display();
}
}
OUTPUT:
run:
Queue is empty
Insert 1
Insert 2
Insert 3
Insert 4
Insert 5
Queue is full
Front index-> 0
Items ->
12345
Rear index-> 4
1 Deleted
Front index-> 1
Items ->
2345
Rear index-> 4
BUILD SUCCESSFUL (total time: 0 seconds)
RESULT:
Date :
GENERATING EMPLOYEE PAYROLL DETAILS
AIM:
To develop a java application for generating pay slips of employees with their Gross and Net
salary.
ALGORITHM:
import java.util.Scanner;
class Employee {
String Emp_name;
int Emp_id;
String Address;
String Mail_id;
String Mobile_no;
System.out.print("Enter ID : ");
Emp_id = Integer.parseInt(s.nextLine());
Programmer() {
BP = 8000;
}
Gross_sal = BP + DA + HRA;
System.out.println("===================================================
==================");
}
}
class AssistantProfessor extends Employee {
AssistantProfessor() {
BP = 15600;
}
public void fnCalc() {
DA = 0.97 * BP;
HRA = 0.10 * BP;
PF = 0.12 * BP;
SCF = 0.001 * BP;
Gross_sal = BP + DA + HRA;
Net_sal = Gross_sal - PF - SCF;
System.out.println("Basic : " + BP);
System.out.println("Dearness Allowance : " + DA);
System.out.println("HRA : " + HRA);
System.out.println("PF : " + PF);
System.out.println("Staff Club Fund : " + SCF);
System.out.println("Gross Salary : " + Gross_sal);
System.out.println("Net Salary : " + Net_sal);
System.out.println("===================================================
==================");
}
}
class AssociateProfessor extends Employee {
AssociateProfessor() {
BP = 37400;
}
Gross_sal = BP + DA + HRA;
Net_sal = Gross_sal - PF - SCF;
System.out.println("===================================================
==================");
}
}
Professor() {
BP = 54000;
}
Gross_sal = BP + DA + HRA;
Net_sal = Gross_sal - PF - SCF;
System.out.println("===================================================
==================");
}
}
class Emp_PaySlip1 {
choice = s.nextInt();
switch (choice) {
case 1:
pgmr.fnRead();
pgmr.fnDisplay();
pgmr.fnCalc();
break;
case 2:
ap.fnRead();
ap.fnDisplay();
ap.fnCalc();
break;
case 3:
asp.fnRead();
asp.fnDisplay();
asp.fnCalc();
break;
case 4:
prof.fnRead();
prof.fnDisplay();
prof.fnCalc();
break;
}
OUTPUT:
1 – Programmer
2 – Assistant Professor
3 – Associate Professor
4 – Professor
5 – For Exit From Here.. :
Enter designation choice : 1
Enter name : Manikandan.R
Enter ID : 4334058
Enter Address : Tindivanam
Enter mail ID : [email protected]
Enter mobile number : 9943222676
** Employee Salary Details **
============================Employee Details :
==========================
Name : Manikandan.R
Emp_ID : 4334058
Address : Tindivanam
Mail ID : [email protected]
Mobile number : 9943222676
============================Employee Salary Details :
=======================
Basic : 8000.0
Dearness Allowance : 7760.0
HRA : 800.0
PF : 960.0
Staff Club Fund : 8.0
Gross Salary : 16560.0
Net Salary : 15592.0
============================Salary Slip with Details :
==========================
1 – Programmer
2 – Assistant Professor
3 – Associate Professor
4 – Professor
5 – For Exit From Here.. :
Enter designation choice :
RESULT:
Thus the application for generating pay slips of employees with their gross and net
salary has been successfully executed.
Ex. No : 4
Date:
FINDING THE AREA OF DIFFERENT SHAPE USING ABSTRACT CLASS
AIM:
To write a java program to find the area of different shapes by using abstract class.
ALGORITHM:
1. Import the java packages.
1. Create an abstract class named Shape that contains two integers and an empty method named
printArea().
2. Create a class Rectangle that extends the class Shape. Override the method printArea () by
getting Width and Length then compute the area and prints the area of the Rectangle.
3. Create a class Triangle that extends the class Shape. Override the method printArea () by
getting Base and Height then compute the area and prints the area of the Triangle.
4. Create a class Circle that extends the class Shape. Override the method printArea() by getting
the Radius, then compute the area and prints the area of the Circle.
5. By using Scanner class get the input during runtime.
6. Create object for a class in memory and assign it to the reference variable, then the method is
invoked.
Sample Output:
-------------------Finding the Area of Shapes :----------------------
----------------------- Area of Rectangle -----------------------
Enter the Width :
2.5
Enter the Length:
2.5
The area of the rectangle is : 6.25
------------------Area of Triangle---------------------------------------
Enter the Base :
6.6
Enter the Height :
2.2
The area of the triangle is :7.26
-----------------Area of Circle--------------------------------------------
Enter the Radius :
4.2
The area of circle is :55.3896
PROGRAM:
import java.util.*;
public int x, y;
do {
System.out.println("Menu \n 1.Area of Rectangle \n 2.Area of Traingle \n 3.Area of
Circle \n 4.Exit");
System.out.print("Enter your choice : ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter length and breadth for area of rectangle : ");
Rectangle1 r = new Rectangle1();
r.x = sc.nextInt();
r.y = sc.nextInt();
r.printArea();
break;
case 2:
System.out.println("Enter bredth and height for area of traingle : ");
Triangle t = new Triangle();
t.x = sc.nextInt();
t.y = sc.nextInt();
t.printArea();
break;
case 3:
System.out.println("Enter radius for area of circle : ");
Circle c = new Circle();
c.x = sc.nextInt();
c.printArea();
break;
default:
System.out.println("Enter correct choice");
}
} while (choice < 4);
}
}
OUTPUT:
Menu
1.Area of Rectangle
2.Area of Traingle
3.Area of Circle
4.Exit
Enter your choice : 1
Enter length and breadth for area of rectangle :
55
66
Area of Rectangle is 3630.0
Menu
1.Area of Rectangle
2.Area of Traingle
3.Area of Circle
4.Exit
Enter your choice : 2
Enter bredth and height for area of traingle :
585
55
Area of Triangle is 16087.5
Menu
1.Area of Rectangle
2.Area of Traingle
3.Area of Circle
4.Exit
Enter your choice : 3
Enter radius for area of circle :
55
Area of Circle is 9507.143
Menu
1.Area of Rectangle
2.Area of Traingle
3.Area of Circle
4.Exit
Enter your choice : 4
Enter correct choice
RESULT:
Thus the Implementation for finding the area of different shapes using abstract class
has been successfully executed
Ex. No : 5
Date:
CIRCLE, RECTANGLE, TRIANGLE AREA CALCULATION USING INTERFACE
AIM:
To develop Java program Shape Area Calculation Using Interface.
ALGORITHM:
1. Import the java packages.
2. Create an Interface named Area that contains two integers and an method named
Compute().
3. Create a class Rectangle that implements Area. then compute the area and prints the area of
the Rectangle.
4. Create a class Triangle that implements the class Area. then compute the area and prints the
area of the Triangle.
5. Create a class Circle that implements the class Area. then compute the area and prints the
area of the Circle.
6. Create object for a class in memory and assign it to the reference variable, then the method
is invoked.
Sample Output:
interface Shape {
public int x = 0, y = 0;
public int x = 0, y = 0;
public int x = 0, y = 0;
do {
System.out.println("Menu \n 1.Area of Rectangle \n 2.Area of Traingle \n 3.Area of Circle
\n 4.Exit");
System.out.print("Enter your choice : ");
choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter length and breadth for area of rectangle : ");
Rectangle1 r = new Rectangle1();
r.x = sc.nextInt();
r.y = sc.nextInt();
r.printArea();
break;
case 2:
System.out.println("Enter bredth and height for area of traingle : ");
Triangle t = new Triangle();
t.x = sc.nextInt();
t.y = sc.nextInt();
t.printArea();
break;
case 3:
System.out.println("Enter radius for area of circle : ");
Circle c = new Circle();
c.x = sc.nextInt();
c.printArea();
break;
default:
System.out.println("Enter correct choice");
}
} while (choice < 4);
}
}
OUTPUT:
Menu
1.Area of Rectangle
2.Area of Traingle
3.Area of Circle
4.Exit
Enter your choice : 1
Enter length and breadth for area of rectangle :
22
33
Area of Rectangle is 726.0
Menu
1.Area of Rectangle
2.Area of Traingle
3.Area of Circle
4.Exit
Enter your choice : 2
Enter bredth and height for area of traingle :
33
44
Area of Triangle is 726.0
Menu
1.Area of Rectangle
2.Area of Traingle
3.Area of Circle
4.Exit
Enter your choice : 3
Enter radius for area of circle :
2
Area of Circle is 12.571428
Menu
1.Area of Rectangle
2.Area of Traingle
3.Area of Circle
4.Exit
Enter your choice : 4
Enter correct choice
RESULT:
Thus the Implementation of different shape area calculated using Interface program is
executed successfully.
Ex. No : 6 USER DEFINED EXCEPTION
Date:
AIM:
To write a java program to implement user defined exception handling.
ALGORITHM:
1. Import the java packages.
1. Create a subclass of Exception named as InvalidCountryException it has only a constructor
displays the value of the exception.
2. The exception is thrown when registerUser ( ) when the country is not an India.
3. The main ( ) method sets up an exception handler for UserRegistration, then calls
registerUser ( ) with a legal value of the user’s country if the country of the user is other
then India it shows exception error message.
PROGRAM:
import java.util.Scanner;
class InvalidCountryException extends Exception {
public InvalidCountryException() {
System.out.println("InvalidCountryException occured");
System.out.println("User Outside India cannot be registered");
}
}
OUTPUT:
RESULT:
Thus the Implementation for user defined exception handling has been successfully
executed.
Ex. No : 7 MULTI THREADED APPLICATION
Date:
AIM:
To write a program that implements a multi-threaded application.
ALGORITHM:
1. Import the java packages.
2. Create a thread that generates random number, Obtain one random number and
check is odd or even.
3. If number is even then create and start thread that computes square of a number,
Compute number * number and display the answer.
4. Notify to Random number thread and goto step 7.
5. If number is odd then create and start thread that computes cube of a number,
Compute number * number * number and display the answer.
6. Notify to Random number thread and goto step 7.
7. Wait for 1 Second and Continue to Step 3 until user wants to exits.
Sample Output:
Main Thread Generates Random Integer : 19
New Thread 19 is ODD and Cube of 19 is : 6859
-----------------------------------------------------
Main Thread Generates Random Integer : 60
New Thread 60 is EVEN and Square of 60 is : 3600
-----------------------------------------------------
Main Thread Generates Random Integer : 58
New Thread 58 is EVEN and Square of 58 is : 3364
-----------------------------------------------------
Main Thread Generates Random Integer : 55
New Thread 55 is ODD and Cube of 55 is : 166375
-----------------------------------------------------
Main Thread Generates Random Integer : 57
New Thread 57 is ODD and Cube of 57 is : 185193
-----------------------------------------------------
PROGRAM:
package oopslab;
import java.util.Random;
/**
*
* @author Manikandan Ramamurthy from University college of Engineering
* Tindivanam
*/
class Square extends Thread {
int x;
Square(int n) {
x = n;
}
int x;
Cube(int n) {
x = n;
}
RESULT:
Thus the Implementation for application for multithreading has been successfully executed.
Ex. No : 8 FILE OPERATION
Date:
AIM:
To write a java program to implement file information such as reads a file name from the
user, displays information about whether the file exists, whether the file is readable, or writable, the
type of file and the length of the file in bytes.
ALGORITHM:
OUTPUT:
run:
------- File Operation------
| 1. New File Creation
| 2. File Information
| 3. Write into the File
| 4. Read from File
| 5. Delete the File
| 6. Exit From Here
|_________
Enter Your Choice = 1
File : FileOperationExample.txt is created successfully.
------- File Operation------
| 1. New File Creation
| 2. File Information
| 3. Write into the File
| 4. Read from File
| 5. Delete the File
| 6. Exit From Here
|_________
Enter Your Choice = 2
The name of the file is: FileOperationExample.txt
The absolute path of the file is: D:\\FileOperationExample.txt
Is file writeable?: true
Is file readable true
The size of the file in bytes is: 0
------- File Operation------
| 1. New File Creation
| 2. File Information
| 3. Write into the File
| 4. Read from File
| 5. Delete the File
| 6. Exit From Here
|_________
Enter Your Choice = 3
Content is successfully wrote to the file.
------- File Operation------
| 1. New File Creation
| 2. File Information
| 3. Write into the File
| 4. Read from File
| 5. Delete the File
| 6. Exit From Here
|_________
Enter Your Choice = 4
The file Contains:
A named location used to store related information is referred to as a File.
------- File Operation------
| 1. New File Creation
| 2. File Information
| 3. Write into the File
| 4. Read from File
| 5. Delete the File
| 6. Exit From Here
|_________
Enter Your Choice = 5
FileOperationExample.txt file is deleted successfully.
------- File Operation------
| 1. New File Creation
| 2. File Information
| 3. Write into the File
| 4. Read from File
| 5. Delete the File
| 6. Exit From Here
|_________
Enter Your Choice = 6
File Operation Completed
BUILD SUCCESSFUL (total time: 8 seconds)
RESULT:
Thus the Implementation for getting file information has been successfully executed.
Ex. No : 9 GENERIC CLASSES
Date:
AIM:
To write a java program to find the maximum value from the given type of elements using a
generic function.
ALGORITHM:
import java.util.*;
/**
*
* @author Manikandan Ramamurthy - University College of Engineering Tindivanam
*/
class MyGeneric {
OUTPUT:
run:
Integer Max: 89
String Max: Manikandan
Double Max: 5.6
Boolean Max: true
Byte Max: 127
BUILD SUCCESSFUL (total time: 5 seconds)
RESULT:
Thus the Implementation for finding the maximum value from the given type of elements
using a generic function has been successfully executed.
Ex. No : 10 JAVAFX CONTROLS, LAYOUTS AND MENUS
Date:
AIM:
To develop Java program for creating controls, layouts and menus using JavaFX.
ALGORITHM:
1. Open new JavaFX New Application and save file name as JavaFXMenuSample
2. Import Supporting packages into program and extends javafx application object
Application.
3. Import menu package from javafx.scene.MenuBar.
4. Create menu and cerate menu items add the menu items to menu bar.
4. Launch the application and display the output.
PROGRAM:
package com.mycompany.mavenproject2;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
BorderPane root = new BorderPane();
Scene scene = new Scene(root,200,300);
MenuBar menubar = new MenuBar();
Menu FileMenu = new Menu("File");
MenuItem filemenu1=new MenuItem("new");
MenuItem filemenu2=new MenuItem("Save");
MenuItem filemenu3=new MenuItem("Exit");
Menu EditMenu=new Menu("Edit");
MenuItem EditMenu1=new MenuItem("Cut");
MenuItem EditMenu2=new MenuItem("Copy");
MenuItem EditMenu3=new MenuItem("Paste");
EditMenu.getItems().addAll(EditMenu1,EditMenu2,EditMenu3);
root.setTop(menubar);
FileMenu.getItems().addAll(filemenu1,filemenu2,filemenu3);
menubar.getMenus().addAll(FileMenu,EditMenu);
primaryStage.setScene(scene);
primaryStage.show();
}
}
OUTPUT:
RESULT:
Thus the Implementation for JavaFX control, layout, menu program is executed
successfully.
Ex. No : 11 MINI PROJECT - OPAC SYSTEM
Date:
AIM:
To develop a mini project OPAC system for library using Java concepts.
ALGORITHM:
2. Extend the JFrame which implements action listener to the class datas.
3. Create the text field for id, name and button for next, address and the panel.
5. Assign the length and breadth value for the layout using grid layout.
8. Create the book name under the driver jdbc odbc driver in the try block.
9. Create the object for exception as e and use it for catching the error.
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Connection id: 12
PROGRAM:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*
* @author Manikandan Ramamurthy - University College of Engineering Tindivanam
*/
public class Data extends JFrame implements ActionListener {
JTextField id;
JTextField name;
JButton next;
JButton addnew;
JPanel p;
static ResultSet res;
static Connection conn;
static Statement stat;
public Data() {
super("My Application");
Container c = getContentPane();
c.setLayout(new GridLayout(5, 1));
id = new JTextField(20);
name = new JTextField(20);
next = new JButton("Next BOOK");
p = new JPanel();
c.add(new JLabel("ISBN Number", JLabel.CENTER));
c.add(id);
c.add(new JLabel("Book Name", JLabel.CENTER));
c.add(name);
c.add(p);
p.add(next);
next.addActionListener(this);
pack();
setVisible(true);
addWindowListener(new WIN());
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == next) {
try {
res.next();
} catch (SQLException e1) {
}
showRecord(res);
}
}
public void showRecord(ResultSet res) {
try {
id.setText(res.getString(2));
name.setText(res.getString(3));
} catch (Exception e) {
}
}//end of the main
//Inner class WIN implemented
OUTPUT:
RESULT:
Thus the program to develop the simple OPAC for the libraries is executed successfully.