Requirement: to multiply A and B matrices, the number of columns in A must be equal to the number of rows in B. You must first do this validity check and throw an IllegalArgumentException type exception if the requirement is not met.
Reminder: the number of rows in a two-dimensional array arr is arr.lenght and the number of columns is arr[0].length (assuming all rows have the same number of elements, which is the case when representing a matrix).
- main: In this method you will do the following (no need to do any validity checks in main):
- Prompt the user to enter the input-files name, then input the file-name and set up a scanner.
- Create two 2-dimensional arrays for matrices A and B, and fill A and B with numbers, inputting values from the input-file as described below.
In the input-file you will have integer numbers only (there will not be any other symbols or signs). All numbers will be separated by whitespaces. The first two numbers will indicate the size of the A matrix (i.e. the number of rows and columns respectively), then values of the A matrix will follow (row after row, listing elements from left to right). After that, the next two numbers will indicate the size of the matrix B (i.e. the number of rows and columns respectively), then values of the B matrix will follow (row after row, listing elements from left to right). You can assume that the 4 numbers representing A and B matrix-sizes will be positive numbers (no need for validity check).
An example file-content is given below. Note that the data are shown in a clear and visually convenient look; however, there is no formatting requirement for the file, i.e. you should not assume/expect any mandatory line-breaks or empty lines.
- 3
1 2 3
4 5 6
- 3
1 2 3
- 5 6
7 8 9 //No specific formatting: e.g. data may be saved all on one line: 2 3 1 2 3 4 5 6 3 3 1 2 3 4 5 6 7 8 9 // or they can be broken into lines in an arbitrary manner
Dr. Hasmik Gharibyan; CSC/CPE 349
- Call matrixProduct method giving A and B matrices, and get the product-matrix (matrix C).
Note: be aware of a potential IllegalArgumentException from matrixProduct; if this happens, arrange to catch it and output an error message. You should not let the program crash.
- Output the content of matrix C on the screen, in an appropriate matrix-format: one line per matrix-row, separating values on the same row with space(s).
For the above example your output should look like this (only bold italic text is the output):
Product matrix:
30 36 42 //c1,1=11+24 + 37=30 c1,2=12+25 + 38=36 c1,3=13+26 + 39=42
66 81 96 //c2,1=41+54 + 67=66 c2,2=42+55 + 68=81 c2,3=43+56 + 69=96
Compile and run your program, test it thoroughly:
Make sure the program works as expected and gives correct results (there are many online tools for matrix-multiplication so you can use one of them to check your data).
Try different size input matrices, including matrices that have only one row or only one column. Also make sure that your program works as expected when input matrices are not eligible/valid for matrix-multiplication (they do not meet the above mentioned size-requirement).
Reviews
There are no reviews yet.