cit 590 intro to programming object oriented programming

9
CIT 590 Intro to Programming Object oriented programming

Upload: nora-stevens

Post on 02-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CIT 590 Intro to Programming Object oriented programming

CIT 590Intro to Programming

Object oriented programming

Page 2: CIT 590 Intro to Programming Object oriented programming

Agenda• Finish up functional programming• More on classes• More discussion of style

Page 3: CIT 590 Intro to Programming Object oriented programming

Sieve example• Functional programming lends itself to recursion really

easily• Erasthothenes algorithm for getting rid of composite

numbers in a list of increasing numbers beginning with the number 2

• An ancient algorithm for finding prime numbers• From any list of increasing numbers remove the multiples

of any number in the list • Easy for list of size 1• Can I do this recursively???

Page 4: CIT 590 Intro to Programming Object oriented programming

The 3 argument version of the reduce function

• We have already seem reduce(function, list)• There is a 3 argument variant which is

• Reduce(function, list, identity element/first element for the process

• Sorting examples • Define insertion of an element into a sorted list • Now use a reduce operation

Page 5: CIT 590 Intro to Programming Object oriented programming

Quicksort• List comprehension = badass • Quicksort.py - how to do something like quickSort in very

few lines of code

Page 6: CIT 590 Intro to Programming Object oriented programming

More examples of classes• Stacks• Last in first out datastructure• Python does not have stacks but you can use a list to

simulate stacks• See stacks.py in the repository

Page 7: CIT 590 Intro to Programming Object oriented programming

Object oriented design• Think of your problem as a bunch of objects interacting

with each other• The way that an object interacts with another object is via

the methods• Library example

• A library is an object• Within the library object I need to have books … make that another

object• These books get issued by patrons … make a patron object• The library is the one that knows about all the patrons and all the

books.• An instance of a book does not need to care about patrons.

Page 8: CIT 590 Intro to Programming Object oriented programming

Inheritance revisited• How to call a method of your parent class?• What if the parent class does not want to define a method

completely? • We will explore this concept more fully when we do abstract

classes in Java

• animals.py has examples of this

Page 9: CIT 590 Intro to Programming Object oriented programming

More examples of refactoring

We’ll refactor refactoringExample.py

Main goal = showing you how to declutter the main function