Exercise 11.4: At the beginning of the chapter, we discussed a pyramid as another natural example of a recursive structure. Implement a Pyramid class recursively, using an approach similar to that used for the Bullseye class. Allow the user to specify the number of levels and the overall width (which can be the same as the height). The main difference in techniques involves the positioning of the components. For the bullseye, the outer circle and inner bullseye were concentric. For the pyramid, you will need to relocate the bottom rectangle and the upper pyramid to achieve the desired effect. Move the components so that the completed pyramid sits with its bottom edge centered on the origin (the default reference point).
from cs1graphics import *
class Bullseye(Drawable):
"""Represent a bullseye with an arbitrary number of bands."""
def __init__(self, numBands, radius, primary='black', secondary='white'):
"""Create a bullseye object with alternating colors.
The reference point for the bullseye will be its center.
numBands the number of desired bands (must be at least 1)
radius the total radius for the bullseye (must be positive)
primary the color of the outermost band (default black)
secondary the color of the secondary band (default white)
"""
if numBands <= 0:
raise ValueError('Number of bands must be positive')
if radius <= 0:
raise ValueError('radius must be positive')
Drawable.__init__(self) # must call parent constructor
self._outer = Circle(radius)
self._outer.setFillColor(primary)
if numBands == 1:
self._rest = None
else: # create new bullseye with one less band, reduced radius, and inverted colors
innerR = float(radius) * (numBands-1) / numBands
self._rest = Bullseye(numBands-1, innerR, secondary, primary)
def _draw(self):
self._beginDraw() # required protocol for Drawable
self._outer._draw() # draw the circle
if self._rest:
self._rest._draw() # recursively draw the rest
self._completeDraw() # required protocol for Drawable
from cs1graphics import *
class Bullseye(Drawable):
"""Represent a bullseye with an arbitrary number
of bands."""
def __init__(self, numBands, radius,
primary='black', secondary='white'):
"""Create a bullseye
object with alternating colors.
The reference point for
the bullseye will be its center.
numBands the number
of desired bands (must be at least 1)
radius
the total radius for the bullseye (must be positive)
primary the
color of the outermost band (default black)
secondary the color of
the secondary band (default white)
"""
if numBands <=
0:
raise ValueError('Number of bands must be positive')
if radius <= 0:
raise ValueError('radius must be positive')
Drawable.__init__(self) # must call parent constructor
self._outer =
Circle(radius)
self._outer.setFillColor(primary)
if numBands ==
1:
self._rest = None
else: # create new
bullseye with one less band, reduced radius, and inverted
colors
innerR = float(radius) * (numBands - 1) / numBands
self._rest = Bullseye(numBands - 1, innerR, secondary, primary)
def getNumBands(self):
"""Return the number of
bands in the bullseye."""
bandcount = 1 # outer is
always there
if self._rest: # still
more
bandcount += self._rest.getNumBands()
return bandcount
def getRadius(self):
"""Return the radius of
the bullseye."""
return
self._outer.getRadius() # ask the circle
def setColors(self, primary,
secondary):
"""Recolor the bullseye
with the two specified colors.
primary will be used for the
outermost band
secondary will be used for the secondary
band
"""
self._outer.setFillColor(primary)
if self._rest:
self._rest.setColors(secondary, primary) # color inversion
def _draw(self):
self._beginDraw() #
required protocol for Drawable
self._outer._draw() #
draw the circle
if self._rest:
self._rest._draw() # recursively draw the rest
self._completeDraw() #
required protocol for Drawable
def _mainAlt():
paper = Canvas(400, 160)
print
"\nUNIT: creating lay1"
lay1 = Layer()
print
"\nUNIT: creating sq1"
sq1 = Square(70, Point(130, 30))
print
"\nUNIT: coloring sq1"
sq1.setFillColor('yellow')
print
"\nUNIT: changing sq1 depth"
sq1.setDepth(60)
print
"\nUNIT: adding sq1 to lay1"
lay1.add(sq1)
print
"\nUNIT: creating simple bullseye"
simple = Bullseye(3, 60)
print
"\nUNIT: moving simple bullseye"
simple.move(65, 80)
print
"\nUNIT: changing depth of simple
bullseye"
simple.setDepth(70)
print
"\nUNIT: adding simple bullseye to lay1"
lay1.add(simple)
print
"\nUNIT: adding lay1 to paper"
paper.add(lay1)
print
"\nUNIT: changing depth of lay1"
lay1.setDepth(80)
print
"\nUNIT: creating sq2"
sq2 = Square(60, Point(130, 120))
print
"\nUNIT: changing color of sq2"
sq2.setFillColor('green')
print
"\nUNIT: adding sq2 to lay1"
lay1.add(sq2)
print
"\nUNIT: changing detph of sq2"
sq2.setDepth(90)
print
"\nUNIT: creating bullseye blue"
blue = Bullseye(4, 45, 'darkblue',
'skyblue')
print
"\nUNIT: adding bullseye blue to paper"
paper.add(blue)
print
"\nUNIT: changing depth of bullseye blue"
blue.setDepth(75)
print
"\nUNIT: moving bullseye blue"
blue.move(195, 80)
print
"\nUNIT: creating sq3"
sq3 = Square(35, Point(135, 80))
print
"\nUNIT: changing color of sq3"
sq3.setFillColor('orange')
print
"\nUNIT: changing depth of sq3"
sq3.setDepth(85)
print
"\nUNIT: adding sq3 to paper"
paper.add(sq3)
raw_input('Press return to continue ')
if __name__ == "__main__":
from time import sleep
paper = Canvas(375, 160)
sleep(2)
simple = Bullseye(3, 60)
paper.add(simple)
simple.move(65, 80)
simple.setDepth(49)
blue = Bullseye(4, 45, 'darkblue',
'skyblue')
paper.add(blue)
blue.move(190, 80)
colored = Bullseye(11, 60, 'red',
'green')
paper.add(colored)
colored.move(320, 80)
colored.setDepth(51)
input('Press return to continue ')
colored.setColors('red', 'yellow')
input('Press return to continue ')
simple.move(75, 0)
input('Press return to continue ')
colored.move(-75, 0)
input('Press return to continue ')
blue.move(0, 50)
input('Press return to continue ')
blue.scale(2)
input('Press return to continue ')
Exercise 11.4: At the beginning of the chapter, we discussed a pyramid as another natural example...
What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that share a set of tasks, even though the tasks are executed in different ways. Polymorphism allows a programmer to use a subclass object in place of a superclass object. Polymorphism allows a subclass to override a superclass method by providing a completely new implementation. Polymorphism allows a subclass to extend a superclass method by performing the superclass task plus some additional work. Assume that...