Question: At this point, we are ready to start using our ` Sandwich ` class objects! * * Solve it ! * * Create your own

At this point, we are ready to start using our `Sandwich` class objects!
**Solve it!**
Create your own ``ComplexNumber`` class in the cell with the comment `#si-complex-number`!
1. Complex numbers have a real and an imaginary part. The `__init__()` method should therefore accept two numbers. Store the first as self.real and the second as self.imag.
2. Implement a `conjugate()` method that returns the object's complex conjugate (as a new `ComplexNumber` object). Recall that $x = a + bi \implies \bar{x}= a - bi$, where $\bar{x}$ is the complex conjugate of $x$.
3. Add the following magic methods to your `ComplexNumber` class:
-`__abs__()` determines the output of the builtin `abs()` function (absolute value). Implement `__abs__()` so that it returns the magnitude of the complex number. Recall that $|a + bi|=\sqrt{a^2+ b^2}$.
- Implement `__lt__()` and `__gt__()` so that `ComplexNumber` objects can be compared by their magnitudes. That is, $(a + bi)<(c + di)$ if and only if $|a + bi|<|c + di|$, and so on.
4. Add the following magic methods to your `ComplexNumber` class:
- Implement `__eq__()` and `__ne__()` so that two `ComplexNumber` objects are equal if and only if they have the same real and imaginary parts.
- Implement `__add__()`,`__sub__()`,`__mul__()`, and `__div__()` appropriately. Each of these should return a new `ComplexNumber` object.

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!