Any help is appreciated
Problem:
Code
Write a Java program that uses multithreading to implement a 3x3
matrix multiplication. The specifications for the program are given as follows:
Assume that the content of input matrices A and B are entered by user from standard input
(keyboard). You may assume input matrices are called A and B, and the resulting matrix is called
Result.
Suppose that your program implements a multiprocessor scheme. So, create a thread for every
processor so that each processor will compute one element of the resulting matrix. In other words,
*
3
in each thread calculate one element (i.e., multiply the elements of the ith row from A and jth
column from B, and add the results to get the value of Result[i.j]).
Program:
Code
import java.util.Scanner;
import java.util.concurrent.*;
public class MatrixMultiplication {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int[][] matrix1 = new int[3][3];
int[][] matrix2 = new int[3][3];
int[][] matrix3 = new int[3][3];
System.out.println("Insert values in Matrix1.");
for(int i=0; i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.println("Column "+ (i+1) + " Row " + (j+1) + ": ");
matrix1[i][j] = keyboard.nextInt();
}
}
System.out.println("Insert values in Matrix2.");
for(int i=0; i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.println("Column "+ (i+1) + " Row " + (j+1) + ": ");
matrix2[i][j] = keyboard.nextInt();
}
}
Runnable one = new Multiply(matrix1, matrix2, matrix3, 0, 0);
Runnable two = new Multiply(matrix1, matrix2, matrix3, 0,1);
Runnable three = new Multiply(matrix1, matrix2, matrix3, 0,2);
Runnable four = new Multiply(matrix1, matrix2, matrix3, 1,0);
Runnable five = new Multiply(matrix1, matrix2, matrix3, 1,1);
Runnable six = new Multiply(matrix1, matrix2, matrix3, 1,2);
Runnable seven = new Multiply(matrix1, matrix2, matrix3, 2,0);
Runnable eight = new Multiply(matrix1, matrix2, matrix3, 2,1);
Runnable nine = new Multiply(matrix1, matrix2, matrix3, 2,2);
Thread thread1 = new Thread(one);
Thread thread2 = new Thread(two);
Thread thread3 = new Thread(three);
Thread thread4 = new Thread(four);
Thread thread5 = new Thread(five);
Thread thread6 = new Thread(six);
Thread thread7 = new Thread(seven);
Thread thread8 = new Thread(eight);
Thread thread9 = new Thread(nine);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
System.out.println("Matrix 3: ");
for(int i=0; i<3;i++)
{
System.out.println();
for(int j=0;j<3;j++)
{
System.out.print(Matrix3[i][j] + " ");
}
}
}
class Multiply implements Runnable
{
int temp=0;
int m;
int n;
public Multiply(int[][] matrix1, int[][] matrix2, int[][] matrix3, int i, int j)
{
int[][] matrixA = matrix1;
int[][] matrixB = matrix2;
int[][] matrixC = matrix3;
m = i;
n = j;
}
@Override
public void run()
{
for(int p=0;p<3;p++)
{
temp += matrixA[m][p] * matrixB[p][n];
}
matrixC[m][n] = temp;
}
}
}
Errors:
Code
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:33: error: non-static variable this cannot be referenced from a static context
Runnable one = new Multiply(matrix1, matrix2, matrix3, 0, 0);
^
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:34: error: non-static variable this cannot be referenced from a static context
Runnable two = new Multiply(matrix1, matrix2, matrix3, 0,1);
^
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:35: error: non-static variable this cannot be referenced from a static context
Runnable three = new Multiply(matrix1, matrix2, matrix3, 0,2);
^
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:36: error: non-static variable this cannot be referenced from a static context
Runnable four = new Multiply(matrix1, matrix2, matrix3, 1,0);
^
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:37: error: non-static variable this cannot be referenced from a static context
Runnable five = new Multiply(matrix1, matrix2, matrix3, 1,1);
^
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:38: error: non-static variable this cannot be referenced from a static context
Runnable six = new Multiply(matrix1, matrix2, matrix3, 1,2);
^
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:39: error: non-static variable this cannot be referenced from a static context
Runnable seven = new Multiply(matrix1, matrix2, matrix3, 2,0);
^
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:40: error: non-static variable this cannot be referenced from a static context
Runnable eight = new Multiply(matrix1, matrix2, matrix3, 2,1);
^
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:41: error: non-static variable this cannot be referenced from a static context
Runnable nine = new Multiply(matrix1, matrix2, matrix3, 2,2);
^
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:69: error: cannot find symbol
System.out.print(Matrix3[i][j] + " ");
^
symbol: variable Matrix3
location: class MatrixMultiplication
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:95: error: cannot find symbol
temp += matrixA[m][p] * matrixB[p][n];
^
symbol: variable matrixA
location: class MatrixMultiplication.Multiply
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:95: error: cannot find symbol
temp += matrixA[m][p] * matrixB[p][n];
^
symbol: variable matrixB
location: class MatrixMultiplication.Multiply
C:\Users\Ryan\Documents\JCreator Pro\MyProjects\MatrixMultiplication\src\MatrixMultiplication.java:97: error: cannot find symbol
matrixC[m][n] = temp;
^
symbol: variable matrixC
location: class MatrixMultiplication.Multiply
13 errors
Process completed.