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 points Complex Class In this problem you need to create a class named Complex that models a complex number, numbers of the form xyi where x and y are real numbers, and i is the imaginary unit equal to the square root of In the xyi 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 builtin Complex class, but this makes for a good, simple example, so were going to make our own. Obviously, youre not allowed to use the builtin 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 componentNote: 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: initself 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: getrealself getimagself setrealself newreal setimagself newimag Overload the strself 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: addself othermulself 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: eqself other Hints: Review on how to addmultiply complex numbers: a bic dia cb di a bic diac bdad bci Constraints: You are not permitted to use Pythons builtin Complex class Your methods must have exactly the same names and arguments as described above. Examples: Copy the following if namemain block into your hwpy file. The comment next to each print statement indicates what should be printed out. if namemain: x Complex printxreal # printximag # printxgetreal # printxgetimag # xsetreal printx #i xsetimag printstrxi #True print x Complex y Complex printxy #i printx #i printy #i z Complex printzyx #i print x Complex z Complex printxz #i y Complex printxy #i printxyz #i printxyz #i print printComplex Complex #False printComplex Complex #False printComplex Complex #True x Complex y Complex z Complex printxyxz xyz #True
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
