Lab #3b:Neo Math Submit the following in Canvas: 1-Output (your output cut-and-p
ID: 3746436 • Letter: L
Question
Lab #3b:Neo Math Submit the following in Canvas: 1-Output (your output cut-and-pasted into a text file called ScreenlO.txt created in NetBeans) 2- Source Code (your Matrix.java and testMatrix.java source files) Two matrices can be added or subtracted if they have the same size. Suppose A (au] and B- lbu] are two matrices of the size mxn, in which ay denotes the element of A in the ith row and the jth column, and so on. The sum and difference of A and B are given by: The multiplication of A and B (A B) is defined only if the number of columns of A is the same as the number of rows of B if A is of the size m x n and B is of the size n t, the A-B-C (represented by Ical) is of the size m x t and the element cia is given by the formula: C anbk+aabz+.ainbnk Create a class Matrix with the following: // data members private final int (1values private final String name: holds the matrix values // constructors public Matrix (String name, int1 values) public Matrix(int[101 values) II name is set to null // nethods publio Matrix add (Matrix mIreturns null if operation is not possible else this plus public Matrix sub (Matrix m) returns nul1 if operation is not possible else this minus m public Matrix mult(Matrix m)1) returns null if operation is not possible else this times m publie string getane II returns the matrix name // tostring override. Make sure the 80verride annotation is there too / Use a StringBuffer to construct the matrix output with a Formatter to use a field width of 3 /l for each matrix value and with each value on a row separated by one space. If name is not // null, then output the name with the format name: on the first output 1ine. Output the matrix /I values as a 2D table with aforementioned formatting. Note that with this override, you can // simply output a Natrix m using System.out.peintin (n)a Override publie String tostring) I/ output name and values in a table - see example Create a test class TestMatrix (in the Netbeans Test Packages source folder) that reads up to ten (10) integer matrices from input file matrices.txt and creates an array of Matrix objects that represent them (hint: you should use a while loop for this!). Each matrix in the input file consists of a line with the matrix name, number of rows and number of columns, followed by a line for each row. Note that you Page 1 of 4Explanation / Answer
matrices.txt
A 3 2
1 2
3 4
5 6
B 3 2
5 6
7 8
0 0
C 2 3
1 1 1
2 8 9
Hi 2 4
1 1 1 0
2 8 9 0
______________
Matrix.java
import java.util.Arrays;
public class Matrix {
private final int[][] values;
private final String name;
public Matrix(String name, int[][] values) {
this.values = values;
this.name = name;
}
public Matrix(int[][] values) {
this.values = values;
this.name = null;
}
public Matrix add(Matrix m) {
if (values.length == m.values.length && values[0].length == m.values[0].length) {
int matrix[][] = new int[m.values.length][m.values[0].length];
for (int i = 0; i < m.values.length; i++) {
for (int j = 0; j < m.values[0].length; j++) {
matrix[i][j] = values[i][j] + m.values[i][j];
}
}
Matrix res = new Matrix(matrix);
return res;
} else {
return null;
}
}
public Matrix sub(Matrix m) {
if (values.length == m.values.length && values[0].length == m.values[0].length) {
int matrix[][] = new int[m.values.length][m.values[0].length];
for (int i = 0; i < m.values.length; i++) {
for (int j = 0; j < m.values[0].length; j++) {
matrix[i][j] = values[i][j] - m.values[i][j];
}
}
Matrix res = new Matrix(matrix);
return res;
} else {
return null;
}
}
public Matrix mult(Matrix m) {
if (values[0].length == m.values.length) //|| values.length==m.values.length && values[0].length==m.values[0].length)
{
int sum = 0;
int matrix3[][] = new int[values.length][m.values[0].length];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < m.values[0].length; j++) {
for (int k = 0; k < m.values.length; k++) {
sum = sum + values[i][k] * m.values[k][j];
}
matrix3[i][j] = sum;
sum = 0;
}
}
Matrix res = new Matrix(matrix3);
return res;
} else {
return null;
}
}
public String getname() {
return name;
}
@Override
public String toString() {
if (name != null) {
System.out.println(name + ":");
}
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[0].length; j++) {
System.out.print(values[i][j] + " ");
}
System.out.println();
}
return "";
}
}
__________________
Test.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int cnt = 0, row, col;
String name;
Matrix m[] = new Matrix[10];
try {
Scanner sc = new Scanner(new File("matrices.txt"));
while (sc.hasNext() && cnt < 10) {
name = sc.next();
row = sc.nextInt();
col = sc.nextInt();
int arr[][] = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
arr[i][j] = sc.nextInt();
}
}
if (cnt < 10)
if (name != null)
m[cnt++] = new Matrix(name, arr);
else
m[cnt++] = new Matrix(arr);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Number of Matrices read is :" + cnt);
for (int i = 0; i < cnt; i++)
System.out.println(m[i]);
//A+B
System.out.println(m[0].getname() + " + " + m[1].getname() + " =");
Matrix ares01 = m[0].add(m[1]);
if (ares01 != null)
System.out.println(ares01);
else
System.out.println("This operation is not possible for these operations!");
//A-B
System.out.println(m[0].getname() + " - " + m[1].getname() + " =");
Matrix sres01 = m[0].sub(m[1]);
if (sres01 != null)
System.out.println(sres01);
else
System.out.println("This operation is not possible for these operations!");
//A*B
System.out.println(m[0].getname() + " * " + m[1].getname() + " =");
Matrix mres01 = m[0].mult(m[1]);
if (mres01 != null)
System.out.println(mres01);
else
System.out.println("This operation is not possible for these operations!");
//A+C
System.out.println(m[0].getname() + " + " + m[2].getname() + " =");
Matrix ares02 = m[0].add(m[2]);
if (ares02 != null)
System.out.println(ares02);
else
System.out.println("This operation is not possible for these operations!");
//A-C
System.out.println(m[0].getname() + " - " + m[2].getname() + " =");
Matrix sres02 = m[0].sub(m[2]);
if (sres02 != null)
System.out.println(sres02);
else
System.out.println("This operation is not possible for these operations!");
//A*C
System.out.println(m[0].getname() + " * " + m[2].getname() + " =");
Matrix mres02 = m[0].mult(m[2]);
if (mres02 != null)
System.out.println(mres02);
else
System.out.println("This operation is not possible for these operations!");
//A+Hi
System.out.println(m[0].getname() + " + " + m[3].getname() + " =");
Matrix ares03 = m[0].add(m[3]);
if (ares03 != null)
System.out.println(ares03);
else
System.out.println("This operation is not possible for these operations!");
//A-Hi
System.out.println(m[0].getname() + " - " + m[3].getname() + " =");
Matrix sres03 = m[0].sub(m[3]);
if (sres03 != null)
System.out.println(sres03);
else
System.out.println("This operation is not possible for these operations!");
//A*Hi
System.out.println(m[0].getname() + " * " + m[3].getname() + " =");
Matrix mres03 = m[0].mult(m[3]);
if (mres03 != null)
System.out.println(mres03);
else
System.out.println("This operation is not possible for these operations!");
//B+C
System.out.println(m[1].getname() + " + " + m[2].getname() + " =");
Matrix ares12 = m[1].add(m[2]);
if (ares12 != null)
System.out.println(ares12);
else
System.out.println("This operation is not possible for these operations!");
//B-C
System.out.println(m[1].getname() + " - " + m[2].getname() + " =");
Matrix sres12 = m[1].sub(m[2]);
if (sres12 != null)
System.out.println(sres12);
else
System.out.println("This operation is not possible for these operations!");
//B*C
System.out.println(m[1].getname() + " * " + m[2].getname() + " =");
Matrix mres12 = m[1].mult(m[2]);
if (mres12 != null)
System.out.println(mres12);
else
System.out.println("This operation is not possible for these operations!");
//B+Hi
System.out.println(m[1].getname() + " + " + m[3].getname() + " =");
Matrix ares13 = m[1].add(m[3]);
if (ares13 != null)
System.out.println(ares13);
else
System.out.println("This operation is not possible for these operations!");
//B-Hi
System.out.println(m[1].getname() + " - " + m[3].getname() + " =");
Matrix sres13 = m[1].sub(m[3]);
if (sres13 != null)
System.out.println(sres13);
else
System.out.println("This operation is not possible for these operations!");
//B*Hi
System.out.println(m[1].getname() + " * " + m[3].getname() + " =");
Matrix mres13 = m[1].mult(m[3]);
if (mres13 != null)
System.out.println(mres13);
else
System.out.println("This operation is not possible for these operations!");
//C+Hi
System.out.println(m[2].getname() + " + " + m[3].getname() + " =");
Matrix ares23 = m[2].add(m[3]);
if (ares23 != null)
System.out.println(ares23);
else
System.out.println("This operation is not possible for these operations!");
//C-Hi
System.out.println(m[2].getname() + " - " + m[3].getname() + " =");
Matrix sres23 = m[2].sub(m[3]);
if (sres23 != null)
System.out.println(sres23);
else
System.out.println("This operation is not possible for these operations!");
//C*Hi
System.out.println(m[2].getname() + " * " + m[3].getname() + " =");
Matrix mres23 = m[2].mult(m[3]);
if (mres23 != null)
System.out.println(mres23);
else
System.out.println("This operation is not possible for these operations!");
}
}
__________________
Output:
Number of Matrices read is :4
A:
1 2
3 4
5 6
B:
5 6
7 8
0 0
C:
1 1 1
2 8 9
Hi:
1 1 1 0
2 8 9 0
A + B =
6 8
10 12
5 6
A - B =
-4 -4
-4 -4
5 6
A * B =
This operation is not possible for these operations!
A + C =
This operation is not possible for these operations!
A - C =
This operation is not possible for these operations!
A * C =
5 17 19
11 35 39
17 53 59
A + Hi =
This operation is not possible for these operations!
A - Hi =
This operation is not possible for these operations!
A * Hi =
5 17 19 0
11 35 39 0
17 53 59 0
B + C =
This operation is not possible for these operations!
B - C =
This operation is not possible for these operations!
B * C =
17 53 59
23 71 79
0 0 0
B + Hi =
This operation is not possible for these operations!
B - Hi =
This operation is not possible for these operations!
B * Hi =
17 53 59 0
23 71 79 0
0 0 0 0
C + Hi =
This operation is not possible for these operations!
C - Hi =
This operation is not possible for these operations!
C * Hi =
This operation is not possible for these operations!
___________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.