Home / Questions / Class and Static Methods - 2 - Hands On
Explanatory Question

Class and Static Methods - 2 - Hands On

👁 505 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

2. Class and Static Methods - 2

1. Define a class method getCircleCount inside Circle which returns the number of circle objects created. Hint: Use classmethod decorator.

2. Try accessing the class method using an object and the class.

3. To test the number of circles created and the area of each circle, provide the radius of each circle in the input.

4. Output the total count of circles.

5. Check the main/tail section for output handling (read-only).

6. Use the Test against custom input box to output the result for debugging.

7. Provide float values separated by comma (refer test case samples) to display the output/error. The outputs should be split into 2 lines:

• The 1st output should return a key-value pair of the circle number and its corresponding area.

• The 2nd output should return the number of circles created or the number of inputs provided.

Solution


import os
import sys

#Add Circle class implementation here
class Circle(object):

    no_of_circles = 0

    def __init__(self, radius):

        self.__radius = radius

        Circle.no_of_circles += 1    

    @classmethod

    def getCircleCount(self):

        return Circle.no_of_circles    

    @staticmethod

    def square(x):

        return x**2
    

    def area(self):

        return 3.14*self.square(self.__radius)
        

'''Check the Tail section for input/output'''

if __name__ == "__main__":
    with open(os.environ['OUTPUT_PATH'], 'w') as fout:
        res_lst = list()
        circcount = list()
        lst = list(map(lambda x: float(x.strip()), input().split(',')))
        for radi in lst:
            c=Circle(radi)
            res_lst.append(str(c.getCircleCount())+" : "+str(c.area()))
        fout.write("{}\n{}".format(str(res_lst), str(Circle.getCircleCount())))