I am having trouble understanding this assignment. My teacher explained it very
ID: 3883440 • Letter: I
Question
I am having trouble understanding this assignment. My teacher explained it very briefly over 10 minutes or so, and didn't actually teach this information thoroughly.
I am not only looking for a solution, but an explanation as well. I need to thoroughly understand this material for the exams.
Thank you very much.
(Classes are brand new to me, and I am still not confident with them.)
The output I currently receive:
(7, 1)
(7, 1)
(7, 1)
IMPORTANT: You cannot change the variables or function names.
Here's the code in plain text without color coding:
import math
class Complex:
def __init__ (self, r, i ):
self.real = r
self.imag = i
# Copy the three functions from Activity 1 of vLec9_8
def magnitude(self):
return math.sqrt(self.real**2 + self.imag**2)
def conjugate(self):
self.imag *= -1
def add(self, c):
if not isinstance(c, Complex):
return "Type Mismatch Error"
self.real += c.real
self.imag += c.imag
#The Following Functions are the assignment I am working on. The above functions were already given.
def multiply(self, c):
new = (self.real + self.imag) * (c.real + c.imag)
return new
#Procedural method: multiply self by c to change self where c is Complex
def divide(self, c):
new2 = ((self.real + self.imag)*(c.real - c.imag))/((c.real + c.imag)*(c.real - c.imag))
return new2
#Procedural method: divide self by c to change self where c is Complex
def power(self, n):
new3 = math.cos(n.real) + ((n.imag)*(math.sin(n.real)))
return new3
# Procedural method: take self to the power n to change self,
# where n is a positive integer
# test code
c1 = Complex(3,2)
c2 = Complex(4,-1)
c1.add(c2)
print((c1.real, c1.imag))
c1.power(4)
print((c1.real, c1.imag))
c1.divide(c2)
print((c1.real, c1.imag))
# should prints
#(7, 1)
#(2108, 1344)
#(416.94117647058823, 440.2352941176471)
Explanation / Answer
import math
class Complex: #To create a class name with Complex.
def __init__ (self, r, i ): #self refer objects of instance variable.
self.real = r
self.imag = i
# Copy the three functions from Activity 1 of vLec9_8
def magnitude(self):
return math.sqrt(self.real**2 + self.imag**2)
def conjugate(self):
self.imag *= -1 #it will multiply the instance variable of imag value with -1.
def add(self, c): # To add instance variables to another object's of the instance variable
if not isinstance(c, Complex): #If c is not a class object the return with an error message
return "Type Mismatch Error"
self.real += c.real #If c is a class object, then add those values with self variables
self.imag += c.imag
#The Following Functions are the assignment I am working on. The above functions were already given.
def multiply(self, c): #To multiply instance variables to another object's of the instance variable
new = (self.real + self.imag) * (c.real + c.imag) # eg: self.real => c1.real, self.imag=>c1.imag, c.real => c2.real, c.imag => c2.imag
return new #return result
#Procedural method: multiply self by c to change self where c is Complex
def divide(self, c): #To divide instance variables to another object's of the instance variable
new2 = ((self.real + self.imag)*(c.real - c.imag))/((c.real + c.imag)*(c.real - c.imag))
return new2 #return result
#Procedural method: divide self by c to change self where c is Complex
def power(self, n): #To find the power of instance variables to another object's of the instance variable
new3 = math.cos(n.real) + ((n.imag)*(math.sin(n.real)))
return new3 #return result
# Procedural method: take self to the power n to change self,
# where n is a positive integer
# test code
c1 = Complex(3,2)
c2 = Complex(4,-1)
c1.add(c2)
print((c1.real, c1.imag))
# self is used to refer instance of attributes. From the above case, c1 and c2 are Complex which means objects of class Complex. By using these objects(c1, c2) we can access methods of class. Like add, multiply, divide and power. These are functions/methods of class Complex.
#c1 = Complex(3,2) => so for c1 the real value is 3 and imag value is 2.
#c2 = Complex(4,-1) => so for c2 the real value is 4 and imag value is -1.
#if you are accessing any methods using these objects (c1 or c2) then the real and imag value would be considered based on its.(based on previous case)
#c1.add(c2) => Here the method/function of add would be called using c1 objects so the self.real value is 3, self.imag value is 2. so it will add with c2 values.
# 3+4 => 7(real), 2-1 = 1(imag).
c1.power(4)
print((c1.real, c1.imag))
#From the above case, c1.real is 3, c1.imag is 2. so the power value n is 4.
#new3 = math.cos(n.real) + ((n.imag)*(math.sin(n.real))) =>Here, n is not a class object so n.real is 4 and n.imag is 4.
#print((c1.real, c1.imag)) => so it will display recently updated values i.e 7, 1. It was replaced by the previous step
c1.divide(c2)
print((c1.real, c1.imag))
#print((c1.real, c1.imag)) => so it will display recently updated values i.e 7, 1. It was replaced by the previous step
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.