Interface for geometric shapes: calculating area and perimeter
TypeScript
Medium
interfaces
Task «Interface for geometric shapes: calculating area and perimeter»
Create a Shape interface that contains getArea() and getPerimeter() methods returning a number. Implement the Circle class (takes radius) and Rectangle class (takes width and height) that implement this interface. Then write a processShape function that takes an array of shapes and prints for each shape its area and perimeter separated by a space. All input data is read from stdin: first the number n (number of shapes), then n lines, each starting with the shape type ('circle' or 'rectangle'), followed by parameters (radius for circle, width and height for rectangle). For each shape, print its area and perimeter, rounded to two decimal places, each pair on a new line.
Input Format
First line: integer n (1 ≤ n ≤ 100). Then n lines, each in the format: <type> <parameters>. For circle: radius (integer or real). For rectangle: width height (integer or real).
Output Format
n lines, each containing two numbers separated by a space: the area and the perimeter, rounded to two decimal places.
Examples
Example 1
INPUT
2
circle 1
rectangle 0.5 0.5
OUTPUT
3.14 6.28
0.25 2.00
Example 2
INPUT
5
1 1 2 3 3
OUTPUT
1 2 2 1 3 2
main.ts