Question: Problem A . ( 1 0 points ) Complex Class In this problem you need to create a class named Complex that models a complex

Problem A.(10 points) Complex Class In this problem you need to create a class named Complex that models a complex number, numbers of the form x+yi, where x and y are real numbers, and i is the imaginary unit equal to the square root of -1. In the x+yi representation, we say that x is the real component of the complex number, and y is the imaginary component. Note that Python already has a built-in Complex class, but this makes for a good, simple example, so were going to make our own. Obviously, youre not allowed to use the built-in Complex class to implement your Complex class. Complex objects have two attributes (also known as instance variables): self.real (a numeric value representing the real component of the complex number) self.imag (a numeric value representing the imaginary component)(Note: Please do not write all of these methods at once. After you have written each method, look for tests below for that method and make sure its working before moving on to the next method) Define a class Complex with the following methods: __init__(self, real, imag): A constructor with two numeric arguments (not counting self), that initializes the real and imag attributes, respectively. You must have getter and setter methods for both real and imag. They should have the following signatures: get_real(self) get_imag(self) set_real(self, new_real) set_imag(self, new_imag) Overload the __str__(self) method so that it returns a string of the format: "+ i" where and represent the real and imaginary components of the complex number, respectively. Overload the + and * operators, so that they take in a Complex object other, and return a new Complex object representing the sum or product (respectively) of the complex numbers represented by self and other. See the Hints section if youre not familiar with how to add or multiply two complex numbers. Youll need to have the following method signatures to overload the operators: __add__(self, other)__mul__(self, other) Overload the == operator, so that it takes in a Complex object other, and returns True if the two objects are equal (their real components are equal, and their imaginary components are equal), or False otherwise. Youll need to have the following method signature to overload the operator: __eq__(self, other) Hints: Review on how to add/multiply complex numbers: (a + bi)+(c + di)=(a + c)+(b + d)i (a + bi)(c + di)=(ac - bd)+(ad + bc)i Constraints: You are not permitted to use Pythons built-in Complex class Your methods must have exactly the same names and arguments as described above. Examples: Copy the following if __name__=='__main__' block into your hw12.py file. The comment next to each print statement indicates what should be printed out. if __name__=='__main__': x = Complex(2.7,3) print(x.real) #2.7 print(x.imag) #3 print(x.get_real()) #2.7 print(x.get_imag()) #3 x.set_real(8) print(x) #8+3i x.set_imag(-4.5) print(str(x)=='8+-4.5i') #True print() x = Complex(1.5,4) y = Complex(-2,0) print(x+y) #-0.5+4i print(x) #1.5+4i print(y) #-2+0i z = Complex(0,-1.5) print(z+y+x) #-0.5+2.5i print() x = Complex(6,3) z = Complex(0,-2.5) print(x*z) #7.5+-15.0i y = Complex(-4,2) print(x*y) #-30+0i print(x*(y+z)) #-22.5+-15.0i print(x*y*z) #0.0+75.0i print() print(Complex(4,2)== Complex(4,3)) #False print(Complex(2,6)== Complex(4,6)) #False print(Complex(3,5)== Complex(3,5)) #True x = Complex(6,3) y = Complex(-4,2) z = Complex(0,-2.5) print(x*y+x*z == x*(y+z)) #True

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!