Kaka
Kaka
STUDENT NAME :
REGISTER NUMBER :
CLASS :
BATCH :
MOHAMED SATHAK ENGINEERING COLLEGE
KILAKARAI – 623806
Student Name :
Register Number :
KILAKARAI – 623806.
BONAFIDE CERTIFICATE
practical CS3381 – Object Oriented Programming Laboratory during the year 2024 - 25.
Staff-in-charge H.O.D.
12 Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change.
Exhibit design and programming skills to build and automate business solutions using cutting
edge technologies.
Strong theoretical foundation leading to excellence and excitement towards research, to
provide elegant solutions to complex problems.
Ability to work effectively with various engineering fields as a team to design, build and
develop system applications. 1
CS3381 OBJECT ORIENTED PROGRAMMING LABORATORY L T P C
0 0 3 1.5
COURSE OBJECTIVES :
To build software development skills using java programming for real world applications.
-
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.
TOTAL: 45 PERIODS
COURSE OUTCOMES:
On completion of this course, the students will be able to
CO1 : Design and develop java programs using object oriented programming concepts
CO2 : Develop simple applications using object oriented concepts such as package, exceptions
CO3: Implement multithreading, and generics concepts
CO4 : Create GUIs and event driven programming applications for real world problems
CO5: Implement and deploy web applications using Java
CO’s- PO’s & PSO’s MAPPING
CO’s PO’s PSO’s
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3
1 2 1 2 1 - - - -
1 2 2 2 1 2 3
2 2 1 3 1 - - - -
2 3 3 2 1 3 1
3 2 2 1 2 1 - - -
1 2 1 3 2 3 2
4 2 2 1 3 - - - -
3 1 1 1 2 1 2
5 1 3 3 1 3 - - -
1 1 1 1 2 1 2
AVg. 2 2 2 2 2 - - -
2 2 2 2 2 2 2
1 - low, 2 - medium, 3 - high, ‘-“- no correlation
INDEX
Exp
DATE NAME OF THE PROGRAM PAGE No. SIGNATURE
No.
4
Finding the area of different shapes
9 Generic Programming
AIM:
ALGORITHM:
1. Start.
2. Input the array array[] and the target value.
3. Set i = 0 (initialize the index).
4. Check if i is less than the length of array[].
5. If array[i] == target, return index i.
6. If not, increment i by 1.
7. Repeat steps 4-6 until either the target is found or the end of the array is reached.
8. If target is not found, return -1 and En
Program:
import java.util.Scanner;
1
// Output the result of the search
if (result != -1) {
System.out.println("Target " + target + " found at index: " + result);
} else {
System.out.println("Target " + target + " not found in the array.");
}
2
Target Found at Index
Input
Output
Input
Enter the target value to search for: 12
Output
Target 12 not found in the array.
Result:
Thus the above program is executed successfully and output also verified.
3
EXNO:1 b
Solve problems by using binary search
DATE:
AIM:
Algorithms:
1. Start.
2. Input the sorted array array[] and the target value.
3. Set low = 0 and high = length of array[] - 1 (initialize search bounds).
4. While low <= high, repeat the following:
Program:
import java.util.Scanner;
Target Found
Input
Enter the target value to search for: 7
Output
Target 7 found at index: 3
5
Target Not Found
Input
Enter the target value to search for: 10
Output
Target 10 not found in the array.
Result:
Thus the above program are executed successfully and output also verified.
6
EXNO:1c
Solve problems by using quadratic sorting algorithms (SELECTION)
DATE:
AIM:
ALGORITHMS:
7
PROGRAM:
import java.util.Scanner;
import java.util.Arrays;
// Bubble Sort
arr[j + 1] = temp;
// Selection Sort
int minIndex = i;
8
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
// Insertion Sort
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
System.out.println(Arrays.toString(arr));
int n = sc.nextInt();
printArray(arr);
System.out.println("\nBubble Sort:");
bubbleSort(arr1);
printArray(arr1);
System.out.println("\nSelection Sort:");
selectionSort(arr2);
printArray(arr2);
System.out.println("\nInsertion Sort:");
insertionSort(arr3);
printArray(arr3);
sc.close();
10
}
Input
64 25 12 22 11
Output
Result:
Thus the above program are executed successfully and output also verified.
11
EXNO:1d
Solve problems by using quadratic sorting algorithms (INSERTION)
DATE:
AIM:
ALGORITHMS:
12
PROGRAM:
// Driver method
public static void main(String args[]) {
int arr[] = {12, 11, 13, 5, 6}; // Array to be sorted
13
}
}
Output
5 6 11 12 13
Result:
Thus the above program is executed successfully and out put also verified.
14
EXNO:2a
Develop stack data structure using classes and objects.
DATE:
AIM:
To write a java program for Develop stack using classes and objects.
ALGORITHMS:
15
PROGRAM:
import java.util.Stack;
class Test {
// Pushing elements on the top of the stack
static void stack_push(Stack<Integer> stack) {
for (int i = 0; i < 5; i++) {
stack.push(i);
}
}
if (pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position: " + pos);
}
Output
Pop Operation:
4
3
2
1
0
Element on stack top: 4
Element is found at position: 3
Element not found
Result:
Thus the above program are executed successfully and output also verified.
17
EXNO:2b
Develop queued at a structures using classes and object
DATE:
AIM:
To write a java program for Develop queue data structures using classes and object.
ALGORITHMS:
5. This method traverses the queue and displays the elements of the queue.
18
PROGRAM:
class Queue {
private int front, rear, capacity;
private int[] queue;
// Set the last element to 0 (optional step, but keeps the queue clean)
if (rear < capacity) {
queue[rear - 1] = 0;
}
// Decrement rear
rear--;
}
System.out.println("Initial Queue:");
// Print the queue elements
q.queueDisplay();
// Try to enqueue one more element (this should print "Queue is full")
q.queueEnqueue(50);
// Dequeue an element
q.queueDequeue();
System.out.println("\nQueue after dequeue:");
q.queueDisplay();
20
// Print the front element of the queue
q.queueFront();
}
}
Output
Initial Queue:
Queue is empty
Queue is full
Result:
Thus the above program is executed successfully and output also verified.
21
EXNO:03
GENERATING EMPLOYEE PAYROLL DETAILS
DATE:
AIM:
To develop a java application for generating pay slips of employees with their gross
and net salary.
AIGORITHM:
class Employee {
private String name;
private String id;
private String address;
private String mailId;
private String mobileNo;
24
class Professor extends Employee {
private float bPay;
private String des;
26
Output
RESULT:
Thus the application for generating pay slips of employees with their gross and
net salary has been successfully executed.
27
EXNO:04
FINDING THE AREA OF DIFFERENT SHAPES
DATE:
AIM:
To write a java program to find the area of different shapes by using abstract class.
ALGORITHM:
2. . Create an abstract class named Shape that contains two integers and an empty method
named printArea().
3. 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.
4. 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
5. 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.
7. Create object for a class in memory and assign it to the reference variable, then the
method is invoked.
28
PROGRAM:
import java.util.Scanner;
@Override
public void printArea() {
System.out.println("----- Area of Rectangle -----");
Scanner in = new Scanner(System.in);
System.out.print("Enter the Width: ");
this.a = in.nextDouble();
System.out.print("Enter the Length: ");
this.b = in.nextDouble();
this.area = a * b; // Calculate area
System.out.println("The area of the rectangle is: " + this.area);
}
}
@Override
public void printArea() {
System.out.println("----- Area of Triangle -----");
Scanner in = new Scanner(System.in);
System.out.print("Enter the Base: ");
this.a = in.nextDouble();
System.out.print("Enter the Height: ");
this.b = in.nextDouble();
this.area = 0.5 * a * b; // Calculate area
29
System.out.println("The area of the triangle is: " + this.area);
}
}
@Override
public void printArea() {
System.out.println("----- Area of Circle -----");
Scanner in = new Scanner(System.in);
System.out.print("Enter the Radius: ");
this.a = in.nextDouble();
this.area = Math.PI * a * a; // Calculate area using Math.PI
System.out.println("The area of the circle is: " + this.area);
}
}
// Rectangle
s = new Rectangle();
s.printArea();
// Triangle
s = new Triangle();
s.printArea();
// Circle
s = new Circle();
s.printArea();
}
}
30
Output
----- Finding the Area of Shapes -----
----- Area of Rectangle -----
Enter the Width: 5
Enter the Length: 10
The area of the rectangle is: 50.0
RESULT:
Thus the Implementation for finding the area of different shapes using abstract class
has been successfully executed.
31
EXNO:05
SOLVE THE ABOVE PROBLEM USING AN INTERFACE
DATE:
AIM:
To write a java program for Solve the above problem using an interface.
ALGORITHM:
32
Program:
import java.io.*;
// To change gear
@Override
public void changeGear(int newGear) {
gear = newGear;
}
// To increase speed
@Override
public void speedUp(int increment) {
speed += increment;
}
// To decrease speed
@Override
public void applyBrakes(int decrement) {
speed -= decrement;
}
// To change gear
@Override
public void changeGear(int newGear) {
gear = newGear;
}
// To increase speed
@Override
public void speedUp(int increment) {
speed += increment;
}
// To decrease speed
@Override
public void applyBrakes(int decrement) {
speed -= decrement;
}
Output
Speed: 2, Gear: 2
Speed: 1, Gear: 1
Result:
Thus the above program are executed successfully and output also verified.
35
EXNO:06 CREATING OWN EXCEPTIONS
DATE:
AIM:
ALGORITHM:
4. 4.The main ( ) method sets up an exception handler for My Exception, then calls
compute()with a legal value (lessthan10) and an illegal one to show both paths
through the code.
36
PROGRAM:
import java.io.*;
// Main method
public static void main(String[] args) {
try {
// Test compute method with different inputs
compute(1);
compute(20); // This will throw the exception
} catch (MyException e) {
System.out.println("Caught " + e); // Handle exception and print the message
37
}
}
}
Output
Called compute(1)
Normal Exit
Called compute(20)
Caught MyException[20]
RESULT:
ThustheImplementationforuserdefinedexceptionhandlinghasbeensuccessfully
executed.
38
EXNO: 07
DATE: MULTITHREADEDAPPLICATION
AIM:
Towriteaprogramthatimplementsamulti-threadedapplicationthathasthree
threads. First thread generates a random integer every 1 second and if the value is even, second
thread computes the square of the number and prints. If the value is odd, the third thread will
print the value of cube of the number.
ALGORITHM:
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.
5. If number is odd then create and start thread that computes cube of a number, Compute number * number *
number and display the answer.
7. Wait for 1 Second and Continue to Step 3 until user wants to exits.
39
PROGRAM:
public Even(int x) {
this.x = x;
}
public Odd(int x) {
this.x = x;
}
try {
for (int i = 0; i < 5; i++) {
int num = r.nextInt(100);
System.out.println("Main Thread Generates Random Integer: " + num);
if (num % 2 == 0) {
Thread t1 = new Thread(new Even(num));
t1.start();
} else {
40
Thread t2 = new Thread(new Odd(num));
t2.start();
}
Thread.sleep(1000);
}
} catch (Exception ex) {
System.out.println("Exception: " + ex.getMessage());
}
}
}
Output
For example, suppose the generated random numbers are: 34, 17, 62, 89, and 12
The output might look like this:
RESULT:
Thus the Implementation for application for multithreading has been successfully
executed.
41
EXNO:08
GETTING FILE OPERATIONS
DATE:
AIM:
To write a java program to implement file operations 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:
42
PROGRAM:
if (f.exists()) {
result = f.canRead() ? "readable." : "not readable.";
System.out.println("\nThe file is " + result);
43
Case 1: File Exists
Input : Enter the File Name: example.txt
Input
Enter the File Name: missingfile.txt
Output
The given file missingfile.txt does not exist.
Input
Enter the File Name: data.bin
Output
The given file data.bin exists.
44
RESULT:
Thus the Implementation for getting file information has been successfully executed.
45
EXNO:09
GENERIC CLASS
DATE:
AIM:
To write a java program for Develop applications to demonstrate the features of generics
classes.
ALGORITHM:
46
Program:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Constructor
public GenericsClass(T data) {
this.data = data;
}
47
Input
Enter an integer: 42
Enter a string: Hello World
Output
Generic Class returns: 42
Generic Class returns: Hello World
RESULT:
Thus the Implementation for generic class has been successfully executed.
48
EXNO:10
DEVELOP APPLICATIONS USING JAVA FX CONTROLS,
DATE: LAYOUTS AND MENUS.
AIM:
To write a java program for Develop applications using JavaFX controls ,layouts
and menus.
ALGORITHM:
49
Program:
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
@Override
public void start(Stage primaryStage) throws Exception {
// Create a BorderPane as the root layout
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 300, 200);
// Create a MenuBar
MenuBar menubar = new MenuBar();
50
// Add menus to the menu bar
menubar.getMenus().addAll(fileMenu, editMenu);
Output
Expected Output:
51
You can interact with these menus, but since no functionality has been implemented
for the menu items, they won't perform any actions yet.
52
Result:
Thus the above program are executed successfully and output also verified.
53
EXNO:11
MINI PROJECT-OPAC SYSTEM
DATE:
AIM:
To develop a mini project OPAC system for library using Java concepts.
ALGORITHM:
54
PROGRAM:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public Data() {
super("My Application");
// Initializing components
id = new JTextField(20);
name = new JTextField(20);
next = new JButton("Next BOOK");
p = new JPanel();
55
// Finalizing the frame
pack();
setVisible(true);
addWindowListener(new WIN());
}
try {
// Loading JDBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Establishing connection
conn = DriverManager.getConnection("jdbc:odbc:stu"); // DSN Name
stat = conn.createStatement();
// Executing query
res = stat.executeQuery("SELECT * FROM stu"); // Table name: stu
res.next(); // Moving to the first record
} catch (Exception e) {
System.out.println("Error: " + e);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == next) {
try {
// Move to the next record
if (res.next()) {
showRecord(res);
} else {
JOptionPane.showMessageDialog(this, "No more records.", "End of records",
JOptionPane.INFORMATION_MESSAGE);
}
} catch (SQLException ex) {
56
System.out.println("Error while fetching next record: " + ex);
}
}
}
Output
1. A window will pop up with fields for ISBN and Book Name.
2. It will initially display the first record from the database (stu table).
3. If you click the "Next BOOK" button, the program will fetch and display the next record from the database.
4. When you close the window, a thank-you message will appear.
57
RESULT:
Thus the program to develop the simple OPAC for the libraries is executed
successfully.
58