Class Notes Week 6


Week 5 Topics

Monday Wednesday Friday


We’ll begin with exercises to reinforce what we learned last week:

Stack traces with lists

Recall that lists are mutable; meaning, we can make changes to its value. If we mutate a parameter, the argument that pointed to the same value will also be altered. To test our understanding, do stack traces of the following two programs and decide whether ARG1 is affected by the call to foo() in both programs:

Program A:

def foo(PARAM1)
  if PARAM1[0] > 0:
    PARAM1[0] += 1
    return True
  else:
    return False

def main():
  ARG1 = [5,-2,10]
  foo(ARG1)
  #is ARG1 the same or changed?

Program B:

def foo(PARAM1)
  if PARAM1[0] > 0:
    PARAM1 = [6,-2,10]
    return True
  else:
    return False

def main():
  ARG1 = [5,-2,10]
  foo(ARG1)
  #is ARG1 the same or changed?

For Program A, ARG1 points to the same list as PARAM1 and thus will be different than before foo()(with the value [6,-2,10]). In Program B, however, there is no affect on ARG1. Why? The line PARAM1 = [6,-2,10] resets the pointer of PARAM1; that is, it causes PARAM1 to point to a new list [6,-2,10] and thus breaks the shared connection with ARG1.

Object Oriented Programming (OOP)

We will introduce a new style of programming called object-oriented programming. OOP is in contrast to the imperative style that we have engaged in thus far, where functions and data are defined separately. In OOP, we introduce the concept of objects which are defined by:

Objects are pre-defined in terms of what type of data and methods they have. This is part of the class definition (we will do this later in the semester). Classes are the data type (like str and list - both types of objects). Objects are instances of a class. For example:

lst = list()
nums = [2,5,6]

Both lst and nums are objects of the class list. They hold data and have methods that can be called on them e.g., append(),index()

nums.append(10)
nums.index(5)

To use objects, you need to first create the object. This is known as initializing or calling the constructor of the object. The constructor is a function that has the same name as the class type (list(), str()). Second, you call methods on the object. There are generally two types of methods:

Some methods do a combination of both (both change and retrieve information).

Graphics Library

We will use the graphics library to help understand objects. Note that we are using a special (simplified) graphics library; refer to the documentation as well as links on the syllabus for the capabilities of the library as well as download instructions if you want to use it on your laptop.

Example programs:

MooStarry Night

The key elements for using the library are:

  1. Import the library. We could use import, but the following approach is simpler (we no longer need to put graphics.function in front of every library call).
from graphics import *
  1. In your program, create a GraphWin object - the graphics window where we’ll draw things.
win = GraphWin() # Creates default window, see documentation to customize
  1. Create whatever shapes you want. For example, a point:
pt = Point(200, 120)
  1. Draw shapes in the window
pt.draw(win)
  1. To prevent the program from closing, we will use a method getMouse() that waits for a user click to finish
win.getMouse()

Play ground

I’ve given a very simple program in playGround.py. We will try out new things in here to get a feel for the graphics library.

On your own, spend a few minutes modifying the program and seeing the changes. In particular try the following:

Animation and Moving

In animate.py, we’ll write a program that animates a circle across the screen. This will require the use of three ideas:

  1. getMouse() method for GraphWin to get the location of where the user clicked
  2. move() method for all shapes to change the current location of a drawn object
  3. sleep() in the time library to create pauses in the code so that the movements don’t occur too fast.

Colors

If you want to see the available colors, use the color picker library:

$ python3 -i
>>>import colorPicker
>>>colorPicker.colorPicker()

This will popup a swatch of colors; click on the color and the name will be printed to the terminal. You can use the name in setFill() etc. to set the color.

Another option is to use a numeric value. One way to describe a color is the RGB standard - how much red, green, and blue there is in the color. This comes from television displays and monitors; a pixel on your screen is a combination of a red light, green light, and blue light mixed together. Each color can be described as an integer between 0 and 255 with 0 being e.g., “no red” and 255 being “full red”. Here is how you can get the color corresponding to 200 red, 200 green, and 0 blue.

color_rgb(200,200,0)

To use this value, give it to setFill()

circ = Circle(Point(50,50),25)
circ.setFill(color_rgb(200,200,0))

Memory Diagrams/Stack Traces for Objects

We will continue to use stack diagrams to trace through programs and understand how they work. This is an essential skill to being able to think computationally and write effective programs. In duplicate.py, we have a program that wants to create two circles, one red and one green, of the same size. The program does not work; work with a partner and trace through the program to see why. The program should look like this: duplicate.jpg

Functions and Objects

Objects interact with functions in the same ways that we say with basic types (ints, floats, strings, and lists). This means that we can return graphics objects we create and also send them in as arguments/parameters.

Exercise: create rectangles and find the tallest

In createRectangles.py, we have a partially completed program. The end goal is to write a program that allows the user to draw rectangles of various sizes, finds the tallest one, and colors it in.

Complete the task in this order:

  1. Pair up and explain each line of the program as is. Ask a ninja if you are not sure what a part of the program is doing.
  2. Write a function to give a color to the rectangle
  3. Change the main part of the program to create multiple rectangles (e.g., 3)
  4. Lastly, write a function to find the tallest rectangle and return it. In main, change the code to only color the tallest rectangle.