I'm a little bit confused with what this method is supposed to do. Here's the full description:
"Write a class for linear equations. A generic linear equation is of the form
y = mx + b where m and b are constants.
Include the following methods:
(a) __init__, __str__, __repr__.
(b) value(x), which returns the value of the equation given x.
© compose(linear_equation) that composes two linear equations. That is,
if y = x + 1 and z = 2a + 5, then y(z) = 2x + 6 and will be called as
y.compose(z). Note that the compose operation is not communitive.
(d) __add__ returns the sum of two linear equations. That is,
if y = ax + b and z = cx + d, then y + z = (a + c)x + (b + d).
Include sample code that uses your class and demonstrates the use of all
methods as well as error handling."
On part c), I don't know what's actually going on when they say compose two linear equations. I tried looking it up but am not finding what I need. So if y = x + 1 and z = 2a + 5, how does y(z) = 2x + 6? I notice the constants are added together, but what about the x and 2a? I'm a little confused. What happens to the "a", or what even is the "a"?
Here's my full program:
Also, I have one more question. I'm supposed to implement error handling? Did I do it right with the try-except? That's honestly my biggest question.
Thanks!
One more thing, where else do I need to implement error handling besides the __add__() method?
"Write a class for linear equations. A generic linear equation is of the form
y = mx + b where m and b are constants.
Include the following methods:
(a) __init__, __str__, __repr__.
(b) value(x), which returns the value of the equation given x.
© compose(linear_equation) that composes two linear equations. That is,
if y = x + 1 and z = 2a + 5, then y(z) = 2x + 6 and will be called as
y.compose(z). Note that the compose operation is not communitive.
(d) __add__ returns the sum of two linear equations. That is,
if y = ax + b and z = cx + d, then y + z = (a + c)x + (b + d).
Include sample code that uses your class and demonstrates the use of all
methods as well as error handling."
On part c), I don't know what's actually going on when they say compose two linear equations. I tried looking it up but am not finding what I need. So if y = x + 1 and z = 2a + 5, how does y(z) = 2x + 6? I notice the constants are added together, but what about the x and 2a? I'm a little confused. What happens to the "a", or what even is the "a"?
Here's my full program:
class LinearEquations(object): def __init__(self, m, b): # print("in constructor") self.m = float(m) self.b = float(b) def __str__(self): # print("in str") return "%2fx + %.2f" % (self.m, self.b) def __repr__(self): # print("in repr") return self.__str__() def value(self, x): return self.m * x + self.b def compose(self, linear_equation): return LinearEquations(self.m * linear_equation.m, \ self.m * linear_equation.b + self.b) def __add__(self, z): """ Combine like terms by adding two linear equations together. """ try: return LinearEquations(self.m + z.m, self.b + z.b) except TypeError as e: print("Error!", e) return None # ---------------------------------main--------------------------------------- # description example executed equation_1 = LinearEquations(1, 1) equation_2 = LinearEquations(2, 5) print("\nLinear Equation 1: y =", equation_1) print("Linear Equation 2: z =", equation_2) print("\nThe 'composition' of the two linear equations is y(z) =", \ equation_1.compose(equation_2)) print("The 'sum' of the two linear equations is y + z =", \ equation_1.__add__(equation_2))I know the compose() method works, but I'm honestly not sure what's going on.
Also, I have one more question. I'm supposed to implement error handling? Did I do it right with the try-except? That's honestly my biggest question.
Thanks!
One more thing, where else do I need to implement error handling besides the __add__() method?