CSC111 Lab 12 2015b

From dftwiki3
Revision as of 09:05, 21 November 2015 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut (talk) 16:49, 20 November 2015 (EST)


Page under construction!


UnderConstruction.jpg


The first part of the lab deals with classes in general and class inheritance in particular, and you will be building classes derived from other classes.
For the second part, you will be working with text-based information in the form of lists. We often have to deal with lists: they can be list of email addresses, list of contacts, list of courses, list of grad schools, list of historical facts, list of books, or authors, etc. And very often we need to extract just a few items from the list that are of interest to us. Once you know programming, such task can be done very simply. In this lab, you are going to process several lists and extract information from them.

The deadline for the submission on Moodle is Friday 12/04 at 11:55 a.m.


Problem 1: Class Inheritance: Rectangles with Labels Inside


MyRectClassLabel.png
  • Create a new program called Lab11_1.py and copy the code below into it.
# lab11_1.py
# ...
# 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 LAB", 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()
  • Run the program. Verify that it displays a Yellow rectangle with a string in the middle.
  • Add a loop to your main program, that will make the rectangle move to the right:


     for i in range( 20 ):
            r1.move( 10,2 )


  • Do you observe something strange? What is happening? Think...



ThinkThinkThink.jpg



  • Think some more. Do not move on until you are sure you know what is going on...



ThinkThinkThink.jpg



Discussion Time!


Discussion.jpg


  • Discuss the various options as a class. Figure out what is going on. What is missing, what the problem is, and how to fix it.



Circles with Labels Inside


  • Use the same approach to create a new class called MyCirc that is a subclass of Circle, and that displays a circle with a label inside. You can use the same program and add to it.


  • Modify the loop in your main program so that it makes both the MyRect object and the MyCirc object move together (maybe not in the same direction).


Building a Car with MyRect

Simple2WheelCar.png


  • Locate the program you wrote a while back, that displays a car on the graphic window (with a body, and two wheels).
  • In case you had implemented a text label as part of your Car class, please remove it. Make sure your program displays only a car with 2 wheels and two rectangles, one for the body, one for the top.
  • Copy the car class in your current program for today's lab.


Using MyRect to build the Body of the Car


  • Organize your program so that the MyRect class is listed at the top of the program, along with MyCirc, and then followed by the Wheel and Car classes.


       ;header
      
       Class MyRect:
           ...
        
       Class Wheel:
           ...

       Class Car:
           ...

       def main():
           ...

       main()
  • Modify your Car class so that it uses MyRect instead of Rectangle for its body. Make the label inside the body be a label that is passed in the constructor of the Car.
  • Verify that your program correctly displays the modified car.
  • Make your car move some deltax, deltay in a loop, the same way you moved the MyRect and MyCirc objects in the previous problem. Verify that the car moves correctly, including its label.