Dabbling in Object Orientation - Task 1

Introduction

For my ongoing education towards becoming a greater computer programmer, the Department of Computer and System Sciences at Stockholm University, has tasked me with a series of programming exercises. Each exercise more challenging than the next, as it usually goes. This article will cover the first of eight pieces of work, the first being object orientation with python as the tool.


Work

The goal with the task was to teach me the basics of object orientation with simple geometrical shapes that inherited properties from a base class called Coords. For the sake of the article I will show you the finished product as an UML-diagram.

Overview of the structure in an UML-Diagram
Overview of the structure in an UML-Diagram

For those unfamiliar with the UML-diagrams, the above figure describes each class and their relationship to each other, as well as their properties and functions. This being my first task, all material was supplied to me with the exception being the rectangle class. Using the previous code as a reference, I swapped out the function for getting (aka. getters) the radius with functions for retrieving the length and width. In addition to changing the getters, I also changed the area and circumference functions to match that of a rectangles area and perimeter calculations.

The arrows represent the classes' inheritance from each other. The Circle class inherits functions from the Coords class, which in turn inherits the base functions from the Object class. Using the Object class' methods, __str__() and __eq__(), I extended the supplied test file with code for comparing rectangles and printing out the results. The figure below demonstrates the resulting file's printing to the console. Unfortunately I cannot show the final code, in order to avoid plagiarism.

Test file for the classes
Test file for the classes