In: Computer Science
Python Please
Define a class that has one data attribute of type float. The initializer does not accept an argument, however, it initializes the data attribute to zero. Include in class the following methods:
1. func that will accept as two arguments. The first argument a value of float type and the second argument if string. The function will perform the following depending on the string argument:
a. if string is '+', the first argument will be added to the data attribute.
b. if string is '-', the first argument will be subtracted from the data attribute.
c. if string is '*', the data attribute will be equal to the data attribute multiplied by first argument.
d. if string is '/', the data attribute will be equal to the data attribute divided by first argument.
e. if string is '^', the data attribute will be equal to the data attribute raised to the power of the first argument.
2. _ _init_ _ method
3. _ _str_ _ that will return the data attribute as a string
4. An accessor that will return the data attribute
class Operations:
#constructor
def __init__(self):
self.data=0.0
#func accepts 2 arguments
def func(self,value,string):
if string=='+':
self.data+=value
elif string=='-':
self.data-=value
elif string=='*':
self.data*=value
elif string=='/':
self.data/=value
elif string=='^':
self.data**=value
#accessor
def getData(self):
return self.data
#str() prints string representation of
object
def __str__(self):
return
str(self.data)
#testing class
#create object for class
operation=Operations()
operation.func(4.5,"+")
operation.func(3.4,"*")
print(operation) #invokes str()
Output