68 lines
1.8 KiB
Java
68 lines
1.8 KiB
Java
|
import java.util.Random;
|
||
|
|
||
|
public class Main {
|
||
|
static int SIZE = 4;
|
||
|
int [][] a;
|
||
|
int [][] b;
|
||
|
int [][] c;
|
||
|
int [][] d;
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
var main = new Main();
|
||
|
main.execute();
|
||
|
}
|
||
|
|
||
|
void execute() {
|
||
|
var random = new Random();
|
||
|
a = new int[SIZE][SIZE];
|
||
|
b = new int[SIZE][SIZE];
|
||
|
c = new int[SIZE][SIZE];
|
||
|
d = new int[SIZE][SIZE];
|
||
|
for (int row = 0; row < SIZE; row++) {
|
||
|
for (int col = 0; col < SIZE; col++) {
|
||
|
a[row][col] = random.nextInt(10);
|
||
|
}
|
||
|
}
|
||
|
for (int row = 0; row < SIZE; row++) {
|
||
|
for (int col = 0; col < SIZE; col++) {
|
||
|
b[row][col] = random.nextInt(10);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
printMatrix(a, "A");
|
||
|
printMatrix(b, "B");
|
||
|
|
||
|
for (int row = 0; row < SIZE; row++) {
|
||
|
for (int col = 0; col < SIZE; col++) {
|
||
|
c[row][col] = computeAtPosition(a, b, row, col);
|
||
|
}
|
||
|
}
|
||
|
printMatrix(c, "C");
|
||
|
|
||
|
for (int row = 0; row < SIZE; row++) {
|
||
|
for (int col = 0; col < SIZE; col++) {
|
||
|
d[row][col] = computeAtPosition(c, a, row, col);
|
||
|
}
|
||
|
}
|
||
|
printMatrix(d, "D");
|
||
|
}
|
||
|
|
||
|
static int computeAtPosition(int[][] m1, int[][] m2, int row, int col) {
|
||
|
int product = 0;
|
||
|
for (int k = 0; k < SIZE; k++) {
|
||
|
product += m1[row][k] * m2[k][col];
|
||
|
}
|
||
|
return product;
|
||
|
}
|
||
|
|
||
|
static void printMatrix(int[][] m1, String label) {
|
||
|
System.out.println(label);
|
||
|
for (int i = 0; i < SIZE; i++) {
|
||
|
for (int j = 0; j < SIZE; j++) {
|
||
|
System.out.print("" + m1[i][j] + "\t");
|
||
|
}
|
||
|
System.out.println();
|
||
|
}
|
||
|
}
|
||
|
}
|