COMPUTER 2430 Object Oriented Programming and Data Structures
COMPUTER 2430 Object Oriented Programming and Data Structures I 1
Complex Numbers Real numbers 3. 1415926… -2 -1 0 1 2 Imaginary numbers 5 i i = i * i = -1 Complex numbers a + bi, where both a and b are real numbers 2
Complex Numbers b a a + bi 3
Operations on Complex Numbers (a 1 + b 1 i) + (a 2 + b 2 i) = (a 1 + a 2) + (b 1 + b 2)i (a 1 + b 1 i) - (a 2 + b 2 i) = (a 1 - a 2) + (b 1 - b 2)i (a 1 + b 1 i) * (a 2 + b 2 i) = (a 1 * a 2 - b 1 * b 2) + (a 1 * b 2 + a 2 * b 1)i (a 1 + b 1 i) / (a 2 + b 2 i) = ((a 1 + b 1 i) * (a 2 - b 2 i)) / ((a 2 + b 2 i) * (a 2 - b 2 i)) (a 2 + b 2 i) * (a 2 - b 2 i) = (a 2 * a 2 + b 2 * b 2) Conjugate of a + bi is a – bi Magnitude of a + bi is 4
Complex Numbers b a a + bi 5
ADT: Complex Number Data Two double (float): real and imag String representation (real, imag) Could be a + bi Or other formats 6
Complex Class public class Complex { private double real, imag; public Complex() { real = imag = 0. 0; } // Default constructor public Complex( Complex z ) { real = z. real; imag = z. imag; }. . . // Copy constructor // this. real = z. real; // this. imag = z. imag; } 7
More Constructors public class Complex { private double real, imag; . . . public Complex( double real. Part, double imag. Part ) { real = real. Part; imag = imag. Part; } public Complex( double real. Part ) { real = real. Part; imag = 0. 0; }. . . } 8
public class Complex { private double real, imag; public Complex( double real. Part, double imag. Part ) { real = real. Part; imag = imag. Part; } /** Does not change either instance, this or parameter z. Usage: z 3 = z 1. plus(z 2); */ public Complex plus ( Complex z ) { return new Complex( real + z. real, imag + z. imag ); } } 9
public class Complex { private double real, imag; public Complex( double real. Part, double imag. Part ) {. . . } /** 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 ); } } 10
Override Method to. String public class Complex { private double real, imag; . . . /** Example: (3. 4, 12. 8) */ @Override public String to. String() { return "(" + real + ", " + imag + ")"; } } 11
Override Method equals public class Complex { private double real, imag; @Override public boolean equals ( ? ) { } } 12
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; } } 13
public class Complex { private double real, imag; . . . public Complex divides ( Complex z ) public Complex conjugate() public double magnitude() public boolean less. Than( Complex z ) public boolean has. Same. Magnitude( Complex z ) } 14
Test 1 • Wednesday, October 10 • Note 01 - Note 14 • 50 points 15
Program 2 • Due 10 pm, October 5 – -1 by 10 pm, October 6 – -5 by 10 pm, October 8 • Follow instructions • Individual Assignment – Do it yourself! 16
Program 3 • Due Wednesday, October 24 • Lab 4 – Part of Prog 3 – Due 10 pm, Monday, October 15 • Lab 5 – Part of Prog 3 – Due 10 pm, Wednesday, October 17 17
- Slides: 17