OOPS with Java Lab B.
Tech (AI/ML) || Sec- ‘E’
1|Page Roll No:2301200100062
OOPS with Java Lab B.Tech (AI/ML) || Sec- ‘E’
2|Page Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
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;
System.out.print(a+ ", " + b);
c =a+b;
while(c<=n)
{
System.out.print(", " +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;
System.out.println("The Fibonacci series using non recursive is");
// Creating object for the fib class.
fib f=new fib();
// Calling non recursive function of fib class.
f.nonrecursive(n);
System.out.println("\n The Fibonacci series using recursive is");
3|Page Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
for(int i=0;i<=n;i++){// Calling recursive function of fib class.
int F1=f.recursive(i);
System.out.print(F1 + ", ");
}
}
}
Output:-
4|Page Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
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 = Integer.parseInt(arg);
sum += num;
}
catch (NumberFormatException e) {
System.err.println("Error: Invalid input. Please provide valid integers.");
System.exit(1);
}
}
System.out.println("Sum of the provided integers: " + sum);
}
}
Output:-
5|Page Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Experiment –3
Object:-Write a java program for Method overloading and Constructor overloading.
Program:-
Method overloading:-
import java.io.*;
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[])
{
System.out.println("add() with 2 parameters");
System.out.println(add(7, 3));
System.out.println("add() with 3 parameters");
System.out.println(add(7, 8, 5));
}
}
Output:-
6|Page Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Constructor overloading:-
public class Student {
// Instance variables of the class
int id;
String name;
Student(){
System.out.println("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();
System.out.println("\nDefault Constructor values: \n");
System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name);
System.out.println("\nParameterized Constructor values: \n");
Student student = new Student(28, "Divyansh");
System.out.println("Student Id : "+student.id + "\nStudent Name : "+student.name);
}
}
Output:-
7|Page Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
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) {
this.name = name;
this.salary = 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|Page Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
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);
System.out.println(manager.name + "'s salary with bonus: Rs " + manager.calculateSalary());
System.out.println(engineer.name + "'s salary with bonus: Rs " + engineer.calculateSalary());
}
}
Output:-
9|Page Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
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 java.io.*;
class MyThread extends Thread {
public void run() {
try {
// Perform your specific task here
System.out.println("Thread " + Thread.currentThread().getId() + " is executing.");
// Simulate an exception (for demonstration purposes)
throw new IOException("An I/O exception occurred.");
}
catch (IOException e) {
System.err.println("Exception caught: " + e.getMessage());
}
}
}
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();
thread.start(); // Start the thread
}
}
}
10 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-
11 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Experiment –6
Object:-Write a Java program to multiply two given matrix.
Program:-
//Multiply two matrices program in java
import java.io.*;
// 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++)
System.out.print(M[i][j] + " ");
System.out.println();
}
}
// 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
System.out.println("\nMatrix A:");
printMatrix(A, row1, col1);
System.out.println("\nMatrix B:");
printMatrix(B, row2, col2);
// Check if multiplication is Possible
if (row2 != col1) {
System.out.println(
"\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:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
for (j = 0; j < col2; j++) {
for (k = 0; k < row2; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
// Print the result
System.out.println("\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:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-
14 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
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 java.util.Scanner;
class employeedetails
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter here Employee details:");
String name = sc.nextLine();
System.out.println("Hello, " + name + " Welcome to the Java Programming.");
}
Output:-
15 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
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 = str.length();
for (int i = (strLength - 1); i >=0; --i) {
reverseStr = reverseStr + str.charAt(i);
}
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + " is a Palindrome String.");
}
else {
System.out.println(str + " is not a Palindrome String.");
}
}
}
16 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-
17 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Experiment –9
Object:-Write a Java program to reverse an array.
Program:-
// Java Program to reverse an array
// using loop
import java.util.Arrays;
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 < arr.length / 2; i++) {
int t = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = t;
}
System.out.println("" + Arrays.toString(arr));
}
}
18 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-
19 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
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 java.io.*;
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)
System.out.println("Yes");
else
System.out.println("No");
}
}
20 | P a g e Roll No:2301200100062
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-
21 | P a g e Roll No:2301200100062