Docsity

electronics essay pdf, Essays (high school) of Electronics

Toggle sidebar
bg1
EE1CPS: Computational Problem Solving Problem Set 9
Problem Set 9: As If You Needed More Classes
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 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
Collaboration
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.
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
Encapsulate shapes
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:
•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.
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
pf3
pf4

Partial preview of the text

Download electronics essay pdf and more Essays (high school) Electronics in PDF only on Docsity!

Problem Set 9: As If You Needed More Classes

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 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

Collaboration

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.

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

Encapsulate shapes

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:

  • 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.

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 """

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. 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))

TO DO - create shape based on parts and add to the ShapeSet

TODO return your ShapeSet

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.

Submission Procedure

  1. Save All your code should be in a single file called ps9.py. Do not ignore this step or save your file(s) with different names.
  2. Time and Collaboration Info. At the start of each file, in a comment, write down the number of hours (roughly) you spent on the problems in that part, and the names of the people you collaborated with. For example: # Problem Set 9 # Name : Jan Lee # Collaborators : John Doe # Time : 1: # ... your code goes here ...
  3. Sanity checks. After you are done with the problem set, do these sanity checks:
    • Run the ps9.py file, and make sure it can be run without errors.
    • Inspect the ShapeSet created from parsing the file in Problem 4; does it match the specifications?