JAVA PROGRAMING Complex Matrix Use the ComplexClass class attached to develop th
ID: 3573460 • Letter: J
Question
JAVA PROGRAMING
Complex Matrix
Use the ComplexClass class attached to develop the ComplexMatrix class for performing matrix operations involving complex numbers. The ComplexMatrixclass should extend the GenericMatrix class and implement the add, multiple, and zero methods. You need to modify GenericMatrix and replace every occurrence of Number by object, because ComplexClass is not a subtype of Number. Write a test program that creates the following tow matrices and displays the result of additions and multiplication of the matrices by invoking the printResult method. DON'T FORGET TO COMMENT YOUR CODE
output:
m1 m2 is 1.0 5.0i 1.0 6.0i 1.0 7.0i 1.0 6.0i 1.0 7.0i 1.0 B.0i 2.0 11.0i 2.0 13.0i 2.0 15.0i 2.0 5. i 2.0 6.0 i 2.0 7.0i 0 6.0i 2.0 7.0 i 2.0 8.0i E 4.0 11.0i 4.0 13.0i 4.0 15.0 3.0 5.0i 3.0 6.0i 3.0 7.0i 3.0 6.0i 3.0 7.0i 3.0 8.0i 6.0 11.0i 6.0 i 6.0 15.0i m1 m2 is 1.0 7.0i 1.0 5.0i 1.0 6.0i 1.0 6.0 i 1.0 7.0i 1.0 8.0i 102.0 5 6.0i -120.0 59.0 i -138.0 (62.0i 2.0 5.0i 2.0 6.0 i 2.0 7.0i. 2.0 6.0i 2.0 7.0i 2.0 8.0i -96.0 74. 0i -1 14.0 80.0i -132.0 86.0i 3.0 5.0 i 3.0 6.0i 3.0 7.0i 3.0 6.0i 3.0 7.0i 3.0 8.0i -90.0 92.0i -108.0 101. 0i -126.0 110.0iExplanation / Answer
Exercise.java
public class Exercise {
public static void main(String[] args) {
// Create two Complex arrays m1 and m2
Complex[][] m1 = new Complex[3][3];
Complex[][] m2 = new Complex[3][3];
for (int i = 0; i < m1.length; i++)
for (int j = 0; j < m1[0].length; j++) {
m1[i][j] = new Complex(i + 1, j + 5);
m2[i][j] = new Complex(i + 1, j + 6);
}
// Create an instance of ComplexMatrix
ComplexMatrix complexMatrix = new ComplexMatrix();
System.out.println(" m1 + m2 is ");
GenericMatrix.printResult(
m1, m2, complexMatrix.addMatrix(m1, m2), '+');
System.out.println(" m1 * m2 is ");
GenericMatrix.printResult(
m1, m2, complexMatrix.multiplyMatrix(m1, m2), '*');
}
}
ComplexMatrix.java
public class ComplexMatrix extends GenericMatrix<Complex> {
@Override /** Add two complex numbers */
protected Complex add(Complex c1, Complex c2) {
return c1.add(c2);
}
@Override /** Multiply two complex numbers */
protected Complex multiply(Complex c1, Complex c2) {
return c1.multiply(c2);
}
@Override /** Specify zero for a complex number */
protected Complex zero() {
return new Complex();
}
}
Complex.java
public class Complex implements Cloneable {
private double a;
private double b;
// Constructors
/** Creates a complex object for number 0 */
public Complex() {
this(0, 0);
}
/** Create a complex object with 0 for b */
public Complex(double a) {
this(a, 0);
}
/** Creates a complex object with specified a and b */
public Complex(double a, double b) {
this.a = a;
this.b = b;
}
// Methods
/** Return real part of complex number */
public double getRealPart() {
return a;
}
/** Return imaginary part of complex number */
public double getImaginaryPart() {
return b;
}
/** Add a complex number to this complex number */
public Complex add(Complex secondComplex) {
return new Complex(a + secondComplex.a,
b + secondComplex.b);
}
/** Subtract a complex number from this complex number */
public Complex subtract(Complex secondComplex) {
return new Complex(a - secondComplex.a,
b - secondComplex.b);
}
/** Multiply a complex number by this complex number */
public Complex multiply(Complex secondComplex) {
return new Complex(a * secondComplex.a - b * secondComplex.b,
b * secondComplex.a + a * secondComplex.b);
}
/** Divide a complex number by this complex number */
public Complex divide(Complex secondComplex) {
return new Complex((a * secondComplex.a + b * secondComplex.b) /
(Math.pow(secondComplex.a, 2) + Math.pow(secondComplex.b, 2)),
(b * secondComplex.a - a * secondComplex.b) /
(Math.pow(secondComplex.a, 2) + Math.pow(secondComplex.b, 2)));
}
/** Returns the absolute value of this complex number */
public double abs() {
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
@Override /** Override the protectec clone method defined in
the Object class, and strengthen its accexxibility */
public Complex clone() throws CloneNotSupportedException {
return (Complex)super.clone();
}
@Override /** Retrun a string description
of this complex number */
public String toString() {
return b == 0 ? a + "" : "(" + a + " + " + b + "i)";
}
}
GenericMatrix.java
public abstract class GenericMatrix<E> {
/** Abstract method for adding two elements of the matrices */
protected abstract E add (E o1, E o2);
/** Abstract method for multiplying two elements of the matrices */
protected abstract E multiply(E o1, E o2);
/** Abstract method for defining zero for the matrix element */
protected abstract E zero();
/** Add two matrices */
public E[][] addMatrix(E[][] matrix1, E[][] matrix2) {
// Check bounds of the two matrices
if ((matrix1.length != matrix2.length) ||
(matrix1[0].length != matrix2[0].length)) {
throw new RuntimeException(
"The matrices do not have the same size");
}
E[][] result =
(E[][])new Object[matrix1.length][matrix1[0].length];
// Perform addition
for (int i = 0; i < result.length; i++)
for (int j = 0; j < result[i].length; j++) {
result[i][j] = add(matrix1[i][j], matrix2[i][j]);
}
return result;
}
/** Multiply two matrices */
public E[][] multiplyMatrix(E[][] matrix1, E[][] matrix2) {
// Check bounds
if (matrix1[0].length != matrix2.length) {
throw new RuntimeException(
"The matrices do not have compatible size");
}
// Create result matrix
E[][] result =
(E[][])new Object[matrix1.length][matrix2[0].length];
// Perform multiplication of two matrices
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
result[i][j] = zero();
for (int k = 0; k < matrix1[0].length; k++) {
result[i][j] = add(result[i][j],
multiply(matrix1[i][k], matrix2[k][j]));
}
}
}
return result;
}
/** Print matrices, the operator, and their operation result */
public static void printResult(
Object[][] m1, Object[][] m2, Object[][] m3, char op) {
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(" " + op + " ");
else
System.out.print(" ");
for (int j = 0; j < m2.length; j++)
System.out.print(" " + m2[i][j]);
if (i == m1.length / 2)
System.out.print(" = ");
else
System.out.print(" ");
for (int j = 0; j < m3.length; j++)
System.out.print(m3[i][j] + " ");
System.out.println();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.