COMPUTER 2430 Object Oriented Programming and Data Structures I
Complex Numbers Real numbers Imaginary numbers 5i i = i * i = -1 a + bi, where both a and b are real numbers 3.1415926… -2 -1 1 2
Complex Numbers b a a + bi
Operations on Complex Numbers (a1 + b1i) + (a2 + b2i) = (a1 + a2) + (b1 + b2)i (a1 + b1i) - (a2 + b2i) = (a1 - a2) + (b1 - b2)i (a1 + b1i) * (a2 + b2i) = (a1 * a2 - b1 * b2) + (a1 * b2 + a2 * b1)i (a1 + b1i) / (a2 + b2i) = ((a1 + b1i) * (a2 - b2i)) / ((a2 + b2i) * (a2 - b2i)) (a2 + b2i) * (a2 - b2i) = (a2 * a2 + b2 * b2) Conjugate of a + bi is a – bi Magnitude of a + bi is
Complex Numbers b a a + bi
ADT: Complex Number Data Two double (float): real and imag String representation (real, imag) Could be a + bi Or other formats
Complex Class public class Complex { private double real, imag; public Complex() // Default constructor real = imag = 0.0; } public Complex( Complex z ) // Copy constructor real = z.real; // this.real = z.real; imag = z.imag; // this.imag = z.imag; ...
More Constructors public class Complex { private double real, imag; ... public Complex( double realPart, double imagPart ) real = realPart; imag = imagPart; } public Complex( double realPart ) imag = 0.0;
public class Complex { private double real, imag; public Complex( double realPart, double imagPart ) real = realPart; imag = imagPart; } /** Does not change either instance, this or parameter z. Usage: z3 = z1.plus(z2); */ public Complex plus ( Complex z ) return new Complex( real + z.real, imag + z.imag );
public class Complex { private double real, imag; public Complex( double realPart, double imagPart ) . . . } /** Does not change either instance, this or parameter z. */ public Complex plus ( Complex z ) return new Complex( this.real + z.real, this.imag + z.imag );
Override Method toString public class Complex { private double real, imag; ... /** Example: (3.4, 12.8) */ @Override public String toString() return "(" + real + ", " + imag + ")"; }
Override Method equals public class Complex { private double real, imag; @Override public boolean equals ( ? ) }
Override Method equals public class Complex { private double real, imag; @Override public boolean equals ( Object obj ) if ( obj instanceof Complex ) Complex temp = (Complex) obj; return (real == temp.real && imag == temp.imag); } return false;
public class Complex { private double real, imag; . . . public Complex divides ( Complex z ) public Complex conjugate() public double magnitude() public boolean lessThan( Complex z ) public boolean hasSameMagnitude( Complex z ) }
Test 1 Wednesday, October 10 Note01 - Note14 50 points
Program 2 Due 10 pm, October 5 Follow instructions -1 by 10 pm, October 6 -5 by 10 pm, October 8 Follow instructions Individual Assignment Do it yourself!
Program 3 Due Wednesday, October 24 Lab 4 Lab 5 Part of Prog3 Due 10 pm, Monday, October 15 Lab 5 Due 10 pm, Wednesday, October 17