Difference between revisions of "CSC111 Lab 12 2015b"

From dftwiki3
Jump to: navigation, search
Line 18: Line 18:
 
The deadline for the submission on Moodle is Friday 12/04 at 11:55 a.m.
 
The deadline for the submission on Moodle is Friday 12/04 at 11:55 a.m.
 
</bluebox>
 
</bluebox>
 +
<br />
 +
<br />
 +
__TOC__
 
<br />
 
<br />
 
<br />
 
<br />

Revision as of 09:15, 21 November 2015

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




List of Lists

Demonstration Example


  • Create a new new program called Lab11_demo.py and copy the code from this program.
  • Carefully read the program to see what it does, and how it does it.
  • Run the program to verify that it works well.
  • Edit the text variable, and make it contain only 5 lines of text (it doesn't matter what countries you pick).
  • Run your program again and verify that the "top-10" and "last-10" sections still work and do not crash, even though the list contains fewer than 10 tuples.
  • Using a similar approach, solve the next problem.


Problem 2: Animals


The text below contains names of animals and their speed, expressed in miles per hour. Write a Python program called Lab11_3.py (no need to write classes for this problem) based on the preparation problem above, and make your program output the following quantities:

  1. The list of all the animals ranked by speed. Fastest first.
  2. The 10 fastest animals listed in order of decreasing speed
  3. The 10 slowest animals listed in order of increasing speed
  4. The ratio of the speeds of the fastest animal versus that of the slowest animal. For example, if the speed of the fastest animal is 50 and the speed of the slowest is 2, then the program should output that the fastest animal is 25 times faster than the slowest one.
  5. The list of all the animals (just their name, not their speed) that are faster than a human being ("human" is one of the species listed). Your program should not contain the number 27.9, which is the speed of a human. Instead it should find this number by looking up "human" in one of your lists and get the speed associated for the human.


Black Mamba Snake 20.0 mph 
Cape Hunting Dog 45.0 mph 
Cat (domestic) 30.0 mph 
Cheetah 70.0 mph 
Chicken 9.0 mph 
Coyote 43.0 mph 
Elephant 25.0 mph 
Elk 45.0 mph 
Giant Tortoise 0.2 mph 
Giraffe 32.0 mph 
Gray Fox 42.0 mph 
Greyhound 39.4 mph 
Grizzly Bear 30.0 mph 
Human 27.9 mph 
Hyena 40.0 mph 
Jackal 35.0 mph 
Lion 50.0 mph 
Mongolian Wild Ass 40.0 mph 
Mule Deer 35.0 mph 
Pig (domestic) 11.0 mph 
Pronghorn Antelope 61.0 mph 
Quarter Horse 47.5 mph 
Rabbit (domestic) 35.0 mph 
Reindeer 32.0 mph 
Six-Lined Racerunner 18.0 mph 
Spider (Tegenaria atrica) 1.2 mph 
Squirrel 12.0 mph 
Thomson's Gazelle 50.0 mph 
Three-Toed Sloth 0.1 mph
Warthog 30.0 mph 
Whippet 35.5 mph 
White-Tailed Deer 30.0 mph 
Wild Turkey 15.0 mph 
Wildebeest 50.0 mph 
Zebra 40.0 mph