|
| 1 | +''' |
| 2 | +area and perimeter of rectangle -- Classes Practice Problems |
| 3 | +Design a class which has 2 methods. One which computes the Area of the Rectangle. The other computes the Perimeter of the Rectangle. You should be able to pass the length l and width w while creating the object for the class. For all infeasible values of length l and width w, print area and perimeter as 0 |
| 4 | +
|
| 5 | +Your class should be named as Rectangle. Method to get area should be named as rectangle_area. Method to get perimeter should be named as rectangle_perimeter. |
| 6 | +
|
| 7 | +Input |
| 8 | +First line contains an integer for the length l Second line contains an integer for the width w |
| 9 | +
|
| 10 | +Output |
| 11 | +Two lines containing integers. First line containing the Area of the Rectangle Second line containing the Perimeter of the Rectangle |
| 12 | +
|
| 13 | +Example |
| 14 | +Input: |
| 15 | +
|
| 16 | +3 |
| 17 | +2 |
| 18 | +Output: |
| 19 | +
|
| 20 | +6 |
| 21 | +10 |
| 22 | +First line is 3 representing the length. Second line is 2 representing the width. Area is 3*2 which is 6 as represented in the first line of the output. Perimeter is 2*(3 + 2) which is 10 as represented in the second line of the output. |
| 23 | +
|
| 24 | +''' |
| 25 | +# Your class should be named as `Rectangle`. |
| 26 | +# Method to get area should be named as `rectangle_area`. |
| 27 | +# Method to get perimeter should be named as `rectangle_perimeter`. |
| 28 | +# You should be taking `length` and `width` as inputs when creating the object for your class. |
| 29 | +class Rectangle: |
| 30 | + def __init__(self,l,b): |
| 31 | + self.length=l |
| 32 | + self.width=b |
| 33 | + def rectangle_area(self): |
| 34 | + if self.length>0 and self.width>0: |
| 35 | + return int(self.length*self.width) |
| 36 | + else: |
| 37 | + return 0 |
| 38 | + def rectangle_perimeter(self): |
| 39 | + if self.length>0 and self.width>0: |
| 40 | + return int(2*(self.length+self.width)) |
| 41 | + else: |
| 42 | + return 0 |
| 43 | +if __name__ == "__main__": |
| 44 | + newRectangle = Rectangle(int(input()), int(input())) |
| 45 | + print(newRectangle.rectangle_area()) |
| 46 | + print(newRectangle.rectangle_perimeter()) |
0 commit comments