Advanced OOP and patterns · 7 / 15 Exercise Medium

Rectangle with computed properties

Objective

Derived data (area, perimeter…) is not stored: it is computed when requested through a read-only property, so it never gets out of date if the measures change. The measures are validated in their setter.

Instructions

  • Create Rectangle with the properties width and height (they must be greater than 0; otherwise ValueError) and the read-only properties area, perimeter and is_square.
  • The first line has the number n of rectangles; each following line has width height integers. Show Area: a Perimeter: p Square: s (Yes or No), or Invalid dimensions if a measure is not valid.
Input
2
3 4
5 5
Output
Area: 12 Perimeter: 14 Square: No
Area: 25 Perimeter: 20 Square: Yes

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise