Question
Use Python: Open the starter code in the file complex_numbers.py class ComplexNumber(object): def __init__(self,real=0, imag=0): self.real = real self.imag = imag def __str__(self): return {0}+{1}j.format(self.real,
Use Python:
Open the starter code in the file complex_numbers.py
class ComplexNumber(object): def __init__(self,real=0, imag=0): self.real = real self.imag = imag def __str__(self): return "{0}+{1}j".format(self.real, self.imag) # complete this stub method for __add__() def __add__(self, other): real = 0 # fix this stub imag = 0 # fix this stub return ComplexNumber(real, imag) # complete this stub method for add() def __mul__(self, other): return ComplexNumber(0, 0) # fix this stub if __name__ == "__main__": # 1. Create a new ComplexNumber object c1 = ComplexNumber(3,4) # Print out the first complex number c1. Note this automatically calls the __str__ magic method. print("Complex number c1 = ", c1) # 2. Create another ComplexNumber object equal to 5. Provide only one argument! c2 = ComplexNumber(real=5) # Print out the second complex number c2. print("Complex number c2 = ", c2) # 3. Create another ComplexNumber object equal to 6i. Provide only one argument! # add code here # Print out the third complex number c3. # add code here print(" Complex Arithmetic") # 4. Add c1 and c2 using the + operator defined by the magic method __add__ print("The sum of c1 and c2 is:", c1 + c2) # 5. Multiply c1 and c2 using the * operator defined by the magic method __mul__ # add code here.
The constructor is interesting in that it uses optional arguments for both the real and imaginary components. Methods like __init__ that start with two underscores (dunder methods) are called magic methods. The starter code provides a second magic method __str__ that overrides the default __str__method in "object" and can be used to print any complex number as demonstrated in the main method.
Above, r and i are keywords that the constructor recognizes.
a. In the main section, construct the complex number c2 = 5 with zero imaginary part. Only provide one argument, so you can see the default value of 0 supplied by the constructor.
Use of a keyword
>> c2 = ComplexNumber(real=5)
Print it out.
class ComplexNumber(object): def __init__(self,real=0, imag=0):
self.real = realself.imag = imag
def __str__(self): return "{0}+{1}j".format(self.real, self.imag)
2
b. Construct the complex number c3 = 6j using a keyword. Use only one argument! Print it out!
c. Complete the stub method __add__(self, other) which adds two complex numbers and returns their sum. The advantage of using this magic method is that we will be able to add two complex numbers using the addition operator as in c1 + c2. Print out the sum of c1 and c2.
The definition of complex multiplication is: ( + )( + ) = ( ) + ( + )
d. Complete the stub method __mul__(self, other) which multiplies two complex numbers and returns their product. The advantage of using this magic method is that we will be able to multiply two complex numbers using the * operator as in c1 * c2.
Print out the product of c1 and c2.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started