0% found this document useful (0 votes)
4 views5 pages

Oops Java Lab Programs (1 2 3)

The document provides three Java programs: one for adding two square matrices based on user input, another for implementing a stack class with basic operations, and a third for an Employee class that allows salary raises. Each program includes a main method demonstrating its functionality with sample outputs. The examples illustrate user interactions and the results of the operations performed.

Uploaded by

vidhyapatil096
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)
4 views5 pages

Oops Java Lab Programs (1 2 3)

The document provides three Java programs: one for adding two square matrices based on user input, another for implementing a stack class with basic operations, and a third for an Employee class that allows salary raises. Each program includes a main method demonstrating its functionality with sample outputs. The examples illustrate user interactions and the results of the operations performed.

Uploaded by

vidhyapatil096
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/ 5

1.

Develop a JAVA program to add two matrices of suitable order N (The value of N
should be read from command line arguments).

import java.util.Scanner;

public class AddMatrices {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter order of the square matrix (N): ");


int N = sc.nextInt();

int[][] A = new int[N][N];


int[][] B = new int[N][N];
int[][] C = new int[N][N];

System.out.println("Enter elements of first matrix:");


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = sc.nextInt();
}
}

System.out.println("Enter elements of second matrix:");


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
B[i][j] = sc.nextInt();
}
}

// Add matrices
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}

System.out.println("Sum of matrices:");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(C[i][j] + " ");
}
System.out.println();
}

sc.close();
}
}
OUTPUT:

Enter order of the square matrix (N): 2


Enter elements of first matrix:
12
34

Enter elements of second matrix:


56
78

Sum of matrices:
68
10 12

2. Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop
a JAVA main method to illustrate Stack operations.

package StackOperation;
import java.util.Scanner;

class Stack {
int[] stack = new int[10]; // Stack array of size 10
int top = -1; // Top of stack

// Push operation
void push(int value) {
if (top == 9) {
System.out.println("Stack Overflow! Cannot push " + value);
} else {
top++;
stack[top] = value;
System.out.println(value + " pushed into stack");
}
}

// Pop operation
void pop() {
if (top == -1) {
System.out.println("Stack Underflow! Nothing to pop");
} else {
System.out.println(stack[top] + " popped from stack");
top--;
}
}

// Display stack
void display() {
if (top == -1) {
System.out.println("Stack is empty!");
} else {
System.out.print("Stack elements: ");
for (int i = 0; i <= top; i++) {
System.out.print(stack[i] + " ");
}
System.out.println();
}
}
}

public class StackOperation {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack s = new Stack();
int choice, value;

do {
System.out.println("\n--- Stack Operations Menu ---");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();

switch (choice) {
case 1:
System.out.print("Enter value to push: ");
value = sc.nextInt();
s.push(value);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
System.out.println("Exiting... Goodbye!");
break;
default:
System.out.println("Invalid choice! Try again.");
}
} while (choice != 4);

sc.close();
}
}
OUTPUT:

10 pushed into stack


20 pushed into stack
30 pushed into stack
Stack elements: 10 20 30
30 popped from stack
Stack elements: 10 20

3. A class called Employee, which models an employee with an ID, name and salary, is
designed as the following class diagram. The method raise Salary (percent) increases
the salary by the given percentage. Develop the Employee class and suitable main
method for demonstration.

package Employee;
public class Employee {
private int id;
private String name;
private double salary;

public Employee(int id, String name, double salary) {


this.id = id;
this.name = name;
this.salary = salary;
}
public void raiseSalary(double percent) {
if (percent > 0) {
double raise = salary * (percent / 100);
salary += raise;
System.out.println(name + "'s salary raised by " + percent + "%. New salary: " + salary);
} else {
System.out.println("Please provide a positive percentage for salary raise.");
}
}
public void displayInfo() {
System.out.println("Employee ID: " + id);
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}

// Main method
public static void main(String[] args) {
// Creating an employee object
Employee emp = new Employee(1001, "John Doe", 50000);

// Displaying initial information


System.out.println("Initial Information:");
emp.displayInfo();

// Raising salary by a given percentage


double raisePercentage = 10;
emp.raiseSalary(raisePercentage);

// Displaying updated information after the raise


System.out.println("\nInformation after salary raise:");
emp.displayInfo();
}
}

OUTPUT:

Initial Information:
Employee ID: 1001
Name: John Doe
Salary: 50000.0
John Doe's salary raised by 10.0%. New salary: 55000.0

Information after salary raise:


Employee ID: 1001
Name: John Doe
Salary: 55000.0

You might also like