In this code, we define a class Rectangle with an initializer method __init__ and a method calcPerimeter to calculate the perimeter of the rectangle.
Code Breakdown:
1. __init__ method: This is the constructor of the class. When an object of the Rectangle class is created, it initializes the attributes a and b with the values provided as arguments.
def __init__(self, aVal, bVal):
self.a = aVal
self.b = bVal
Here, self.a and self.b are set to aVal and bVal, respectively.
2. calcPerimeter method: This method calculates the perimeter of the rectangle using the formula .
def calcPerimeter(self):
return 2 * self.a + 2 * self.b
3. Creating an object and calling calcPerimeter:
obj = Rectangle(1, 5)
print(obj.calcPerimeter())
Here, we create an object obj of the Rectangle class with a = 1 and b = 5.
Then, calcPerimeter is called on obj, which calculates .
Output:
The output of this code is 12.
0 Comments:
Post a Comment