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

Class and Static Methods - 1 - Hands On

👁 491 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

1. Class and Static Methods - 1

1. Define a class Circle with attribute radius.

2. Define the following:

• A class variable count which determines the number of circles created.

• A method area that calculates the area of a circle.

3. The input should be comma-separated-values, while the output should be the area for each value and the total number of circles created.

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

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

6. 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 the area of the circles for which inputs were provided.

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

Solution


import os
import sys


#Add Circle class implementation below

class Circle(object):   

    no_of_circles = 0    

    def __init__(self, radius):

        self.__radius = radius

        Circle.no_of_circles += 1        

    def getCirclesCount(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()
        lst = list(map(lambda x: float(x.strip()), input().split(',')))
        for radius in lst:
            res_lst.append(Circle(radius).area())
        fout.write("{}\n{}".format(str(res_lst), str(Circle.no_of_circles)))