


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



In this problem set, you will be creating and using classes and methods. Along the way, you should become familiar with concepts such as inheritance and overriding methods.
Workload Please let me know how long you spend on each problem. We want to be careful not to overload you by giving out problems that take longer than we anticipated
You may work with other students. However, each student should write up and submit his or her assignment separately. Be sure to indicate with whom you have worked. For further detail, please review the collaboration policy as stated on Blackboard.
Add the code you write for this problem set to the following template: ps9.py
For problem 4, use the following sample input file: shapes.txt
In this problem set, we will be using classes to encapsulate shapes.
We have implemented two different shape subclasses for you, namely Square and Circle. For each of these shapes, we are interested in getting the area (note how the respective area methods override the area method of the Shape superclass), checking for equality, and specifying a string representation of the object.
Problem 1 Implement the Triangle class, which also extends Shape. Triangle should have the same methods as the other subclasses of Shape. Other things to note:
Triangle with base 3.0 and height 4.
Note: Remember that a double underscore (__) around a method name indicates that the method has special meaning to Python, even though it can be used and overridden like any other method. Python has some special conventions for calling such methods; for example, len(x) makes Python call x.len(). You can learn more about these special methods from the Python Language Reference.
It may be handy to work with a set of shapes. In this problem we define a class, ShapeSet, which manages a set of shape objects. Create a method, addShape, to add a shape to the set, with the stipulation that no two shapes in the set may be equal (that is, when being compared with the == operator). The iter method should return an iterator that allows you to iterate over the set of shapes, one shape at a time. You may assume that the ShapeSet is not mutated while any iterator is active. More information on iterator types can be found in the Python Library Reference.
Problem 2 Fill in the following code skeleton according to the specifications.
Derived from Prof. Eric Grimson and Prof. John Guttag, Introduction to Computer Science and Programming , MIT OpenCourseWare. License: Creative commons BY-NC-SA
class ShapeSet : def init ( self ): """ Initialize any needed variables """
def addShape ( self , sh ): """ Add shape sh to the set ; no two shapes in the set may be equal sh : shape to be added """
def iter ( self ): """ Return an iterator that allows you to iterate over the set of shapes , one shape at a time """
def str ( self ): """ Return the string representation for a set , which consists of the string representation of each shape , categorized by type. """
The string representation of ShapeSet should include the string representation of each shape (each of which would be on its own line, if printed). Categorize the shapes in the set by their types. If our set consisted of a square with side length 4, another square with side length 1, a circle with radius 2, and a triangle with base 1 and height 1, the string representation of this ShapeSet could be:
Circle with radius 2. Square with side 4. Square with side 1. Triangle with base 1.0 and height 1.
Now that we have a working representation of individual shapes and groups of shapes, we can utilize these in outside code. For example, suppose we want to find the shape(s) with the largest area in a ShapeSet. This function will be outside of the classes we’ve defined.
Problem 3 Write a function findLargest that returns a tuple containing all items in a ShapeSet with the largest area. def findLargest ( shapes ): """ Returns a tuple containing the elements of ShapeSet with the largest area. shapes : ShapeSet """
To test your code, try finding the largest shape in several different ShapeSets. For example:
>>> ss = ShapeSet () >>> ss. addShape ( Triangle (1.2 ,2.5)) >>> ss. addShape ( Circle (4)) >>> ss. addShape ( Square (3.6)) >>> ss. addShape ( Triangle (1.6 ,6.4)) >>> ss. addShape ( Circle (2.2))
The file information will be stored as follows:
circle, square, circle, triangle,4, square, circle,
For a sample shape file, please see shapes.txt Note that circles and squares have one argument (side length and radius, respectively), while triangles have two (base and height). You will need to find a way to deal with either case while you read the file. The information on strings at the Python Library Reference may be helpful.
You can test your code by printing out the ShapeSet returned; this will also help you to test your ShapeSet and other functions:
>>> ss = readShapesFromFile ( " shapes. txt " ) >>> print ss Circle with radius 4. Circle with radius 2. Circle with radius 4. Circle with radius 3. Circle with radius 6. Circle with radius 19. Square with side 3. Square with side 4. Square with side 16. Square with side 2. Square with side 10. Triangle with base 4.2 and height 4. Triangle with base 3.0 and height 6. Triangle with base 1.3 and height 4. Triangle with base 4.0 and height 1. Triangle with base 2.6 and height 1.