Python: Shape Classes With Area Method | HackerRank certification python(basic)

 

Implement two classes;

Rectangle:

  • The constructor for the rectangle must take two arguments that denote the length of the rectangle's sides 
  • The class must have an area method that returns the area of rectangle.

Circle:

  • The constructor for circle must take one argument that the radius of the circle.
  • The circle class must have an area method that returns the area of circle. To implement the area method; use a precise Pi value, preferably the constant math.pi
Your implementation of all the classes will be tested by a provided code stub on several input files. Each input file contains several queries, and each query constructs an object of one of the classes and prints the area of the object to the standard output with exactly 2 decimal points.



Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Rectangle:

    def __init__(self,l,b):

        self.l=l

        self.b=b

    def area(self):

        return self.l*self.b

    pass

class Circle:

    def __init__(self,r):

        self.r=r

    def area(self):

        return math.pi*(self.r**2)

    pass

if __name__ == '__main__'



Labels : #hackerrank certification ,#python (basic) ,

Post a Comment