Lab Manual - OOP 2024-25
Lab Manual - OOP 2024-25
Lab Manual - OOP 2024-25
BACHELOR OF TECHNOLOGY
III SEMESTER
Laboratory Manual
Session:2024-25
OBJECT ORIENTED PROGRAMMING PRACTICAL BOOK
COMPUTER SCIENCE &ENGINEERING DEPARTMENT
PREFACE
It gives us immense pleasure to present the first edition of the OBJECT ORIENTED
PROGRAMMING Practical Book for the B.Tech . 3rd semester students for PARUL
UNIVERSITY.
The OOP theory and laboratory courses at PARUL UNIVERSITY, WAGHODIA,
VADODARA are designed in such a way that students develop the basic understanding of the
subject in the theory classes and then try their hands on the experiments to realize the various
implementations of problems learnt during the theoretical sessions. The main objective of the
OOP laboratory course is: Learning OOP through Experimentations. All the experiments are
designed to illustrate various problems in different areas of OOP and also to expose the students
to various uses.
The objective of this OOP Practical Book is to provide a comprehensive source for all
the experiments included in the OOP laboratory course. It explains all the aspects related to
every experiment such as: basic underlying concept and how to analyze a problem. It also gives
sufficient information on how to interpret and discuss the obtained results.
We acknowledge the authors and publishers of all the books which we have consulted
while developing this Practical book. Hopefully this OOP Practical Book will serve the purpose
for which it has been developed.
INSTRUCTIONS TO STUDENTS
1. The main objective of the OOP laboratory is: Learning through the Experimentation. All
the experiments are designed to illustrate various problems in different areas of OOP and
also to expose the students to various problems and their uses.
2. Be prompt in arriving to the laboratory and always come well prepared for the practical.
3. Every student should have his/her individual copy of the OOP Practical Book.
4. Every student have to prepare the notebooks specifically reserved for the OOP practical
work: ” OOP Practical Book”
5. Every student has to necessarily bring his/her OOP Practical Book, OOP Practical Class
Notebook and OOP Practical Final Notebook.
6. Finally find the output of the experiments along with the problem and note results in the
OOP Practical Notebook.
7. The grades for the OOP practical course work will be awarded based on our performance
in the laboratory, regularity, recording of experiments in the OOP Practical Final
Notebook, lab quiz, regular viva-voce and end-term examination.
CERTIFICATE
Mr./Ms.............................................................................................................
Head Of Department:...........................................
TABLE OF CONTENT
Output:-
Page | 1
CSE-PIET(PU)
Practical-2
2203031260035 OOP Laboratory
// Perform addition, subtraction, multiplication, division, and modulo operations on 'a' and 'b'.
int A = a + b;
System.out.println("Addition: " + A);
int S = a - b;
System.out.println("Subtraction: " + S);
int M = a * b;
System.out.println("Multiplication: " + M);
int D = a / b;
System.out.println("Division: " + D);
int R = a % b;
System.out.println("Remainder: " + R);
// Bitwise Operations
int x = 12; // 1100 in Binary
int y = 6; // 0110 in Binary
Page | 2
CSE-PIET(PU)
2203031260035 OOP Laboratory
}
}
Output:-
Page | 3
CSE-PIET(PU)
Practical-3
2203031260035 OOP Laboratory
}
// This is the main class that contains the program's entry point.
public class Arithmetic_Bitwise {
public static void main(String[] args) {
// Print a message to the console.
System.out.println("Prince_Chhodavadiya");
Page | 5
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 6
CSE-PIET(PU)
Practical-4
2203031260035 OOP Laboratory
// Prompt the user to enter the employee's name and read it as a string.
System.out.print("Enter employee name: ");
name = scanner.nextLine();
// Prompt the user to enter the employee's age and read it as an integer.
System.out.print("Enter employee age: ");
age = scanner.nextInt();
// Prompt the user to enter the employee's salary and read it as a double.
System.out.print("Enter employee salary: ");
salary = scanner.nextDouble();
}
Page | 7
CSE-PIET(PU)
2203031260035 OOP Laboratory
Page | 8
CSE-PIET(PU)
Practical-5
2203031260035 OOP Laboratory
Aim:- Write a Java program that prints all real solutions to the quadratic
equation ax2 + bx+c = 0. Read in a, b, c and use the quadratic formula. If
the discriminate b2-4ac is negative, display a message stating that
there are no real solutions?
Code:-
// Import the Scanner class from the java.util package to read user input.
import java.util.Scanner;
System.out.println("Real solutions:");
System.out.println("Root 1: " + root1);
System.out.println("Root 2: " + root2);
} else if (discriminant == 0) {
// Calculate and display a real solution if the discriminant is zero.
double root = -b / (2 * a);
System.out.println("Real solution:");
System.out.println("Root: " + root);
} else {
// Display a message indicating no real solutions if the discriminant is negative.
System.out.println("No real solutions.");
}
Page | 9
CSE-PIET(PU)
2203031260035 OOP Laboratory
}
}
Output:-
Page | 10
CSE-PIET(PU)
Practical-6
2203031260035 OOP Laboratory
Aim:- The Fibonacci sequence is defined by the following rule. The first
2 values in the sequence are 1, 1. Every subsequent value is the sum of
the 2 values preceding it. Write a Java program that uses both recursive
and non- recursive functions to print the nth value of the Fibonacci
sequence?
Code:-
// Import the Scanner class from the java.util package to read user input.
import java.util.Scanner;
return fib;
}
Page | 11
CSE-PIET(PU)
2203031260035 OOP Laboratory
// Calculate the nth Fibonacci number using both recursive and non-recursive methods.
int recursiveResult = recursiveFibonacci(n);
int nonRecursiveResult = nonRecursiveFibonacci(n);
Page | 12
CSE-PIET(PU)
Practical-7
2203031260035 OOP Laboratory
Aim:- Write a Java program that prompts the user for an integer and
then prints out all the prime numbers up to that Integer?
Code:-
// Import the Scanner class from the java.util package to read user input.
import java.util.Scanner;
Page | 13
CSE-PIET(PU)
2203031260035 OOP Laboratory
}
}
}
}
Output:-
Page | 14
CSE-PIET(PU)
Practical-8
2203031260035 OOP Laboratory
// Prompt the user to enter the dimensions of the first and second matrices.
System.out.print("Enter the number of rows in the first matrix: ");
int rowsA = scanner.nextInt();
System.out.print("Enter the number of columns in the first matrix: ");
int colsA = scanner.nextInt();
return result;
}
}
Output:-
Page | 16
CSE-PIET(PU)
Practical-9
2203031260035 OOP Laboratory
Page | 17
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 18
CSE-PIET(PU)
Practical-10
2203031260035 OOP Laboratory
// Default constructor
public Overloading() {
System.out.println("Default constructor");
}
// Method overloading
Page | 19
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 20
CSE-PIET(PU)
Practical-11
2203031260035 OOP Laboratory
// Concrete method
void display() {
System.out.println("This is a shape.");
}
}
Page | 22
CSE-PIET(PU)
Practical-12
2203031260035 OOP Laboratory
Output:-
Page | 24
CSE-PIET(PU)
Practical-13
2203031260035 OOP Laboratory
Page | 25
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 26
CSE-PIET(PU)
Practical-14
2203031260035 OOP Laboratory
Page | 27
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 28
CSE-PIET(PU)
Practical-15
2203031260035 OOP Laboratory
// Method in the outer class to create and use the inner class
void useInnerClass() {
InnerClass inner = new InnerClass(); // Create an instance of the inner class
inner.display(); // Call the display method of the inner class
}
}
Output:-
Page | 29
CSE-PIET(PU)
Practical-16
2203031260035 OOP Laboratory
package com.example;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
if (b != 0) {
return a / b;
} else {
throw new ArithmeticException("Cannot divide by zero!");
}
}
}
// Now, create another file outside the "com" folder to access the Calculator class from the user-
defined package.
import com.example.Calculator;
public class PackageExample {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int result = calculator.add(5, 3);
System.out.println("Addition: " + result);
result = calculator.subtract(5, 3);
System.out.println("Subtraction: " + result);
result = calculator.multiply(5, 3);
System.out.println("Multiplication: " + result);
result = calculator.divide(10, 2);
System.out.println("Division: " + result);
}
}
Page | 30
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 31
CSE-PIET(PU)
Practical-17
2203031260035 OOP Laboratory
if (line.isEmpty()) {
break; // Exit the loop when an empty line is entered
}
// Split the line into words using space as a delimiter and count words
String[] words = line.split("\\s+");
wordCount += words.length;
Page | 32
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 33
CSE-PIET(PU)
Practical-18
2203031260035 OOP Laboratory
int left = 0;
int right = str.length() - 1;
return true; // If the loop completes without finding a mismatch, it's a palindrome
}
}
Page | 34
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 35
CSE-PIET(PU)
Practical-19
2203031260035 OOP Laboratory
Aim:- Write a Java program that reads a line of integers and then
displays each integer and the sum of all integers. (Use StringTokenizer
class)?
Code:-
import java.util.Scanner;
import java.util.StringTokenizer;
Page | 36
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 37
CSE-PIET(PU)
Practical-20
2203031260035 OOP Laboratory
Aim:- Write a java program for creating single try block with multiple
catch blocks.
Code:-
import java.util.InputMismatchException;
import java.util.Scanner;
try {
// Prompt the user to enter a number
System.out.print("Enter a number: ");
int number = scanner.nextInt(); // Read an integer from the user
Page | 38
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 39
CSE-PIET(PU)
Practical-21
2203031260035 OOP Laboratory
Aim:- Write a program for multiple try blocks and multiple catch blocks
including finally.
Code:-
public class Main {
public static void main(String[] args) {
// Print a message to the console
System.out.println("Prince_Chhodavadiya");
try {
// Attempt to access an element outside the bounds of the array
int[] numbers = { 1, 2, 3 };
System.out.println(numbers[5]); // This will throw an ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
// Handle the specific exception for array index out of bounds
System.out.println("Array index out of bounds!");
}
try {
// Attempt to access the length of a null string
String str = null;
System.out.println(str.length()); // This will throw a NullPointerException
} catch (NullPointerException e) {
// Handle the specific exception for a null pointer
System.out.println("NullPointerException occurred!");
}
try {
// Attempt to perform division by zero
int result = 10 / 0; // This will throw an ArithmeticException
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
// Handle the specific exception for arithmetic errors
System.out.println("ArithmeticException occurred!");
} finally {
// This block is executed regardless of whether an exception was thrown
System.out.println("Finally block executed.");
}
Page | 40
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 41
CSE-PIET(PU)
Practical-22
2203031260035 OOP Laboratory
try {
// Attempt to set a negative age
int age = -5;
if (age < 0) {
// Throw a CustomException with a custom error message
throw new CustomException("Age cannot be negative");
}
System.out.println("Age: " + age);
} catch (CustomException e) {
// Catch and handle the CustomException
System.out.println("CustomException caught: " + e.getMessage());
}
}
}
Output:-
Page | 42
CSE-PIET(PU)
Practical-23
2203031260035 OOP Laboratory
Aim:- Write a java program for producer and consumer problem using
Threads.
Code:-
import java.util.LinkedList;
import java.util.Queue;
@Override
public void run() {
Page | 43
CSE-PIET(PU)
2203031260035 OOP Laboratory
try {
for (int i = 1; i <= 5; i++) {
buffer.produce(i); // Produce an item and add it to the buffer
Thread.sleep(1000); // Sleep for 1 second
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
@Override
public void run() {
try {
for (int i = 1; i <= 5; i++) {
int item = buffer.consume(); // Consume an item from the buffer
Thread.sleep(1500); // Sleep for 1.5 seconds
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
Page | 44
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 45
CSE-PIET(PU)
Practical-24
2203031260035 OOP Laboratory
while (true) {
int number = random.nextInt(100); // Generate a random integer between 0 and 99
System.out.println("Generated: " + number);
// Depending on whether the number is even or odd, create and start threads for square or cube
calculation
if (number % 2 == 0) {
Thread t2 = new Thread(new SquareCalculator(number));
t2.start(); // Start the square calculator thread
} else {
Thread t3 = new Thread(new CubeCalculator(number));
t3.start(); // Start the cube calculator thread
}
try {
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
@Override
public void run() {
Page | 46
CSE-PIET(PU)
2203031260035 OOP Laboratory
@Override
public void run() {
int cube = number * number * number;
System.out.println("Cube of " + number + ": " + cube);
}
}
Page | 47
CSE-PIET(PU)
Practical-25
2203031260035 OOP Laboratory
Page | 48
CSE-PIET(PU)
Practical-26
2203031260035 OOP Laboratory
while (true) {
System.out.println("Options:");
System.out.println("1. Add item");
System.out.println("2. Search item");
System.out.println("3. Remove item");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
switch (choice) {
case 1:
System.out.print("Enter item to add: ");
String newItem = scanner.nextLine();
items.add(newItem);
System.out.println("Item added: " + newItem);
break;
case 2:
System.out.print("Enter item to search: ");
String searchItem = scanner.nextLine();
Page | 50
CSE-PIET(PU)
2203031260035 OOP Laboratory
Output:-
Page | 51
CSE-PIET(PU)