Exercise 9
Exercise 9
Shapes
Consider the following 4 shapes:
Parallelogram: a four sided figure with parallel pairs of sides. A Parallelogram is defined by the lengths of
its two pairs of sides (labelled base and side in the picture below) and the interior angle (in degrees) between
adjacent sides (labelled theta in the picture below).
Rectangle: a parallelogram with four right angles. A Rectangle is defined by the lengths of its two pairs of
sides (labelled base and side in the picture below).
Rhombus: a parallelogram with four equal sides. A Rhombus is defined by the length of its sides (labelled
base in the picture below) and the interior angle (in degrees) between adjacent sides (labelled theta in the
picture below).
Square: a parallelogram with four equal sides and four right angles; both a Rectangle and a Rhombus. A
Square is defined by the length of its sides (labelled base in the picture below).
Your first task is to figure out the IS-A hierarchy of these shapes. Draw it out in a piece of paper. Are there
any instances of multiple parents? 1
1 In Python, you can inherit from multiple parents. Just add them separated by commas: class MyClass(ParentA, ParentB)
1
Your Task
You must write four classes: Parallelogram, Rectangle, Rhombus and Square, ensuring that the class
hierarchy follows your diagram from the previous step. You may create additional classes if you wish (are
there any good reasons why you might want to do this?). The parameters of init methods should always
be input in the following order: (base, side, theta) (theta being given in degrees), though of course, not
every class’ init will take all three. So for example, a Rectangle will only take (base, side).
Objects of these classes must have the following methods:
Be Lazy
One way to solve this would be to write four completely independent classes, and have each class completely
implement all of their own functions. This would be a bad idea (why?). If you use inheritance correctly, you
should find the exercise much simpler. Remember, you should never calculate something when you can just
get another method to do the work for you. Hint: It’s possible to set more than one parent for your class.
Are there any shapes here for which that would be a sensible thing to do?
What to Submit
All of your code should be submitted to MarkUs in a file called ex9.py that doesn’t import anything other
than math, and does not ask for input or print anything when imported.
2 “I am a Square with area 100.0” would also be acceptable, you don’t need to do any rounding