0% found this document useful (0 votes)
20 views1 page

Import Mathematics

The document contains a Java program that defines a Matrix class with methods for adding and subtracting 2x2 matrices. It initializes two matrices, displays them, and prints the results of their addition and subtraction. The main program demonstrates the functionality of these methods.

Uploaded by

rudrabes
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)
20 views1 page

Import Mathematics

The document contains a Java program that defines a Matrix class with methods for adding and subtracting 2x2 matrices. It initializes two matrices, displays them, and prints the results of their addition and subtraction. The main program demonstrates the functionality of these methods.

Uploaded by

rudrabes
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/ 1

import Mathematics.

Matrix; package Mathematics;

public class MainProgram { public class Matrix {

public static void main(String[] args) { public int[][] add(int[][] A, int[][] B) {

int[][] matrix1 = { int[][] result = new int[2][2];

{1, 2}, for (int i = 0; i < 2; i++) {

{3, 4} for (int j = 0; j < 2; j++) {

}; result[i][j] = A[i][j] + B[i][j];

int[][] matrix2 = { }

{5, 6}, return result;

{7, 8} }

};

public int[][] subtract(int[][] A, int[][] B) {

Matrix m = new Matrix(); int[][] result = new int[2][2];

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

System.out.println("Matrix 1:"); for (int j = 0; j < 2; j++) {

m.display(matrix1); result[i][j] = A[i][j] - B[i][j];

} }

System.out.println("Matrix 2:"); return result;

m.display(matrix2); }

public void display(int[][] matrix) {

int[][] sum = m.add(matrix1, matrix2); for (int i = 0; i < 2; i++) {

System.out.println("Addition Result:"); for (int j = 0; j < 2; j++) {

m.display(sum); System.out.print(matrix[i][j] + " ");

int[][] diff = m.subtract(matrix1, matrix2); System.out.println();

System.out.println("Subtraction Result:"); }

m.display(diff); }

} }

You might also like