Document 13718879

advertisement
Exercise 9.5: Make circle a subclass of an ellipse Section 7.2.3 presents class Circle. Make a similar class
Ellipse for representing an ellipse. Then create a new class Circle that is a subclass of Ellipse. Filename:
Ellipse_Circle.py.
Exercise 9.6: Make super- and subclass for a point A point (x, y) in the plane can be represented by a
class: class Point: def __init__(self, x, y): self.x, self.y = x, y def __str__(self): return ’(%g, %g)’% (self.x,
self.y) We can extend the Point class to also contain the representation of the point in polar coordinates.
To this end, create a subclass PolarPoint whose constructor takes the polar representation of a point, (r,
θ), as arguments. Store r and θ as attributes and call the superclass constructor with the corresponding x
and y values (recall the relations x = r cos θ and y = r sin θ between Cartesian and polar coordinates).
Add a __str__ method in class PolarPoint which prints out r, θ, x, and y. Verify the implementation by
initializing three points and printing these points. Filename: PolarPoint.py.
Exercise 9.7: Modify a function class by subclassing Consider a class F implementing the function f(t; a, b)
= e −at sin(bt):
class F:
def __init__(self, a, b):
self.a, self.b = a, b
def __call__(self, t):
return exp(-self.a*t)*sin(self.b*t)
We now want to study how the function f(t; a, b) varies with the parameter b, given t and a.
Mathematically, this means that we want to compute g(b;t, a) = f(t; a, b). Write a subclass Fb of F with a
new __call__ method for evaluating g(b;t, a). Do not reimplement the formula, but call the __call__
method in the superclass to evaluate f(t; a, b). The Fs should work as follows: f = Fs(t=2, a=4.5) print f(3)
#b=3 Hint. Before calling __call__ in the superclass, the attribute b in the superclass must be set to the
right value. Filename: Fb.py.
Exercise 9.12: Understand if a class can be used recursively Suppose you want to compute f 00(x) of
some mathematical function f(x), and that you apply some class from Section 9.2 twice, e.g., ddf =
Central2(Central2(f)) Will this work? Hint. Follow the program flow, and find out what the resulting
formula will be. Then see if this formula coincides with a formula you know for approximating f 00(x)
(actually, to recover the well-known formula with an h parameter, you would use h/2 in the nested calls
to Central2).
Exercise 9.20: Make line drawing of a person; program A very simple sketch of a human being can be
made of a circle for the head, two lines for the arms, one vertical line, a triangle, or a rectangle for the
torso, and two lines for the legs. Make such a drawing in a program, utilizing appropriate classes in the
Shape hierarchy. Filename: draw_person.py.
Exercise 9.21: Make line drawing of a person; class Use the code from Exercise 9.20 to make a subclass
of Shape that draws a person. Supply the following arguments to the constructor: the center point of
the head and the radius R of the head. Let the arms and the torso be of length 4R, and the legs of length
6R. The angle between the legs can be fixed (say 30 degrees), while the angle of the arms relative to the
torso can be an argument to the constructor with a suitable default value. Filename: Person.py.
Langtangen, Hans Petter (2014-08-01). A Primer on Scientific Programming with Python (Texts in
Computational Science and Engineering) (Page 593). Springer Berlin Heidelberg. Kindle Edition.
Download