Question: Problem Description Given two integer matrices, A and B , you are asked to write a program to perform matrix addition ( A + B

Problem Description
Given two integer matrices, A and B, you are asked to write a program to perform matrix addition (A + B).
Both matrices will have the same number of rows and columns.
You need to divide A and B into four equal (or close to equal) size submatrices (I will refer to them as A00, A01, A10, A11 and B00, B01, B10, B11)
If the original matrices have R rows and C columns, then each submatrix should have dimensions close to (R/2) x (C/2). In other words, each submatrix should be about one-quarter the size of the original matrices.
You need to create four Java threads. Each thread performs addition on one pair of the submatrices. For example, thread 0 performs addition on A00 and B00, thread 1 performs addition on A01 and B01,... etc.
The final result should be stored in a matrix C of size R x C.
You must divide the two-dimensional array into the form such as: A=[(A_00&A_01@A_10&A_11)]. Same for B and C.
Using the above example, if
A =231251
312224
123272
361513
Then the upper left corner is
A_00=231
312
One of your threads is responsible for adding
A_00+ B_00=C_00
Same as
231+654=885
312332643
List of classes that you will write:
Main contains the main method.
ThreadOperation extends Thread and performs submatrix addition
Instructions for Part 1
For part 1 you need to create both of the above classes.
In the main method of Main, instantiate four ThreadOperation objects, start them, and join them. Each ThreadOperation will take as input (through the constructor) two matrices and a quadrant indicator. The indicator could be a String, an int, an enum or a set of indexes. Its up to you.
In Main.java, write a static method named print2dArray that takes a two-dimensional array as input and prints it out with the rows and columns lined up. You must use System.out.printf.
Instantiate a test 2d array with any values you like in main and use it to verify that print2dArray works.
The filename should be given through the command prompt and passed into main via String[] args
Open and connect to the file using a Scanner.
Read in the number of rows and columns and save these in local variables in main.
Read in the first and second matrices (two-dimensional arrays) from the file. I recommend writing a method to accomplish this task and calling the method twice (once for each matrix). Consider using this method header:
public static int[][] matrixFromFile(int rows, int columns, Scanner file_reader)
NOTE: if you are using a static scanner or an object-oriented approach then you may not need to pass these arguments to the method.
Information on the file format
the first line has two numbers, R and C (R rows, C columns), the size of both matrices A and B
the next R lines each has C elements for one of the rows of A
the next R lines each has C elements for one of the rows of B
Example:
46
231251
312224
123272
361513
654143
332211
754325
218484
For the above example, 4 is the number of rows, 6 is the number of columns. The first matrix values are highlighted in green and the second matrix is highlighted in red. The result of the sum should be as follow:
885394
644435
877597
579997
Example: The upper left quadrants of the corresponding matrices (highlighted in yellow) will be added together
46
231251
312224
123272
361513
654143
332211
754325
218484
For your convenience, three test cases are provided: matrix1.txt, matrix2.txt, and matrix3.txt.
One of the goals is to minimize the resource usage, such as memory and processor cycles. Explain how multi-threaded code accomplishes this goal in your document. YOU MUST ANSWER THIS QUESTION IN A COMMENT AT THE TOP OF YOUR Main CLASS. Tell me about blocking on I/O, multicore machines, how sluggish humans are, etcetera, and then tell me how multi-threading helps. Compare threads to processes and tell me the advantages of multi-threading. It doesnt have to be long. Three sentences will suffice if they are good sentences.
UML Diagram for Matrix Addition Part 1
Main
+ print2dArray(matrix: int[][]) : void
ThreadOperation
- A : int[][]
- B : int[][]
- quadrant : String
<>ThreadOperation(A : int[][], B : int[][], quadrant : String)
+ run() : void
Compilation and Execution
test your program as follows:
javac *.java
java Main matrix1.txt
WE USE THIS AS A BASE
/*
This code is provided to give you a
starting place. It should be modified.
No further imports are needed.
*/
import java.io.IOException;
import java.io.File;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System.out.println();
}
}
TEST USING
46
231251
312224
123272
361513
654143
332211
754325
218484

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!