You are on page 1of 25

CSC148H Week 1

Dan Zingaro

January 2, 2017
Welcome!

I Welcome to CSC148
I Previous experience: CSC108 or equivalent experience
I Goals
I Designing programs using OO programming principles
I Thinking recursively and writing recursive functions
I Writing and using recursive, linked, and other data structures
I Reasoning about efficiency of code
I Reasoning about sorting algorithms
Evaluation

I Ten labs (10%)


I Two assignments (20%)
I Two term tests (20%)
I Better test counts for 12%, worse test for 8%
I Final exam (50%)
I 40% rule: students who earn less than 40% on the exam do
not pass the course
Lectures

I We have three lectures per week


I The plan is to introduce most of the new material in the first
two hours of each week
I The third hour of each week will usually focus on worksheets
and exercises
Labs

I Labs are done with a partner (pair programming)


I They start in week 2; check your timetable for room locations
I There is a lab every week except the first and last week
I Each lab is worth 1%
I To get the mark, work hard during the lab and complete most
of it
Assignments

I The assignment handouts will be on the course website


I Due at 22:00 on due date; submitted electronically using
MarkUs
I Work individually or with one or two others
Assignment Late Policy

I No assignments are accepted late


I If you can’t finish the work, you can earn part marks for a
good partial solution
I Illness and other disasters are another matter; contact me as
soon as you can
Help!

I Office hours (before class, or by appointment)


I Attend any of the three instructor’s office hours!
I Closed labs (led by your TA)
I Online discussion boards
I Anonymous feedback
I . . . form study groups!
Academic Integrity

In brief:
I Never look at someone else’s assignment work (not even a
draft)
I Never show other students your assignment work (not even a
draft)
I Don’t post anything online (e.g. pastebin)
I Discuss how to solve an assignment only with your assignment
partner, the course TAs, and instructor
Academic Integrity...

I often handled many academic offense cases in CSC108 and


CSC148
I Waste of your time and mine
I Doesn’t help you learn the course material
I Results in mark penalties and transcript annotations
Checklist for This Week

I Read the course information sheet


I Bookmark the course website and discussion board
I Log in to the online discussion board. Make sure your login
works!
I If you plan on working on your own computer, install software
listed on course website
I Drop by office hours and introduce yourself
Python Objects

Here are two kinds of Python objects: strings and turtles.


>>> w1 = "words"
>>> w2 = w1.lower()
>>> w1 is w2
False
>>> import turtle
>>> t = turtle.Turtle()
>>> t.pos()
(0.00,0.00)
>>> t.forward(100)
Vandalizing Existing Classes

>>> from turtle import Turtle


>>> t1 = Turtle()
>>> t1.pos()
(0.00,0.00)
>>> t1.forward(100)
>>> t1.pos()
(100.00,0.00)
>>> t1.neck
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: ’Turtle’ object has no attribute ’neck’
>>> Turtle.neck = "reptilian" # don’t do this
>>> t2 = Turtle()
>>> t2.neck
’reptilian’
Classes and Methods

I Sometimes, the built-in object types (e.g. string, list, dict)


are not natural for supporting new types of data
I By defining a new class, we can add a new type to Python
I We can then make objects of that type (e.g. objects of type
Student)
I Class names are nouns: student, course, rectangle, animal,
ship, (and str, list, dict!)
I Methods are actions specific to each type of object
I e.g. for ship: move, turn, shoot, dodge, raise shields, lower
shields, teleport
Attributes

I An attribute is a feature or characteristic of an object


I Implemented using instance variables
I Unlike a method, it is not an action
I An attribute is something that an object has, not something
the object does
Class name: Ship
Possible methods: move, turn, shoot, dodge, raise_shields,
lower_shields, teleport
Possible attributes: weight, length, width, direction, speed
Creating Objects

I When an object is created, its __init__ method is called


I __init__ is known as a constructor because it constructs
objects
I Inside __init__, assign values to the object’s attributes
Object-Oriented Analysis

I Suppose that you’re given a description of how a system


should operate
I Find the objects (nouns)
I Determine object behaviour (methods)
I Determine object state (instance variables)
I Determine object interactions
Designing a New Class

Somewhere in the real world there is a description of points in


two-dimensional space:
In two dimensions, a point is two numbers (coordinates) that
are treated collectively as a single object. Points are often written in
parentheses with a comma separating the coordinates. For example,
(0, 0) represents the origin, and (x, y) represents the point x units
to the right and y units up from the origin. Some of the typical
operations that one associates with points might be calculating the
distance of a point from the origin or from another point, or finding
a midpoint of two points, or asking if a point falls within a given
rectangle or circle.

Find the most important noun (good candidate for a class. . . ), its
most important attributes, and operations that this noun should
support.
Building Class Point (BAD!)

>>> from math import sqrt


>>> class Point:
... pass
...
>>> def initialize(point, x, y):
... point.x = x
... point.y = y
...
>>> def distance(point):
... return sqrt(point.x**2 + point.y**2)
...
>>> Point.__init__ = initialize
>>> Point.distance = distance
>>> p2 = Point(12, 5)
>>> p2.distance()
13.0
Building Class Point (Good)

from math import sqrt

class Point:
"""Two dimensional point
"""

def __init__(self: ’Point’,


x: float,
y: float) -> None:
"""Initialize this point
>>> p = Point(3, 4)
"""
self.x = x
self.y = y

# and so on
Designing Another Class

A rational number consists of a numerator and denominator;


the denominator cannot be 0. Rational numbers are written like
7/8. Typical operations include determining whether the rational is
positive, adding two rationals, multiplying two rationals, comparing
two rationals, and converting a rational to a string.
Find the most important noun (good candidate for a class. . . ), its
most important attributes, and operations that this noun should
support.
Inheritance

I Many times, a new class will have much in common with an


existing class
I Copying-and-pasting from the old class, and then making a
few changes, is a bad idea
I What if you then discover a bug in the original class?
I What if you find that there are many classes you’d like to
create from the original one?
I Inheritance allows a new class to specialize an existing class
by specifying only what is different between them
I The class that inherits is called a subclass, and the class that
is inherited from is its superclass
Is-a

I Inheritance models ‘is-a” relationships


I The subclass “is-a” subset of the superclass
I e.g.
I The superclass could be Ship, representing all ships
I The subclass could be IndestructibleShip that “is-a” Ship
but cannot take damage
Has-a

I Be careful not to use inheritance to model has-a relationships


I For example, consider Segment (line segments) and Point
(points)
I Is a point a line segment? — No
I Is a line segment a point? — No
I Inheritance cannot be used here
I A line segment “has” two points, not “is” two points
I Has-a relationships are modeled with instance variables, not
inheritance
Inheriting from a Class

I So far all classes have started with class Name:


I To inherit from another class, add the superclass name in
parentheses

class IndestructibleShip(Ship):
...

When calling a method on an IndestructibleShip:


I If the method exists in IndestructibleShip, it is called
I Otherwise, the one in Ship is called

You might also like