Unformatted text preview:

MIT OpenCourseWare http://ocw.mit.edu 6.00 Introduction to Computer Science and Programming Fall 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.6.00: Introduction to Computer Science and Programming Problem Set 9: As If You Needed More Classes Handed out: Thursday, October 30, 2008 Due: Monday, November 3, 2008 Introduction 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 us 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. Collaboration You may work with other students. However, each student should write up and hand in his or her assignment separately. Be sure to indicate with whom you have worked. For further details, please review the collaboration policy as stated in the syllabus. Getting Started 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 Problem 1 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. Implement the Triangle class, which also extends Shape. Triangle should have the same methods as the other subclasses of Shape. Other things to note: A Triangle is initialized with a base and height. The string representation for a triangle with base 3.0 and height 4.0 should be: Triangle with base 3.0 and height 4.0 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.Problem 2 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. Fill in the following code skeleton according to the specifications. class ShapeSet:def __init__(self):""" Initialize any needed variables """ # TO DO def addShape(self, sh):""" Add shape sh to the set; no two shapes in the set may be equal sh: shape to be added """ # TO DO def __iter__(self):""" Return an iterator that allows you to iterate over the set of shapes, one shape at a time """ # TO DO def __str__(self):""" Return the string representation for a set, which consists of the string representation of each shape, categorized by type. """ # TO DO 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.0 Square with side 4.0Square with side 1.0Triangle with base 1.0 and height 1.0 Problem 3 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. 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))>>> largest = findLargest(ss)>>> largest(<__main__.Circle object at xxxxx>,)>>> for e in largest: print eCircle with radius 4.0 Note that largest is a tuple containing an instance of Circle, because that is the shape with the largest area in the set. In some cases, there may be multiple shapes with the largest area. For example, a triangle with base 4 and height 6 had the same area as a triangle with base 3 and height 8. In this case, the tuple should contain both shapes. >>> ss = ShapeSet()>>> ss.addShape(Triangle(3,8))>>> ss.addShape(Circle(1))>>> ss.addShape(Triangle(4,6))>>> largest = findLargest(ss)>>> largest(<__main__.Triangle object at xxxxx>, <__main__.Triangle object at xxxxx>)>>> for e in largest: print eTriangle with base 3.0 and height 8.0Triangle with base 4.0 and height 6.0 You can check that the shape in the tuple is actually the shape you originally added to the ShapeSet by using the is keyword, which checks for object equality: >>> t = Triangle(6,6)>>> c = Circle(1)>>> ss = ShapeSet()>>> ss.addShape(t)>>> ss.addShape(c)>>> largest = findLargest(ss)>>> largest(<__main__.Triangle object at xxxxx>,)>>> largest[0] is tTrue >>> largest[0] is cFalse Problem 4 Currently, to create a new ShapeSet, we are required to first create several individual shapes, then add them one by one to the ShapeSet. If we want to use a large number of shapes or if we want to reuse the same shapes from one program to the next, writing the code to create each can be tedious and redundant. To solve this problem, we can store shape information in a file, then write a function to read the file line-by-line, creating a ShapeSet with the information provided.Write a function readShapesFromFile that will create and return a ShapeSet based on the information provided in the given file. def


View Full Document

MIT 6 00 - Study Guide

Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?