Question: Python has the complex class for performing complex number arithmetic. In this exercise, you will design and implement your own Complex class. Note that the

Python has the complex class for performing complex number arithmetic. In this exercise, you will design and implement your own Complex class. Note that the complex class in Python is named in lowercase, but our custom Complex class is named with C in uppercase.
A complex number has the form
, where
and
are real numbers and
is
. The numbers
and
are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas:
You can also obtain the absolute value for a complex number using the following formula:
A complex number can be interpreted as a point on a plane by identifying the
values as the coordinates of the point. The absolute value of the complex number corresponds to the distance of the point to the origin (0,0).
Write a class named Complex for representing complex numbers and the methods add, sub, mul, truediv, and abs for performing complex-number operations, and override the str method by returning a string representation for a complex number. The str method returns (a + bi) as a string. If b is 0, it simply returns a. Provide a constructor Complex(a, b) to create a complex number with the default value of 0 for a and b. Also provide the getRealPart() and getImaginaryPart() methods to get the real and imaginary parts of the complex number, respectively.
Sample Run
Enter the real part of the first complex number: 3.5
Enter the imaginary part of the first complex number: 6.5
Enter the real part of the second complex number: -3.5
Enter the imaginary part of the second complex number: 1
(3.5+6.5i)+(-3.5+1.0i)=(0.0+7.5i)
(3.5+6.5i)-(-3.5+1.0i)=(7.0+5.5i)
(3.5+6.5i)*(-3.5+1.0i)=(-18.75+-19.25i)
(3.5+6.5i)/(-3.5+1.0i)=(-0.4339622641509434+-1.9811320754716981i)
|(3.5+6.5i)|=7.3824115301167
(3.5+6.5i)+(-3.5+1.0i)*(3.5+6.5i)=(-15.25+-12.75i)

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!