Class Notes Week 6

Week 6 Topics

Monday Wednesday Friday

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:

lst1 = 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()


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. The * in this context imports all the the classes and functions from the library. This is useful shortcut when we will be using most of the features in a library.

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


  1. To prevent the program from closing, we will use a method getMouse() that waits for a user click to finish


Play ground

I've given a very simple program in 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, 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.


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

$ python3 -i
>>>from colorPicker import 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.


To use this value, give it to setFill()

circ = Circle(Point(50,50),25)

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