Question
In this programming assignment, you must define a class called LinearEquation that will allow you to create, print, and perform several operations on linear equations.
In this programming assignment, you must define a class called LinearEquation that will allow you to create, print, and perform several operations on linear equations.
Write an __init__ method that takes as arguments a value for m, the slope, and a value for b, the y-intercept, and creates a LinearEquation object.
Write a method called showEq that returns a string of the form "mx + b", where the values for m and b are substituted in instead of the letters. If b is negative, you should print a minus sign instead of a plus sign. Spaces should also appear in the designated places. If m = 0 or b = 0, then nothing should be printed for that term. (See the section "Input and Output" below.)
Write a method called add that adds together two LinearEquation objects. It should return a new LinearEquation object whose slope is equal to the sum of the two slopes, and whose y-intercept is equal to the sum of the two y-intercepts. For example, if you were to add together 2x + 3 and 3x - 2, you would get 5x + 1.
Write a method sub that similarly subtracts two LinearEquation objects.
Write a method called compose that returns a new LinearEquation object that represents the composition of the two equations given as arguments. For example, if f(x) = 2x + 3 and g(x) = 3x - 2, then f(g(x)) = 2(3x-2) + 3 = 6x - 1.
Write a method called evaluate that takes a LinearEquation object and an integer value, and returns the value of the equation using that integer for x. For example, if f is the equation 2x + 3, then f.evaluate(1) is 2(1) + 3 = 5.
Input and Output
There is no input for your program. Instead, all method calls will be called from the main program, which is provided for you here:
def main(): f = LinearEquation(5,3) print("f(x) =",f.showEq()) print("f(3) =",f.evaluate(3)," ") g = LinearEquation(-2,-6) print("g(x) =",g.showEq()) print("g(-2) =",g.evaluate(-2)," ") h = f.add(g) print("h(x) = f(x) + g(x) =",h.showEq()) print("h(-4) =",h.evaluate(-4)," ") j = f.sub(g) print("j(x) = f(x) - g(x) =",j.showEq()) print("j(-4) =",j.evaluate(-4)," ") k = f.compose(g) print("f(g(x)) =",k.showEq()," ") m = g.compose(f) print("g(f(x)) =",m.showEq()," ") g = LinearEquation(5,-3) print("g(x) =",g.showEq()) print("g(-2) =",g.evaluate(-2)," ") h = f.add(g) print("h(x) = f(x) + g(x) =",h.showEq()) print("h(-4) =",h.evaluate(-4)," ") j = f.sub(g) print("j(x) = f(x) - g(x) =",j.showEq()) print("j(-4) =",j.evaluate(-4)," ") main()
Your task is to define the class LinearEquation with all of the methods defined above, and add the class to the top of the main program. Do NOT modify the main program itself in any way! (The only exception would be if you need to fix the tabs to match your code.) If you've written your class correctly, your output will look exactly like the following:
f(x) = 5x + 3
f(3) = 18
g(x) = - 2x - 6
g(-2) = -2
h(x) = f(x) + g(x) = 3x - 3
h(-4) = -15
j(x) = f(x) - g(x) = 7x + 9
j(-4) = -19
f(g(x)) = - 10x - 27
g(f(x)) = - 10x - 12
g(x) = 5x - 3
g(-2) = -13
h(x) = f(x) + g(x) = 10x
h(-4) = -40
j(x) = f(x) - g(x) = 6
j(-4) = 6
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started