Create a class called Complex for performing arithmetic with complex numbers. Write a driver program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is ?-1

Use floating-point numbers to represent the data of the class. Provide a constructor that enables an object of this class to be initialized when it is created. The constructor should contain default values in case no initializers are provided. Provide methods for each of the following:
a) Adding two ComplexNumbers: The real parts are added together to form the real part of the result, and the imaginary parts are added together to form the imaginary part of the result.
b) Subtracting two ComplexNumbers: The real part of the right operand is subtracted from the real part of the left operand to form the real part of the result, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand to form the imaginary part of the result.
c) Printing ComplexNumbers in the form (a, b), where a is the real part and b is the imaginary part.

```
# Complex number class.

class ComplexNumber:
"""Complex numbers of the form realPart + imaginaryPart * i"""

def __init__( self, real = 0.0, imaginary = 0.0 ):
"""Initializes ComplexNumber object"""

self.realPart = float( real )
self.imaginaryPart = float( imaginary )

def add( self, cNumber ):
"""Returns sum of two ComplexNumber objects"""

real = self.realPart + cNumber.realPart
imaginary = self.imaginaryPart + cNumber.imaginaryPart

# create and return new complexNumber object
return ComplexNumber( real, imaginary )

def subtract( self, cNumber ):
"""Returns difference of two ComplexNumber objects"""

real = self.realPart - cNumber.realPart
imaginary = self.imaginaryPart - cNumber.imaginaryPart

# create and return new complexNumber object
return ComplexNumber( real, imaginary )

def printComplex( self ):
"""Prints ComplexNumber in form
( realPart, imaginaryPart )"""

print "(%f, %f)" % ( self.realPart, self.imaginaryPart )
# Exercise 7.3: ex07_03.py
# Driver to test class Complex.

from Complex import ComplexNumber

print "Testing the complex numbers class"

complexNumber1 = ComplexNumber( 5.0, 2.0 )
complexNumber2 = ComplexNumber( 3.0, 4.0 )
defaultComplexNumber = ComplexNumber()

# demonstrate default constructor arguments
print "defaultComplexNumber is",
defaultComplexNumber.printComplex()

# demonstrate ComplexNumber addition
sum = complexNumber1.add( complexNumber2 )
print "The sum of complexNumber1 and complexNumber2 is",
sum.printComplex()

# demonstrate ComplexNumber subtraction
difference = complexNumber1.subtract( complexNumber2 )
print "The difference of complexNumber1 and complexNumber2 is",
difference.printComplex()
```
Testing the complex numbers class
defaultComplexNumber is (0.000000,0.000000)
The sum of complexNumber1 and complexNumber2 is (8.000000,6.000000)
The difference of complexNumber1 and complexNumber2 is (2.000000,-
2.000000)

Computer Science & Information Technology

You might also like to view...

Flickr offers fewer print services and instead generates income from selling advertisements on its website

Indicate whether the statement is true or false

Computer Science & Information Technology

When the mouse is placed in the column heading between the border of columns B and C and is dragged to the right, column B becomes narrower

Indicate whether the statement is true or false.

Computer Science & Information Technology