In: Computer Science
Using Python write the following script as well as using comments
By considering the details below, write a class that will work out the body mass index for specific values of weight and height. The design of the actual class is shown below:
bmi
-weight:float
-height:float
-bmi:float
+set_weight(weight:float)
+set_height(height:float)
+calc_bmi()
+get_bmi():float
specific designs of the methods are shown below:
set_weight
set the weight attribute to the value in the parameter weight
set_height
set the height attribute to the value in the parameter height
calc_bmi
calculate Bmi using the formula: bmi=weight/(height/100)**2
get_bmi
return the value stored in the bmi attribute
After creating the class write a main function that will create an instance of the class and test the implementation.
#class named as bmi
class bmi:
#constructor
#It is required so as to initialize instance variable
def __init__(self):
#set all variables to 0
self.weight=0.0
self.height=0.0
self.bmi=0.0
#function to set weight
def set_weight(self,weight):
#setting weight to parameter
self.weight=weight
#function to set height
def set_height(self,height):
#setting height to parameter value
self.height=height
#function for calculation of bmi
def calc_bmi(self):
#using given formula to calculate bmi and store in self.bmi
self.bmi=self.weight/(self.height/100)**2
#getter for bmi
def get_bmi(self):
#return the value of self.bmi
return self.bmi
#implementation
#creating an instance of bmi class which is BMI
BMI=bmi()
#calling setter for weight to set weight of instance BMI to
70
BMI.set_weight(70)
#calling setter for height to set height of instance BMI to 180
cm
BMI.set_height(180)
#function to calculate bmi
BMI.calc_bmi()
#printing the returned value from the getter for bmi
#%.2f is used to print 2 digits after decimal point
print("Body Mass Index: %.2f"%BMI.get_bmi())