1 of 16

CSE 163

Object-Oriented Programming I�

Suh Young Choi�

🎶 Listening to: Orbitron

💬 Before Class: If you could pick a language to be instantly fluent in but could never visit any country where it’s officially recognized, what would you pick?

2 of 16

Checking in

Weekly Tokens

  • Can be used to resubmit or late-submit a Checkpoint or Reading Assignment
  • Checkpoints and RAs are re-opened after the initial grading period is over

Project/Portfolio

  • Fairly comprehensive Q&A in Ed Post #325
  • Your submission for Part 1 determines which option you will pursue, along with any groupmates if you do a Project
  • Written components assessed for completeness and quality; code components assessed for correctness and validity

2

3 of 16

Announcements

  • Reading Assignment 3 due tonight (2/2)

  • THA 1 FINAL RESUBMISSIONS due tomorrow night (2/3)!

  • THA 3 Peer Reviews due Wednesday night (2/4)!

  • Project Proposals & Portfolio Vision Statements due Thursday night (2/5)!

3

4 of 16

Last Time + This Time

Last Time

  • Data Literacy
  • Data Context
  • Humanistic Computing

This Time

  • Objects (state and behavior)
  • References
  • Defining a class
  • Instantiating

4

5 of 16

Objects

  • An object stores state and provides behaviors that operates on that state
  • Each object has its own state, but objects of the same class can have the same behaviors

5

State:

self.name

Behavior:

.thwip()

State:

self.name

Behavior:

.thwip()

6 of 16

Objects

6

spider_man = SpiderMan(‘Peter Parker’)

amazing_sm = SpiderMan(‘Peter Parker’)

State:

self.name

Behavior:

.thwip()

State:

self.name

Behavior:

.thwip()

7 of 16

Objects

7

spider_man = SpiderMan(‘Peter Parker’)

amazing_sm = SpiderMan(‘Peter Parker’)

spider_man

State:

self.name

State:

‘Peter Parker’

Behavior:

.thwip()

amazing_sm

State:

self.name

State:

‘Peter Parker’

Behavior:

.thwip()

8 of 16

Objects

8

State:

self.name

State:

self.name

State:

‘Peter Parker’

Behavior:

.thwip()

spider_man = SpiderMan(‘Peter Parker’)

amazing_sm = SpiderMan(‘Peter Parker’)

spider_man.thwip()

spider_man

State:

‘Peter Parker’

Behavior:

.thwip()

amazing_sm

9 of 16

References

9

State:

self.name

State:

self.name

State:

‘Peter Parker’

Behavior:

.thwip()

spider_man = SpiderMan(‘Peter Parker’)

amazing_sm = spider_man

spider_man.thwip()

spider_man

State:

‘Peter Parker’

Behavior:

.thwip()

amazing_sm

10 of 16

References

10

State:

‘Peter Parker’

Behavior:

.thwip()

spider_man = SpiderMan(‘Peter Parker’)

amazing_sm = spider_man

spider_man.thwip()

spider_man

amazing_sm

= SpiderMan(‘Peter Parker’)

11 of 16

Multiple references �be like

11

12 of 16

Class

  • A class lets you define a new object type by specifying what state and behaviors it has
  • A class is a blueprint that we use to construct instances of the object

Here is a full class

12

class SpiderFolk:

def __init__(self, name: str) -> None:

self.name: str = name

def thwip(self) -> None:

print(self.name + ‘: Thwip!')

A class definition

An initializer that sets fields (state)

A method (behavior)

13 of 16

Spiderfolking�Around

13

s1 = SpiderFolk(‘Cindy’)

s2 = SpiderFolk(‘Jessica’)

s3 = s1

s1.thwip()

s2.thwip()

s3.thwip()

s1

name:

‘Cindy'

s2

name: ‘Jessica'

s3

# Cindy: Thwip!

# Jessica: Thwip!

# Cindy: Thwip!

14 of 16

Note to Self

  • In the SpiderFolk example, every method (initializer or otherwise) takes a parameter called “self”
  • This indicates which instance the method is being called on

  • On the first line, self references Cindy
  • On the second line, self references Jessica
  • On the third line, self references Cindy

14

class SpiderFolk:

def __init__(self, name):

self.name = name

def thwip(self):

print(self.name + ‘: Thwip!')

s1 = SpiderFolk(‘Cindy')

s2 = SpiderFolk(‘Jessica')

s3 = s1

s1.thwip()

s2.thwip()

s3.thwip()

15 of 16

Group Work:

Best Practices

When you first working with this group:

  • Introduce yourself!
  • If possible, angle one of your screens so that everyone can discuss together

Tips:

  • Starts with making sure everyone agrees to work on the same problem
  • Make sure everyone gets a chance to contribute!
  • Ask if everyone agrees and periodically ask each other questions!
  • Call TAs over for help if you need any!

15

16 of 16

Next Time + Before Next Time

Before Next Time

  • RA 3 due tonight!
  • Make final submissions on THA 1 by tomorrow night
  • Keep working on Project/Portfolio Part 1
  • THA 3 Peer Reviews

Next Time

  • More OOP and classes

16