Java Write a method to add two matrices and a method to multiply two matrices. T
ID: 3816652 • Letter: J
Question
Java
Write a method to add two matrices and a method to multiply two matrices. The headers of the two methods are:
public static double [] [] multiplyMatrix(double[][] a, double[][] b)
public static double [] [] addMatrix(double[][] a, double[][] b)
Write a test program that prompts the user to enter three 3*3 matrices and displays output.
Example:
Using this method for printing:
public static void printResult(
double[][] m1, double[][] m2, double[][] m3, double[][] resultMatrix, char op1, char op2) {
for (int i = 0; i < m1.length; i++) {
for (int j = 0; j < m1[0].length; j++)
System.out.print(" " + m1[i][j]);
if (i == m1.length / 2)
System.out.print( " " + op1 + " " );
else
System.out.print( " " );
for (int j = 0; j < m2[0].length; j++)
System.out.print(" " + m2[i][j]);
if (i == m1.length / 2)
System.out.print( " " + op2 + " " );
else
System.out.print( " " );
for (int j = 0; j < m3[0].length; j++)
System.out.print(" " + m3[i][j]);
if (i == m1.length / 2)
System.out.print( " = " );
else
System.out.print( " " );
for (int j = 0; j < resultMatrix[0].length; j++)
System.out.print(" " + resultMatrix[i][j]);
System.out.println();
}
}
Enter matrix 1: 1 2 3 4 5 6 7 8 9 Enter matrix 2: 9 8 7 6 5 4 3 2 1 Enter matrix 3: 0 2 4 1 4.5 2.2 1.1 4.3 5.2 The output is 1.0 2.0 3.0 0.0 2.0 4.0 9.0 8.0 7.0 30.0 26.0 22.0 4.0 5.0 6.0 6.0 5.0 4.0 1.0 4.5 2.2 85.0 73.5 56.2 1.1 4.3 5.2 7.0 8.0 9.0 3.0 2.0 1.0 139.1 118.3 95.2Explanation / Answer
MatrixUtils.java
====
public class MatrixUtils {
// Matrices.
private double[][] matA = new double[3][3];
private double[][] matB = new double[3][3];
/**
* Converts an array to matrix / 2-d Array.
* @param {double[]} arr.
* @return {double[][]}
*/
public static double[][] ArrayToMatrix( double[] arr ) {
int k = 0;
double[][] mat = new double[3][3];
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j) {
mat[i][j] = arr[k];
++k;
}
return mat;
};
public static void printMatrix( double[][] mat ) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
System.out.printf(" %-4.4f ", mat[i][j]);
}
System.out.println();
}
};
/** CONSTRUCTOR **/
public MatrixUtils( double[] a, double[] b ) {
matA = ArrayToMatrix( a );
matB = ArrayToMatrix( b );
};
/**
* Add matrices matA and matB.
* @return {double[][]} Result of adding two matrices.
*/
public double[][] add() {
double[][] res = new double[3][3];
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
res[i][j] = matA[i][j] + matB[i][j];
}
}
return res;
};
/**
* Multiplies matrices matA and matB.
*/
public double[][] multiply() {
double[][] res = new double[3][3];
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
res[i][j] =
(matA[i][0] * matB[0][j]) +
(matA[i][1] * matB[1][j]) +
(matA[i][2] * matB[2][j]);
}
}
return res;
};
}
MatrixDriver.java
====
import java.util.Scanner;
public class MatrixDriver {
public static void main(String[] args) {
double[] a = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.9, 7.3, 8.3, 9.9 };
double[] b = a;
MatrixUtils m1 = new MatrixUtils( a, b );
double[][] res = m1.multiply();
MatrixUtils.printMatrix(res);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.