CSc Fall 2005 Homework 4

Section P (Tuesday and Thursday): due at beginning of class Thursday, October 20.

Section G (Monday and Wednesday): due at beginning of class Wednesday, October 19.

At the end of this page is most of the code for a Complex class that represents a complex number as a pair of doubles. There are four constructors, most of the arithmetic operations, and an override of the toString method.

1. The implementation of the subtract, divide, and divideD methods have been removed. You write them.

2. Modify your methods for computing the sine and cosine, from HW3, to apply to complex numbers. This requires replacing all arithmetic operations with method calls from the Complex class.

3. The complex  sine and cosine can also be computed using these formulas:

4. You now have two ways to compute the sine and cosine of a complex number. Write a JUnit class to demonstrate that both methods give the same values, within a tolerance delta that allows for rounding and other numerical errors. There is no overloaded assertEqual that takes two complex numbers, of  course, since complex numbers are not provided in the Java API. But you can compute actual using the formulas in 3) and the expected from your complex sine or cosine, and write:

assertEquals(actual.getReal(), expected.getReal(), 1E-14);
assertEquals(actual.getImag(), expected.getImag(), 1E-14);

5. Write nested for loops to run the real part of a complex number from -2 to +2 and similarly for the imaginary part. (Added Oct 17: ) Go in steps of 0.2 on each. For each generated value, compute the complex sine and cosine both ways, and use assertEquals to  show that they are the same (within delta, of course). Experiment to see how small you can make delta; you can probably get closer than 1E-14.

6. For each generated value in 5), compute sin2 + cos2, and compare the result with the complex number 1 + 0i.

 

Here is the complex class, with the implementations of three methods removed. The spacing may look strange because FrontPage doesn't know much about tabs. Just copy and paste, Select All, then do Source/Format, or Shift+Control+F. 

 

 

public class Complex {
	private double real; // real part of the complex number

	private double imag; // imag part of the complex number

	// no-argument constructor, initializes this Complex to 0 + 0i
	public Complex() {
		real = 0.0;
		imag = 0.0;
	} // end Complex no-argument constructor

	// initialize with given args
	public Complex(double theReal, double theImag) {
		real = theReal;
		imag = theImag;
	} // end two-argument constructor
	
	// initialize with given real, set imag to zero
	public Complex(double theReal) {
		real = theReal;
		imag = 0.0;
	} // end one-argument constructor
	
	// initialize with a Ccmplex
	public Complex(Complex z){
		real = z.getReal();
		imag = z.getImag();
	}

	// Get the real part of a complex number
	public double getReal() {
		return real;
	} // end method getReal

	// Get the imaginary part of a complex number
	public double getImag() {
		return imag;
	} // end method getImag

	// Set the real part of a complex number
	public void setReal(double newRealValue) {
		real = newRealValue;
	} // end method setReal

	// Set the imaginary part of a complex number
	public void setImag(double newImagValue) {
		imag = newImagValue;
	} // end method setImag

	// add two Complex numbers
	public Complex add(Complex right) {
		double resultReal = real + right.real;
		double resultImag = imag + right.imag;
		return new Complex(resultReal, resultImag);
	} // end method sum

	// add a real double to a complex
	public Complex addD(double theReal) {
		double resultReal = real + theReal;
		double resultImag = imag;
		return new Complex(resultReal, resultImag);
	} // end method addD

	// subtract two Complex numbers
	// DELETED; YOU WRITE THIS

	// multiply two Complex numbers
	public Complex multiply(Complex right) {
		double resultReal = real * right.real - imag * right.imag;
		double resultImag = imag * right.real + real * right.imag;
		return new Complex(resultReal, resultImag);
	} // end method multiply

	// multiply a Complex by a double
	public Complex multiplyD(double theReal) {
		double resultReal = real * theReal;
		double resultImag = imag * theReal;
		return new Complex(resultReal, resultImag);
	} // end method multiplyD

	// divide two Complex numbers
	// DELETED; YOU WRITE THIS
	
//	 divide a Complex by a double
	// DELETED; YOU WRITE THIS

	// return String representation of a Complex number
	public String toString() {
		return real + " + " + imag + "i";
	} // end method toString

} // end class Complex


 

 Back to Dan McCracken's  Home Page