0% found this document useful (0 votes)
3 views14 pages

Os 13

The document outlines various Java programming experiments related to Object-Oriented Programming (OOP) concepts, including inheritance, polymorphism, multithreading, matrix multiplication, user input handling, palindrome checking, array reversal, and ugly number validation. Each experiment includes a specific objective, code implementation, and expected output. The experiments are designed for B.Tech (CSE) students to enhance their understanding of Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

Os 13

The document outlines various Java programming experiments related to Object-Oriented Programming (OOP) concepts, including inheritance, polymorphism, multithreading, matrix multiplication, user input handling, palindrome checking, array reversal, and ugly number validation. Each experiment includes a specific objective, code implementation, and expected output. The experiments are designed for B.Tech (CSE) students to enhance their understanding of Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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:2301200100013


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:2301200100013


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:2301200100013
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’

Output:-

11 | P a g e Roll No:2301200100073
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:2301200100073
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:2301200100073
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-

14 | P a g e Roll No:2301200100073
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:2301200100073
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:2301200100073
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-

17 | P a g e Roll No:2301200100073
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:2301200100073
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-

19 | P a g e Roll No:2301200100073
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:2301200100073
OOPS with Java Lab B.Tech (CSE) || Sec- ‘A’
Output:-

21 | P a g e Roll No:2301200100073

You might also like