0% found this document useful (0 votes)
61 views

Oops New

Uploaded by

Shivani Appi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Oops New

Uploaded by

Shivani Appi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Object Oriented Programming with JAVA BCS306A

1. Develop a JAVA program to add TWO matrices of suitable order N (The value of N should be read
from command line arguments).
package java1;
import java.util.Scanner;

public class MatrixAddition {


public static void main(String[] args) {
int m, n;
int[][] a, b, c;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the size of matrix (row, col): ");
m = scanner.nextInt();
n = scanner.nextInt();
System.out.println("Enter the elements of the first matrix:");
a = readMatrix(m, n);
System.out.println("Enter the elements of the second matrix:");
b = readMatrix(m, n);
c = addMatrix(a, b);
System.out.println("Result:");
printMatrix(c);
scanner.close();
}

public static int[][] readMatrix(int m, int n) {


Scanner scanner = new Scanner(System.in);
int[][] matrix = new int[m][n];

for (int i = 0; i < m; i++) {


for (int j = 0; j < n; j++) {
matrix[i][j] = scanner.nextInt();
}
}

return matrix;
}

public static void printMatrix(int[][] matrix) {


for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}

public static int[][] addMatrix(int[][] a, int[][] b) {


int[][] result = new int[a.length][a[0].length];

for (int i = 0; i < a.length; i++) {


for (int j = 0; j < a[0].length; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}

return result;
}
}

Dept. of ISE, GSSSIETW, Mysuru


Object Oriented Programming with JAVA BCS306A

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 java1;
import java.util.Scanner;

public class Stack {


final int MAX = 10;
int s[] = new int[MAX];
int top = -1;
void push(int element) {
if (top >= MAX - 1)
System.out.println("Stack overflow");
else
s[++top] = element;
}

int pop() {
int element = 0;
if (top == -1)
System.out.println("Stack underflow");
else
element = s[top--];
return element;
}

void display() {
if (top == -1)
System.out.println("Stack empty");
else {
System.out.print("Elements in the stack are: ");
for (int i = top; i > -1; i--)
System.out.print(s[i] + " ");
System.out.println(); // add a new line after displaying elements
}
}
}

package java1;

import java.util.Scanner;

public class StackDemo {


public static void main(String args[]) {
Stack stack = new Stack();
System.out.println("Program to perform stack operations");
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("1. push 2. pop 3. display 4. Exit");
System.out.println("Enter your choice");
int choice = scanner.nextInt();
int element = 0;

switch (choice) {
case 1:
System.out.println("Enter the element to be pushed");
element = scanner.nextInt();
stack.push(element);
break;

Dept. of ISE, GSSSIETW, Mysuru


Object Oriented Programming with JAVA BCS306A

case 2:
element = stack.pop();
System.out.println("The popped element is: " + element);
break;
case 3:
System.out.println("Elements in the stack are:");
stack.display();
break;
case 4:
return;
}
}
}
}

Dept. of ISE, GSSSIETW, Mysuru

You might also like