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...

A(n) ________ is the site that comes built into the Office 365 interface

Fill in the blank(s) with correct word

Computer Science & Information Technology

Where is the New Cell Style command?

A) On the Insert tab B) On the Themes gallery C) On the Styles Gallery D) On the Page Layout tab

Computer Science & Information Technology

The ____________________ and ostream classes provide the data declarations and methods used for data input and output, respectively.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

What is Dumpster diving?

A. An attack in which an attacker goes through the files kept in a recycle bin folder on a computer system. B. It's a diving sport. C. An attack in which an attacker goes through a firm’s trash bins looking for documents, backup tapes, floppy disks, and other information-carrying media. D. An attack in which an attacker uses a software to search memory garbage data.

Computer Science & Information Technology