Difference between revisions of "CSC111 Lab 11 2015"

From dftwiki3
Jump to: navigation, search
Line 66: Line 66:
 
</source>
 
</source>
 
<br />
 
<br />
=Problem 2=
+
=Problem 2: Presidents=
 
<br />
 
<br />
 
Below is a CSV list (coma-separated values) of past presidents.  Using similar Python code as you have used for the previous problem, process this list and output:
 
Below is a CSV list (coma-separated values) of past presidents.  Using similar Python code as you have used for the previous problem, process this list and output:
Line 122: Line 122:
 
</source>
 
</source>
 
<br />
 
<br />
=Rectangles with Labels Inside=
+
=Problem 3: Class Inheritance: Rectangles with Labels Inside=
 +
<br />
 +
::<source lang="python">
 +
# lab11_3.py
 +
# D. Thiebaut
 +
# A graphic program with a new MyRect class that is derived from
 +
# the Rectangle class in the graphics.py library.
 +
 
 +
from graphics import *
 +
 
 +
# window geometry
 +
WIDTH = 600
 +
HEIGHT = 400
 +
 
 +
 
 +
class MyRect( Rectangle ):
 +
    """MyRect: a class that inherits from Rectangle, in graphics.py"""
 +
 
 +
    def __init__( self, p1, p2, labl ):
 +
        """constructor.  Constructs a rectangle with a text label in its center"""
 +
 
 +
        # call the Rectangle constructor and pass it the 2 points
 +
        Rectangle.__init__( self, p1, p2 )
 +
 
 +
        # put a label at a point in-between p1 and p2.
 +
        midPoint = Point( (p1.getX()+p2.getX())/2,
 +
                          (p1.getY()+p2.getY())/2 )
 +
        self.label = Text( midPoint, labl )
 +
 
 +
    def draw( self, win ):
 +
        """draw the rectangle and the label on the window."""
 +
        # call the draw() method of the rectangle class to draw the rectangle
 +
        Rectangle.draw( self, win )
 +
 
 +
        # then draw the label on top
 +
        self.label.draw( win )
  
 +
 +
def main():
 +
    # open the window
 +
    win = GraphWin( "CSC Aquarium", WIDTH, HEIGHT )
 +
 +
    # create an object of type MyRect with the string "CSC111" in the middle
 +
    r1 = MyRect( Point( 100, 100 ), Point( 200, 200 ), "CSC111" )
 +
    r1.setFill( "Yellow" )
 +
    r1.draw( win )
 +
 +
    # close the window when the user clicks the mouse
 +
    win.getMouse()
 +
    win.close()
 +
 +
main()
 +
 +
</source>
 +
 +
<br />
 
=Circles with Labels Inside=
 
=Circles with Labels Inside=
  

Revision as of 10:01, 12 April 2015

--D. Thiebaut (talk) 19:36, 11 April 2015 (EDT)



...