OOPS with Java Lab B.
Tech (AI/ML) || Sec- ‘E’
1| P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
INDEX
2| P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Experiment – 1
Object:- Write a java program to find the Fibonacci series using recursive and non
recursive functions.
Program:-
class fib {
int a,b,c;
void nonrecursive(int n) //Non recursive function to find the Fibonacci series.
{
a =0;
b=1;
[Link](a+ ", " + b);
c =a+b;
while(c<=n)
{
[Link](", " +c);
a =b;
b=c;
c=a+b;
}
}
int recursive(int n) // Recursive function to find the Fibonacci series.
{
if(n==0)
return (0);
if(n==1)
return (1);
else
return(recursive(n-1)+recursive(n-2));
}
}
public class Fibonacci {
public static void main(String[] args) {
int n=5;
[Link]("The Fibonacci series using non recursive is");
// Creating object for the fib class.
fib f=new fib();
// Calling non recursive function of fib class.
[Link](n);
[Link]("\n The Fibonacci series using recursive is");
3| P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
for(int i=0;i<=n;i++){// Calling recursive function of fib class.
int F1=[Link](i);
[Link](F1 + ", ");
}
}
}
Output:-
4| P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Experiment –2
Object:-Develop a program that accepts command line arguments to calculate and
display the sum of integers.
Program:-
public class SumCalculator {
public static void main(String[] args) {
int sum = 0;
for (String arg : args) {
try {
int num = [Link](arg);
sum += num;
}
catch (NumberFormatException e) {
[Link]("Error: Invalid input. Please provide valid integers.");
[Link](1);
}
}
[Link]("Sum of the provided integers: " + sum);
}
}
Output:-
5| P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Experiment –3
Object:-Write a java program for Method overloading and Constructor overloading.
Program:-
Method overloading:-
import [Link].*;
class MethodOverloading {
static int add(int a, int b)
{
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
public static void main(String args[])
{
[Link]("add() with 2 parameters");
[Link](add(7, 3));
[Link]("add() with 3 parameters");
[Link](add(7, 8, 5));
}
}
Output:-
6| P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Constructor overloading:-
public class Student {
// Instance variables of the class
int id;
String name;
Student(){
[Link]("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
public static void main(String[] args) {
// Object creation
Student s = new Student();
[Link]("\nDefault Constructor values: \n");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
[Link]("\nParameterized Constructor values: \n");
Student student = new Student(28, "Divyansh");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
}
}
Output:-
7| P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Experiment –4
Object:-Create a program where different types of employees (e.g., Manager, Engineer)
inherit from a base Employee class and demonstrate polymorphic behavior (e.g.,
calculate salary).
Program:-
// Employee Class
class Employee {
public String name;
public double salary;
public Employee(String name, double salary) {
[Link] = name;
[Link] = salary;
}
public double calculateSalary() {
// Default implementation for bonus calculation
return 0.0;
}
}
// Manager Class
class Manager extends Employee {
public Manager(String name, double salary) {
super(name, salary);
}
@Override
public double calculateSalary() {
return salary * 0.25;
}
}
// Engineer Class
class Engineer extends Employee {
public Engineer(String name, double salary) {
super(name, salary);
}
@Override
8| P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
public double calculateSalary() {
return salary * 0.5;
}
}
// Main Class (Driver class)
public class Main {
public static void main(String[] args) {
Manager manager = new Manager("Rajeev", 80000);
Engineer engineer = new Engineer("Sandeep", 60000);
[Link]([Link] + "'s salary with bonus: Rs " + [Link]());
[Link]([Link] + "'s salary with bonus: Rs " + [Link]());
}
}
Output:-
9| P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Experiment –5
Object:-Develop a multithreaded Java application where each thread performs a
specific task and handles exceptions appropriately (e.g., using try-catch blocks).
Program:-
import [Link].*;
class MyThread extends Thread {
public void run() {
try {
// Perform your specific task here
[Link]("Thread " + [Link]().getId() + " is executing.");
// Simulate an exception (for demonstration purposes)
throw new IOException("An I/O exception occurred.");
}
catch (IOException e) {
[Link]("Exception caught: " + [Link]());
}
}
}
public class MultithreadedApp {
public static void main(String[] args) {
int numThreads = 3; // Number of threads to create
for (int i = 0; i<numThreads; i++) {
MyThread thread = new MyThread();
[Link](); // Start the thread
}
}
}
10 | P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Output:-
11 | P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Experiment –6
Object:-Write a Java program to multiply two given matrix.
Program:-
//Multiply two matrices program in java
import [Link].*;
// Driver Class
class matricesmultiplication {
// Function to print Matrix
static void printMatrix(int M[][], int rowSize,
int colSize)
{
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++)
[Link](M[i][j] + " ");
[Link]();
}
}
// Function to multiply
// two matrices A[][] and B[][]
static void multiplyMatrix(int row1, int col1,
int A[][], int row2,
int col2, int B[][])
{
int i, j, k;
// Print the matrices A and B
[Link]("\nMatrix A:");
printMatrix(A, row1, col1);
[Link]("\nMatrix B:");
printMatrix(B, row2, col2);
// Check if multiplication is Possible
if (row2 != col1) {
[Link](
"\nMultiplication Not Possible");
return;
}
// Matrix to store the result
// The product matrix will
// be of size row1 x col2
int C[][] = new int[row1][col2];
// Multiply the two matrices
for (i = 0; i < row1; i++) {
12 | P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
for (j = 0; j < col2; j++) {
for (k = 0; k < row2; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
// Print the result
[Link]("\nResultant Matrix:");
printMatrix(C, row1, col2);
}
// Driver code
public static void main(String[] args)
{
int row1 = 4, col1 = 3, row2 = 3, col2 = 4;
int A[][] = { { 1, 1, 1 },
{ 2, 2, 2 },
{ 3, 3, 3 },
{ 4, 4, 4 } };
int B[][] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 } };
multiplyMatrix(row1, col1, A, row2, col2, B);
}
}
13 | P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Output:-
14 | P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Experiment –7
Object:-Write a Java program to display the Employee details using scanner class.
Program:-
// Java program to demonstrate the use of Scanner class
// to take input from user
import [Link];
class employeedetails
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter here Employee details:");
String name = [Link]();
[Link]("Hello, " + name + " Welcome to the Java Programming.");
}
Output:-
15 | P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Experiment –8
Object:-Write a Java program that checks whether a given string is palindrome
number or not.
Program:-
class palindrome {
public static void main(String[] args) {
String str = "Radar", reverseStr = "";
int strLength = [Link]();
for (int i = (strLength - 1); i >=0; --i) {
reverseStr = reverseStr + [Link](i);
}
if ([Link]().equals([Link]())) {
[Link](str + " is a Palindrome String.");
}
else {
[Link](str + " is not a Palindrome String.");
}
}
}
16 | P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Output:-
17 | P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Experiment –9
Object:-Write a Java program to reverse an array.
Program:-
// Java Program to reverse an array
// using loop
import [Link];
public class reverseanarray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
// Swap elements from start to end
for (int i = 0; i < [Link] / 2; i++) {
int t = arr[i];
arr[i] = arr[[Link] - 1 - i];
arr[[Link] - 1 - i] = t;
}
[Link]("" + [Link](arr));
}
}
18 | P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Output:-
19 | P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Experiment –10
Object:-Write a Java program to checks whether a given number is ugly or not.
Program:-
// Java implementation to
// check if a number is ugly number
import [Link].*;
public class uglynumber {
// Function to check the ugly
// number
static int isUgly(int n)
{
// Base Cases
if (n == 1)
return 1;
if (n <= 0)
return 0;
// Condition to check if
// a number is divide by
// 2, 3, or 5
if (n % 2 == 0) {
return (isUgly(n / 2));
}
if (n % 3 == 0) {
return (isUgly(n / 3));
}
if (n % 5 == 0) {
return (isUgly(n / 5));
}
return 0;
}
// Driver Code
public static void main(String args[])
{
int no = isUgly(15);
if (no == 1)
[Link]("Yes");
else
[Link]("No");
}
}
20 | P a g e Roll No:23012016400
OOPS with Java Lab [Link] (AI/ML) || Sec- ‘E’
Output:-
21 | P a g e Roll No:23012016400