Rectangle on the Table
Implement a class Rectangle for a rectangle that lies flat on a table and has a width and a height. Write these methods:
Rectangle(width, height)makes a rectangle of that size.area()returns the width times the height.perimeter()returns the distance all the way around: twice the width plus twice the height.widen(extra)makes the rectangleextrawider. The height stays the same. It returns nothing.turn()gives the rectangle a quarter turn, so that the width and the height change places. It returns nothing.fits_in(frame_width, frame_height)returnstrueif the rectangle, as it lies now, fits in a frame of that size: its width is at mostframe_widthand its height is at mostframe_height. It is not turned to make it fit.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
Rectangle(3, 2)
area() returns 6
perimeter() returns 10
widen(2)
area() returns 10
turn()
fits_in(2, 5) returns true
fits_in(5, 2) returns false
The rectangle starts 3 wide and 2 high: area 6, perimeter 10. Widened by 2 it is 5 by 2, with area 10. After the turn it is 2 wide and 5 high, so it fits a 2 by 5 frame but not a 5 by 2 frame.
Constraints
1 <= width, height <= 201 <= extra <= 51 <= frame_width, frame_height <= 250- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Classes problems
- Scoreboard by RoundsBeginner
- Stack With a Size LimitBeginner
- Take a NumberBeginner
- Tally CounterBeginner
- Thermostat Between Two LimitsBeginner
- Whose Turn Is ItBeginner
- Average of the Last K ReadingsEasy
- Call Limiter per UserEasy