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

Java Matrix Operations Example

The document contains a Java class named 'Matrix' that implements basic matrix operations such as addition, subtraction, and multiplication. It includes methods for inputting and printing the matrix, as well as error handling for incompatible matrix operations. The main method demonstrates the usage of the class by creating two matrices, performing operations on them, and displaying the results.

Uploaded by

lfminhtu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Java Matrix Operations Example

The document contains a Java class named 'Matrix' that implements basic matrix operations such as addition, subtraction, and multiplication. It includes methods for inputting and printing the matrix, as well as error handling for incompatible matrix operations. The main method demonstrates the usage of the class by creating two matrices, performing operations on them, and displaying the results.

Uploaded by

lfminhtu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Import [Link].

Scanner;

Public class Matrix {

Int m, n, i, j;

Int[][] matrix;

Public Matrix(int m, int n) {

This.m = m;

This.n = n;

[Link] = new int[m][n];

Public void InputMatrix(Scanner sc) {

[Link](“Nhap phan tu ma tran: “);

For (i = 0; i < m; i++) {

For (j = 0; j < n; j++) {


[Link](“Nhap a%d%d: “, i + 1, j + 1);

Matrix[i][j] = [Link]();

Public void PrintMatrix() {

For (i = 0; i < m; i++) {

For (j = 0; j < n; j++) {

[Link](“%d “, [Link][i][j]);

[Link](“\n”);

}
Public Matrix AddMatrix(Matrix other) {

If (this.m != other.m || this.n != other.n)

Throw new IllegalArgumentException(“Khong cong duoc 2 ma


tran.”);

Matrix res = new Matrix(m, n);

For (i = 0; i < m; i++) {

For (j = 0; j < n; j++) {

[Link][i][j] = [Link][i][j] + [Link][i][j];

Return res;

}
Public Matrix MinusMatrix(Matrix other) {

If (this.m != other.m || this.n != other.n)

Throw new IllegalArgumentException(“Khong tru duoc 2 ma


tran.”);

Matrix res = new Matrix(m, n);

For (i = 0; i < m; i++) {

For (j = 0; j < n; j++) {

[Link][i][j] = [Link][i][j] – [Link][i][j];

Return res;

}
Public Matrix MultiplyMatrix(Matrix other) {

If (this.n != other.m)

Throw new IllegalArgumentException(“Khong nhan duoc 2 ma


tran.”);

Matrix res = new Matrix(this.m, other.n);

For (int i = 0; i < this.m; i++) {

For (int j = 0; j < other.n; j++) {

Int sum = 0;

For (int k = 0; k < this.n; k++) {

Sum += [Link][i][k] * [Link][k][j];

}
[Link][i][j] = sum;

Return res;

Public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

Matrix m1 = new Matrix(2, 2);

[Link](sc);

[Link]();

Matrix m2 = new Matrix(2, 2);

[Link](sc);

[Link]();
Matrix sum = [Link](m2);

[Link]();

Matrix diff = [Link](m2);

[Link]();

Matrix prod = [Link](m2);

[Link]();

[Link]();

You might also like