Oops New
Oops New
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;
return matrix;
}
return result;
}
}
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;
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;
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;
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;
}
}
}
}