Q:

(ComplexMatrix) Use the Complex class introduced in Programming Exercise 13.17 to develop the ComplexMatrix class for performing matrix operations involving complex numbers

0

1 (ComplexMatrix) Use the Complex class introduced in Programming Exercise 13.17 to develop the ComplexMatrix class for performing matrix operations involving complex numbers. The ComplexMatrix class 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 Complex is not a subtype of Number. Write a test program that creates the following two matrices and displays the result of addition and multiplication of the matrices by invoking the printResult method.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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)";
	}
}

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();
	}
}

Exercise_19_11.java

/*********************************************************************************
* (ComplexMatrix) Use the Complex class introduced in Programming Exercise 13.17 *
* to develop the ComplexMatrix class for performing matrix operations involving  *
* complex numbers. The ComplexMatrix class 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        *
* Complex is not a subtype of Number. Write a test program that creates the      *
* following two matrices and displays the result of addition and multiplication  *
* of the matrices by invoking the printResult method.                            *
*********************************************************************************/
public class Exercise_19_11 {
	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("\nm1 + m2 is ");
		GenericMatrix.printResult(
			m1, m2, complexMatrix.addMatrix(m1, m2), '+');

		System.out.println("\nm1 * m2 is ");
		GenericMatrix.printResult(
			m1, m2, complexMatrix.multiplyMatrix(m1, m2), '*');
 	}
}

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();
		}
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now