This project is part of the freeCodeCamp Python Certification.
It implements a simple geometry calculator using object-oriented programming. The application models rectangles and squares, allowing users to compute geometric properties and visualize shapes using text-based output.
- Create rectangles with custom width and height
- Create squares as a special case of rectangles
- Calculate area of shapes
- Calculate perimeter of shapes
- Compute the diagonal using
$d = \sqrt{width^2 + height^2}$ - Generate a text-based visual representation of the shape
- Modify dimensions dynamically (width, height, or side)
- Determine how many times one shape fits inside another
- Python 3
-
You should create a
Rectangleclass. -
When a
Rectangleobject is created, it should be initialized withwidthandheightattributes. The class should also contain the following methods:-
set_width: Sets the width of the rectangle. -
set_height: Sets the height of the rectangle. -
get_area: Returns area$(widthรheight)$ . -
get_perimeter: Returns perimeter$2(width+height)$ . -
get_diagonal: Returns diagonal$\sqrt{width^2 + height^2}$ . -
get_picture: Returns a string that represents the shape using lines of*. The number of lines should be equal to the height and the number of*in each line should be equal to the width. There should be a new line (\n) at the end of each line. If the width or height is larger than50, this should return the string:Too big for picture.. -
get_amount_inside: Takes another shape (square or rectangle) as an argument. Returns the number of times the passed in shape could fit inside the shape (with no rotations). For instance, a rectangle with a width of4and a height of8could fit in two squares with sides of4.
-
-
If an instance of a
Rectangleis represented as a string, it should look like:Rectangle(width=5, height=10). -
You should create a
Squareclass that subclassesRectangle. -
When a
Squareobject is created, it should be initialized with a single side length. The__init__method should store the side length in both thewidthandheightattributes from theRectangleclass. -
The
Squareclass should contain the following methods:-
set_width: Overrides theset_widthmethod from theRectangleclass. It should set the width and height to the side length. -
set_height: Overrides theset_heightmethod from theRectangleclass. It should set the width and height to the side length. -
set_side: Sets the height and width of the square equal to the side length.
-
-
The
Squareclass should be able to access theRectangleclass methods. -
If an instance of a
Squareis represented as a string, it should look like:Square(side=9).
Here is an example usage:
rect = Rectangle(10, 5)
print(rect.get_area())
rect.set_height(3)
print(rect.get_perimeter())
print(rect)
print(rect.get_picture())
sq = Square(9)
print(sq.get_area())
sq.set_side(4)
print(sq.get_diagonal())
print(sq)
print(sq.get_picture())
rect.set_height(8)
rect.set_width(16)
print(rect.get_amount_inside(sq))And here is an example of the output:
50
26
Rectangle(width=10, height=3)
**********
**********
**********
81
5.656854249492381
Square(side=4)
****
****
****
****
8
Example test cases are included as commented lines at the bottom of main.py.
Uncomment them to run and see the output.
- This project was developed to meet the certification requirements.
- The implementation focuses on correctness and clarity rather than extended functionality.