Objective
Create a Python program that models several geometric shapes from the following class diagram. The diagram represents public, protected and private attributes, as well as inheritance.
Instructions
- Design a base class called
GeometricShapewith the properties common to all the shapes, such as the color, and a constructor to initialize them. - Create classes that inherit from
GeometricShapeto represent a circle, a square and a triangle. Each one implements the methods that calculate its area and its perimeter. - In the main program, create objects of the different shapes and display their information (color, area and perimeter).
- Notice how inheritance and encapsulation produce more organized and reusable code.
Input and output format. Read, one per line and with no prompts: the color and radius of the circle, the color and side of the square, and the color, base and height of the triangle. Print the three blocks, separated by an empty line, each with the lines Circle Information: / Square Information: / Triangle Information:, Color: …, Area: … and Perimeter: …. Area and perimeter with 2 decimals. The perimeter of the triangle is 3 × base (approximation for an equilateral triangle) and its area base × height / 2.
Red
5
Blue
4
Green
6
8
Circle Information:
Color: Red
Area: 78.54
Perimeter: 31.42
Square Information:
Color: Blue
Area: 16.00
Perimeter: 16.00
Triangle Information:
Color: Green
Area: 24.00
Perimeter: 18.00
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise