Complete the Rectangle class so that area() and perimeter() return the right values.
What to build
A class with two calculated results.
Requirements
- Complete
area()andperimeter(). - Leave the rest of the program as it is.
Input
One line with the width and height, two whole numbers from 1 to 10000.
Output
Two lines: the area, then the perimeter.
Constraints
Each size is from 1 to 10000.
Example
Input:
4 3
Output:
12
14
Why it works
The sizes are stored on the object, so methods read them through self instead of taking parameters.
Examples
Example 1 · Input
4 3
Expected output
12 14
Example 2 · Input
1 1
Expected output
1 4
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Store the values in __init__ with self.width = width.
Hint 2
Methods read them back as self.width and self.height.
Solution
Show one correct solution (try it yourself first)
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
w, h = input().split()
r = Rectangle(int(w), int(h))
print(r.area())
print(r.perimeter())