Question: Use Python for following. A complex number is a number of the form a + bi, where a and b are real numbers and i

Use Python for following.

A complex number is a number of the form a + bi, where a and b are real numbers and i is sqrt(1). The numbers a and b are known as the real part and the imaginary part of the complex number, respectively.

Addition, subtraction, multiplication, and division for complex numbers is de- fined as follows:

(a+bi)+(c+di) = (a+c)+(b+d)i

(a+bi)(c+di) = (a-c)+(b-d)i

(a+bi)x(c+di) = (ac-bd)+(bc+ad)i

(a + bi)/(c + di) = [(ac+bd)/(c2+d2)]+[(bc-ad)/(c2+d2)i]

The absolute value for a complex number is given by:

|(a+bi)|=sqrt(a2 +b2)

A complex number can be interpreted as a point on a plane by identifying the (a, b) values as the coordinates of the point. The absolute value of the complex number corresponds to the distance of the point to the origin.

Python has the complex class for performing complex number arithmetic. Here, you will design and implement your own class to represent complex numbers.

In a file called complex.py, create a class named Complex for representing com- plex numbers, including overriding special methods for addition, subtraction, multiplication, division, and absolute value. Also include a __str__ method that returns a string representation for a complex number as (a+bi). If b is 0, __str__ should simply return a.

Provide a constructor Complex(a,b) to create a complex number a+bi with the default values of 0 for a and b. Also provide getRealPart() and getImaginaryPart() methods for returning the real and imaginary parts of the complex number, re- spectively.

Write a main function to test your Complex class by prompting the user to enter the real and imaginary parts of two complex numbers and displaying the results of calling each method.

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 Databases Questions!