 # Complex Numbers in Python

Subject Computer Science Python Programming

## Question

Problem A. 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 we’re going to make our own. Obviously, you’re not allowed to use the built-in Complex class to implement your Complex class.

Complex objects have two instance variables:
real (a numeric value representing the real component of the complex number)
imag (a numeric value representing the imaginary component of the complex number)

(Note: Please do not write all of these methods at once. Copy the test cases in the section below this into your hw10.py file, and uncomment a few of them at a time as you implement things: make sure that each method works before moving on to the next one. This is something you should be doing anyway but is especially important when writing long pieces of code like class definitions)

The Complex class must include the following:
__init__(self, real, imag): A constructor with two numeric arguments (not counting self), that initialize the real and imag instance variables, 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:
"<real> + <imag>i"
where <real> and <imag> 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 you’re not familiar with how to add or multiply two complex numbers. You’ll need to have the following method signatures to overload the operators:
__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. You’ll 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:
Do not import/use any library modules.
Your submission should have no code outside of class/function definitions (comments are fine).
You are not permitted to use Python’s built-in Complex class
Your methods must have exactly the same names and arguments as described above.

Example: Copy the following code into your hw10.py file, and uncomment lines as you write the relevant methods. What each line should print is noted in the comment on the right. You should write some additional tests of your own: this is not a very good set of tests for the number of methods this problem requires. Make sure to comment out all tests before submitting to github.

##x = Complex(2.7,3)
##y = Complex(-4,0)
##z = Complex(0,-1.5)

##print(x.real)       # 2.7
##print(x.get_real()) # 2.7
##print(y.get_imag()) # 0

##x.set_real(6)
##z.set_imag(-2.5)

##print(x.real)       # 6
##print(x)             # 6 + 3i
##print(y)             # -4 + 0i
##print(z)             # 0 + -2.5i
##print(x+y)          # 2 + 3i
##print(z+y+x)       # 2 + 0.5i
##print(y*z)          # 0.0 + 10.0i
##print(x*z)          # 7.5 + -15.0i
##print(x*y+x*z)       # -16.5 + -27.0i
##print(x*(y+z))       # -16.5 + -27.0i
##print(x == y)       # False
##print(x*y+x*z == x*(y+z))   # True

## Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

#==========================================
# Purpose: Complex class
# Input Parameter(s): real component, imaginary component
# Return Value(s): Complex number object
#==========================================
class Complex:

def __init__(self, real, imag):
self.real = real
self.imag = imag

def get_real(self):
return self.real

def get_imag(self):
return self.imag

def set_real(self, new_real):
self.real = new_real

def set_imag(self, new_imag):
self.imag = new_imag

def __str__(self):
return str(self.real) + ' + ' + str(self.imag) + 'i'

a = self.get_real()
b = self.get_imag()
c = other.get_real()
d = other.get_imag()
return Complex(a+c, b+d)

def __mul__(self, other):
a = self.get_real()
b = self.get_imag()
c = other.get_real()
d = other.get_imag()
return Complex(a*c-b*c, a*d+b*c)

def __eq__(self, other):
a = self.get_real()
b = self.get_imag()
c = other.get_real()
d = other.get_imag()
return a == c and b == d

##'''
##Complex class Tests
##'''
##print()
##print('Complex class Tests')
##print()
##
##x = Complex(2.7,3)
##y = Complex(-4,0)
##z = Complex(0,-1.5)
##
##print(x.real)       # 2.7
##print(x.get_real()) # 2.7
##print(y.get_imag()) # 0
##
##x.set_real(6)
##z.set_imag(-2.5)
##
##print(x.real)       # 6
##print(x)             # 6 + 3i
##print(y)             # -4 + 0i
##print(z)             # 0 + -2.5i
##print(x+y)          # 2 + 3i
##print(z+y+x)       # 2 + 0.5i
##print(y*z)          # 0.0 + 10.0i
##print(x*z)          # 7.5 + -15.0i
##print(x*y+x*z)       # -16.5 + -27.0i
##print(x*(y+z))       # -16.5 + -27.0i
##print(x == y)       # False
##print(x*y+x*z == x*(y+z))   # True

#==========================================
# Purpose: Employee class
# Input Parameter(s): csv line
# Return Value(s): Employee object
#==========================================
class Employee:

def __init__(self, line):
params = line.strip().split(',')
self.name = params
self.position = params
self.salary = float(params)
self.seniority = float(params)
self.value = float(params)

def __str__(self):
return self.name + ', ' + self.position

def net_value(self):
return round(self.value - self.salary, 2)

def __lt__(self, other):
return self.net_value() < other.net_value()...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.