Program design - University of Oklahoma

Download Report

Transcript Program design - University of Oklahoma

Day 2 – Lesson 6
Program Design
Python Mini-Course
University of Oklahoma
Department of Psychology
1
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Lesson objectives
1. Describe the four major
programming paradigms that can be
used in Python
2. List the steps in the program design
process
3. Use pseudocode to develop the logic
for a simple algorithm
2
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Sources
Wikipedia
http://en.wikipedia.org/wiki/Imperative_programming
http://en.wikipedia.org/wiki/Procedural_programming
http://en.wikipedia.org/wiki/Functional_programming
http://en.wikipedia.org/wiki/Object-oriented_programming
http://en.wikipedia.org/wiki/Event-driven_programming
http://en.wikipedia.org/wiki/Pseudocode
3
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Programming paradigms
 The fundamental styles of
computer programming
 Paradigms describe
 concepts and abstractions used to
represent the elements of a program
 the steps that compose a
computation
4
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Programming paradigms
 Imperative / Procedural
 Fortran, COBOL, Pascal, BASIC
 Functional
 Mathematica, R
 Object-oriented (OOP)
 C++, Java, Python
 Event-driven
5
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Procedural programming
 Programs are step-by-step
instructions of how to perform a
task
 Specify a series of algorithmic steps
 Emphasizes use of re-usable
procedures (a.k.a functions) to
change the program state
6
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Procedural programming in
Python
# create empty list
target = []
# iterate over each thing in source
for item in source_list:
trans1 = G(item)
trans2 = F(trans1)
# add transformed item to target
target.append(trans2)
7
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Functional programming
 Based on the lambda calculus
 Programs are based on the
systematic evaluation of
mathematical functions
 Programs describe what to do,
not how to do it
8
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Functional programming in
Python
# Define how to apply two generic
# transformations
compose2 = lambda A, B: \
lambda x: A(B(x))
# Apply two specific functions
# F and G to each item in source
target = \
map(compose2(F, G), source_list)
9
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Object-oriented programming
 Programs are collections of
“objects” that interact
 Objects have attributes and
behaviors, much like things in
the real world
10
Python Mini-Course: Day 2 - Lesson 6
4/18/09
OOP in Python
 Python is inherently OOP
 Everything in Python is an object
 We’ll discuss OOP in detail on Days 6
and 7
 Example: see oop.py
11
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Event-driven programming
 The flow of the program is
determined by events
 user actions (mouse clicks, key
presses, etc.)
 messages from other programs
 sensor outputs
 Used for GUIs
12
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Event-driven programming
 Programs have two sections
 Event detection
 Event handling
 Example: StudyXXX.py
13
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Program design process
Identify the program components
you will need

Functions, object classes, event
handlers, etc.
2. Design the interfaces and stub
them out

Keep in mind the principles of
encapsulation and generalization
1.
14
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Program design process
3. Write a test routine
4. Fill in the program components.
Test and debug them
5. Write the main routine
6. Test and debug the program
15
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Pseudocode
 Pseudocode is a compact and
informal high-level description of an
algorithm
 Uses the structural conventions of some
programming language
 Is intended for human reading rather
than machine reading
 Omits details that are not essential for
human understanding of the algorithm
16
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Pseudocode example
bubbleSort for a list of sortable items:
start loop
set swapped = false
for each item in the list (except the last one)
if item > next_item then
swap( item, next_item )
set swapped = true
end if
end for loop
if swapped is true then repeat loop
end loop
17
Python Mini-Course: Day 2 - Lesson 6
4/18/09
Using pseudocode
 Purpose
 Easy for humans to understand
 Is a compact and environmentindependent description of the key
principles of an algorithm
 Useful for:
 sketching out the structure of the
program before coding
 documenting algorithms for publication
18
Python Mini-Course: Day 2 - Lesson 6
4/18/09