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.

c. compose(linear_eq) 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.

Here's what I've put together:

class LinearEquation(object): def __init__(self, m, B)/>/>/>: self.m = float(m) self.b = float(B)/>/>/> def __str__(self): return "%.2fx + %.2f" % (self.m, self.B)/>/>/> def __repr__(self): return self.__str__() def value(self, x): return self.m * x + self.b def compose(self, linear_eq): return LinearEquation(self.m * linear_eq.m, \ self.m * linear_eq.b + self.B)/>/>/> def __add__(self, z): return LinearEquation(self.m + z.m, self.b + z.B)/>/>/> # ---------------------------------main--------------------------------------- eq1 = LinearEquation(2, 3) eq2 = LinearEquation(5, 6) print("\nEquation 1: y =", eq1) print("Equation 2: y =", eq2) print("\nThe sum of the two equations is y =", eq1.__add__(eq2))

... and this is the output:

Equation 1: y = 2.00x + 3.00

Equation 2: y = 5.00x + 6.00

The sum of the two equations is y = 7.00x + 9.00

... so I guess it works, but what can I do to improve it? The only thing I haven't done from the description is Error handling. How would I implement that into the program? I assume in the __add__() method? Should I use try-except... or if-else? Any tips?

LOL I just noticed the face in the description! But I have one more question, should I change from "float" to "int" so I'm not getting decimal whole numbers?

This post has been edited by **Frenchi33**: 24 October 2017 - 04:24 PM