import [Link].
Scanner;
class MatrixInputOutput {
private Scanner sc = new Scanner([Link]);
public void printMatrix(int[][] matrix) {
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[i].length; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
public int[][] getMatrix(int rows, int cols) {
int[][] matrix = new int[rows][cols];
[Link]("Enter the Elements of the matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link]("Element [" + i + "][" + j + "]: ");
matrix[i][j] = [Link]();
}
}
return matrix;
}
}
class MatrixOperation {
public int[][] multiply(int[][] matrixA, int[][] matrixB, int rowsA, int colsA,
int colsB) {
int[][] result = new int[rowsA][colsB];
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = 0;
for (int k = 0; k < colsA; k++) {
result[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
}
return result;
}
public int[][] add(int[][] matrixA, int[][] matrixB, int rows, int cols) {
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
return result;
}
public int[][] subtract(int[][] matrixA, int[][] matrixB, int rows, int cols) {
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrixA[i][j] - matrixB[i][j];
}
}
return result;
}
public int[][] transpose(int[][] matrix, int rows, int cols) {
int[][] result = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[j][i] = matrix[i][j];
}
}
return result;
}
}
public class Matrix {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("***************** Matrix Operation
******************");
[Link]("Enter the Rows of Matrix-A: ");
int rowsA = [Link]();
[Link]("Enter the Columns of Matrix-A: ");
int colsA = [Link]();
[Link]("Enter the Rows of Matrix-B: ");
int rowsB = [Link]();
[Link]("Enter the Columns of Matrix-B: ");
int colsB = [Link]();
if (rowsA != rowsB || colsA != colsB) {
[Link]("Addition and subtraction require matrices of the
same dimensions.");
}
if (colsA != rowsB) {
[Link]("Matrix multiplication requires that columns of
Matrix A equal rows of Matrix B.");
}
MatrixInputOutput matrixIO = new MatrixInputOutput();
MatrixOperation matrixOperation = new MatrixOperation();
int[][] matrixA = [Link](rowsA, colsA);
int[][] matrixB = [Link](rowsB, colsB);
[Link]("Matrix - A:");
[Link](matrixA);
[Link]("Matrix - B:");
[Link](matrixB);
while (true) {
[Link]("Matrix Operation");
[Link]("1. Matrix Addition");
[Link]("2. Matrix Subtraction");
[Link]("3. Matrix Multiplication");
[Link]("4. Transpose Matrix A");
[Link]("5. Transpose Matrix B");
[Link]("6. Exit");
[Link]("Enter your Choice: ");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Matrix Addition Result:");
int[][] additionResult = [Link](matrixA, matrixB,
rowsA, colsA);
[Link](additionResult);
break;
case 2:
[Link]("Matrix Subtraction Result (A - B):");
int[][] subtractionResult = [Link](matrixA,
matrixB, rowsA, colsA);
[Link](subtractionResult);
break;
case 3:
if (colsA != rowsB) {
[Link]("Matrix multiplication is not possible
due to dimension mismatch.");
} else {
[Link]("Matrix Multiplication Result:");
int[][] multiplicationResult =
[Link](matrixA, matrixB, rowsA, colsA, colsB);
[Link](multiplicationResult);
}
break;
case 4:
[Link]("Transpose of Matrix A:");
int[][] transposeA = [Link](matrixA, rowsA,
colsA);
[Link](transposeA);
break;
case 5:
[Link]("Transpose of Matrix B:");
int[][] transposeB = [Link](matrixB, rowsB,
colsB);
[Link](transposeB);
break;
case 6:
[Link]("Exiting...");
[Link]();
return;
default:
[Link]("Invalid choice. Please enter a number
between 1 and 6.");
break;
}
}
}
}