You have to Create a class called Algebra, which has a single class variable called scale, and
Question:
You have to Create a class called Algebra, which has a single class variable called scale, and a constructor which takes two numerical values x and y. The class should implement the following methods:
- Write a method called add which returns the sum of x and y.
- Write a class method called multiply, which takes a single number parameter a and return the product of a and scale.
- Write a static method called subtract, which takes two number parameters, b and c, and return b - c.
- Write a method called print_value, which prints the value of x and y, separated by a comma.
This learning activity requires you to write a program in Python.
Post a Python script file (.py) rather than copy and pasting your code into the text editor in discussion forum.
For example, please check the following solved answer. You can provide the answer here in that way or .py file
class Algebra:
scale = 2
def __init__(self, x, y):
self.x = x
self.y = y
def add(self):
return self.x + self.y
@classmethod
def multiply(cls, a):
return a * cls.scale
@staticmethod
def subtract(b, c):
return b - c
def print_value(self):
print(f"{self.x}, {self.y}")
algebra = Algebra(10, 20)
print("Sum:", algebra.add())
a = 5
print("Product of a and scale:", Algebra.multiply(a))
b, c = 20, 8
print("Difference:", Algebra.subtract(b, c))
algebra.print_value()
Management Accounting Information for Decision-Making and Strategy Execution
ISBN: 978-0137024971
6th Edition
Authors: Anthony A. Atkinson, Robert S. Kaplan, Ella Mae Matsumura, S. Mark Young