Python linear equations
(Algebra: linear equations) Design a class named LinearEquation for a 2 x 2 system of linear equations.
The class contains:
■ The private data fields a, b, c, d, e, and f with get methods.
■ A constructor with the arguments for a, b, c, d, e, and f.
■ Six get methods for a, b, c, d, e, and f.
■ A method named isSolvable() that returns true if ad - bc is not 0.
■ The methods getX() and getY() that return the solution for the equation.
Write a test program that prompts the user to enter a, b, c, d, e, and f and displays the result. If ad - bc is 0, report that “The equation has no solution.”
class LinearEquation():
def __init__(self, a, b, c, d, e, f):
self.__a = a
self.__b = b
self.__c = c
self.__d = d
self.__e = e
self.__f = f
def getA(self):
return self.__a
def getB(self):
return self.__b
def getC(self):
return self.__c
def getD(self):
return self.__d
def getE(self):
return self.__e
def getF(self):
return self.__f
def isSolvable(self):
if LinearEquation.getA(self) * LinearEquation.getD(self) - LinearEquation.getB(self) * LinearEquation.getC(self) != 0:
return True
else:
return False
def getX(self):
if LinearEquation.isSolvable(self) == False:
return 'The equation has no solution!'
else:
x = (LinearEquation.getE(self) * LinearEquation.getD(self) - LinearEquation.getB(self) * \
LinearEquation.getF(self)) / (LinearEquation.getA(self) * LinearEquation.getD(self) - \
LinearEquation.getB(self) * LinearEquation.getC(self))
return x
def getY(self):
if LinearEquation.isSolvable(self) == False:
return 'The equation has no solution!'
else:
y = (LinearEquation.getA(self) * LinearEquation.getF(self) - LinearEquation.getE(self) * \
LinearEquation.getC(self)) / (LinearEquation.getA(self) * LinearEquation.getD(self) - \
LinearEquation.getB(self) * LinearEquation.getC(self))
return y
def main():
equ = LinearEquation(12, 23, 15, 10, 8, 19)
print('X = ', equ.getX(), ' y = ', equ.getY())
main()
SOLUTION:
X = 1.5866666666666667 y = -0.48
The class contains:
■ The private data fields a, b, c, d, e, and f with get methods.
■ A constructor with the arguments for a, b, c, d, e, and f.
■ Six get methods for a, b, c, d, e, and f.
■ A method named isSolvable() that returns true if ad - bc is not 0.
■ The methods getX() and getY() that return the solution for the equation.
Write a test program that prompts the user to enter a, b, c, d, e, and f and displays the result. If ad - bc is 0, report that “The equation has no solution.”
class LinearEquation():
def __init__(self, a, b, c, d, e, f):
self.__a = a
self.__b = b
self.__c = c
self.__d = d
self.__e = e
self.__f = f
def getA(self):
return self.__a
def getB(self):
return self.__b
def getC(self):
return self.__c
def getD(self):
return self.__d
def getE(self):
return self.__e
def getF(self):
return self.__f
def isSolvable(self):
if LinearEquation.getA(self) * LinearEquation.getD(self) - LinearEquation.getB(self) * LinearEquation.getC(self) != 0:
return True
else:
return False
def getX(self):
if LinearEquation.isSolvable(self) == False:
return 'The equation has no solution!'
else:
x = (LinearEquation.getE(self) * LinearEquation.getD(self) - LinearEquation.getB(self) * \
LinearEquation.getF(self)) / (LinearEquation.getA(self) * LinearEquation.getD(self) - \
LinearEquation.getB(self) * LinearEquation.getC(self))
return x
def getY(self):
if LinearEquation.isSolvable(self) == False:
return 'The equation has no solution!'
else:
y = (LinearEquation.getA(self) * LinearEquation.getF(self) - LinearEquation.getE(self) * \
LinearEquation.getC(self)) / (LinearEquation.getA(self) * LinearEquation.getD(self) - \
LinearEquation.getB(self) * LinearEquation.getC(self))
return y
def main():
equ = LinearEquation(12, 23, 15, 10, 8, 19)
print('X = ', equ.getX(), ' y = ', equ.getY())
main()
SOLUTION:
X = 1.5866666666666667 y = -0.48
Коментари
Постави коментар