Post on 05-Jul-2018
transcript
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 1/21
Fundamentals of Programming
Guiding teachers
Lect. PhD CZIBULA Istvan
Assist. PhD GACEANU Radu
Assist. PhD MOLNAR Arthur
Assist. PhD MIHOC Tudr
PhD student BOCICOR Maria!Iu"iana
PhD student MARIAN Zsu#sanna
ScheduleLectures: 2 hours/week
Seminars: 2 hours/week
Labs: 2 hours/week
Course URL
Course page: http://cs.ubbcluj.ro/~istvanc/fp
E$ai"% istvanc@cs.ubbcluj.ro
Please use your ubbcluj.ro email.
Do not send emails from other accounts (yahoogmail etc!
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 2/21
"ims of the activity
• To know the key concepts of programming
•
To know the basic concepts of software engineering (design, implementation andmaintenance of software systems)
• To understand the basic software tools
• To learn Python programming language, and to get used to Python programming,
running, testing, and debugging programs
• To ac!uire and impro"e programming style
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 3/21
#ourse content
# $ntroduction to software de"elopment processes
2 Procedural programming
% &odular programming' ser defined types *b+ect based programming
-e"elopment principles . ayered architecture
0 -e"elopment principles . & -iagrams
1 Program testing and inspection
3ecursion
4 5lgorithms comple6ity
#7 -i"ision method
## 8acktracking method
#2 9earch algorithms and their comple6ity
#% 9ort algorithms and their comple6ity
#' *"er"iew
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 4/21
"ctivity and grading
ab (%7) ab documentations and programms
T (%7) Practical test (in the regular session)
; ('7) <ritten e6am (in the regular session)
9 7 7 9eminar acti"ity
Attendance is compulsory to all the activities
The exam is conditioned by minimum presence !"# Course
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 5/21
Lecture $% &ntroduction to development processes
• <hat is programming
• 8asic elements of a Python program
• 5 simple featuredri"en de"elopment process
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 6/21
'hat is programming
(ard)are and soft)are(ard)are computers (desktop, mobile, etc) and related devices
Soft)are programs or systems which run on hardware
Programming language notation that defines synta6 and semantics of programs
Python: a high le"el programming language =#> $t is a great language for beginner
programmers =%>
Python interpreter : a program which allow us to run/interpret new programs
Python standard library: builtin functions and types =2>
Program # ?ello world
print (@?ello worldA)
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 7/21
'hat computers do
B storage and retrie"al
internal memory
hard disk, memory stick
B operations
processor
B communication
keyboard, mouse, display
network connector
&nformation and data
*ata collection of symbols stored in a computer (eg #2% decimal number or @abcA string are
stored using binary representations)
&nformation interpretation of data for human purposes (eg #2%, @abcA)
&nformation and data processing
B $nput de"ices produce data from information (eg #2% taken from keyboard)
B -ata is stored in memory (eg binary representation of #2%)
B *utput de"ices produce information from data (eg #2% displayed from a binary
representation)
+asic operations of processors
B binary representation
B operations on binary representations (and, or, notD add, etc)
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 8/21
+asic elements of a Python program
Program 2 5dding two integers
E 3eads two integers and prints the sum of thema F input(G;nter the first number: G)b F input(G;nter the second number: G)c F int(a) H int(b)print(GThe sum of G, a, G H G, b, G is G, c)
Lexical elements
5 Python program is di"ided into a number of lines
Comments start with a hash (E) character and ends at the end of the line
&dentifiers (or names) are se!uences of characters which start with a letter (aI, 5J) or an
underscore (K) followed by Iero or more letters, underscores, and digits (74)Literals are notations for constant "alues of some builtin types
*ata model
All data in Python programs is represented by ob+ects, an ob,ect being Lthe PythonAs
abstraction for dataM
5n ob,ect has:
B an identity we may think of of it as the ob+ectAs address in memoryD
B a type which determines the operations that the ob+ect supports and also
defines the possible "aluesD
B a value*nce an ob+ect has been created, its identity and type cannot be changed
The "alue of some ob+ects can change *b+ects whose "alues can change mutable ob,ects
*b+ects whose "alues cannot be changed immutable ob,ects
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 9/21
Standard types
Types classifies "alues 5 type denotes a domain (a set of "alues) and operations on those
"alues
-umbers Numbers are immutable once created, their "alues cannot be changed
int (integers):
B 3epresents the mathematical set of integers (positi"e and negati"e, unlimited
precision)
B *perations: H, , O, /, see =2, section ''>
B iterals: #
bool (booleans):
B 3epresents the the truth "alues True and alse
B *perations: =2, section '#>
B iterals: alse, TrueD 7, #
float (reals):B 3epresents the mathematical set of double precision floating point numbers
B *perations: H, , O, /, see =2, section ''>
B iterals: %#'
Se.uences
B 3epresents the finite ordered sets inde6ed by nonnegati"e numbers
B et a be a se!uence
len(a) returns the number of itemsD
a=7>, a=#>, Q, a=len(a)#> represent the set of items
B ;6amples: =#, @aA>
Strings:
B 5 string is an immutable se!uenceD
B The items of a string are nicode characters
B iterals: @abcA, LabcM
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 10/21
/ariables
B Rariables are reser"ed memory locations to store "alues
B 5 variable has a name which refers to an ob+ectD so a "ariable has also a type,
and a "alue
B 5 "ariable is introduced in a program using a name binding operation
assignment
0xpressions
5n expression is a combination of e6plicit values, constants, "ariables, operators, and
functions that are interpreted according to the particular rules of precedence, which computes
and then produces/returns another "alue
;6amples:
B numeric e6pression: # H 2
B boolean e6pression # S 2
B string e6pression: @#A H @2A
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 11/21
Statements
9tatements are the basic operations of a program 5 program is a se!uence of statements
AssignmentB 5ssignment is a statement
B 5ssignments are used to (re)bind names to "alues and to modify items of
mutable ob+ects
B 8ind name:
6 F # E6 is a "ariable (of type int)
B 3ebind name:
6 F 6 H 2 Ea new "alue is assigned to 6
B 3ebind name of mutable se!uences:
y F =#, 2> Emutable se!uence
y=7> F # Ethe first item is bound to #
B +loc1
B 5 block is a piece of Python program te6t that is e6ecuted as a unit
B 5 se!uence of statements is a block
B 8locks of code are denoted by line indentation
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 12/21
(o) to )rite programs
+asic roles in soft)are engineering
Programmers2*evelopers
B se computers to write/develop programs for users
Clients2sta1eholders
B <ho is affected by the outcome of a pro+ect
Users
● Run programs on their computers.
5 soft)are development process is an approach to building, deploying, and possible
maintaining software $t indicates:
B <hat tasks/steps can be taken during de"elopment
B $n which order
<e will use: a simple featuredri"en de"elopment process
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 13/21
Problem statement 5 problem statement is a short description of the problem being sol"ed
Calculator Problem statement
5 teacher (client) needs a program for students (users) who learn or use rational numbers
The program shall help students to make basic arithmetic operation
Re.uirementsRe.uirements define in details what is needed from the client perspecti"e 3e!uirements
define:
B <hat the clients needD andB <hat the system must include to satisfy the clientU needs
Re.uirements guidelines
B 3ood re.uirements ensure your system )or1s li1e your customers expect%
4*on5t create problems to solve problems67
B Capture the list of features your software is supposed to do
B The list of features must clarify the problem statement ambiguities
B
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 14/21
Feature
Feature is a small, client"alued function:
B e6pressed in the form 8action9 <result> Sob+ectV,
action a function that the application must pro"ide
result the result obtained after e6ecuting the function
ob+ect an entity within the application that implements the
function
B and typically can be implemented )ithin a fe) hours (in order to be easy to
make estimates)
Calculator eature list
# Add a number to calculator
2 Clear calculator
% Undo the last operation (user may repeat this operation)
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 15/21
A simple feature:driven development process
B 8uild a feature list from the problem statement
B Plan iterations (at this stage, an iteration may include asingle feature)
B or each iteration
&odel planned features
$mplement and test features
5n iteration is a set period of time within a pro+ect in which you produce a stable, e6ecutable
"ersion of the product, together with any other supporting documentation
&teration will result in a working and usefoul program for the client (will interact with the
user, perform some computation, show results)
;6ample: iteration plan
&teration Planned features
$# # Add a number to calculator
$2 2 Clear calculator
$% % Undo the last operation (user may repeat this operation)
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 16/21
&teration modeling 5t the beginning of each iteration you must understand the work re!uired to implement it Wou
must investigate2analy;e each feature in order to determine work items/tasks Then, work
items are scheduled ;ach work item will be independently implemented and tested
&teration $ : Add a number to calculator or simple programs (eg Calculator), running scenarios help de"eloper to understand what
must be implemented 5 running scenario shows possible interactions between users and the
program under de"elopment
Running scenario for adding numbers
ser Program -escription
a 7 9hows total
b #/2 5dd number to calculator
c #/2 9hows total
d 2/% 5dd number to calculator
e /0 9hows total
f #/0 5dd number to calculator
g # 9hows total
h 0/0 5dd number to calculator
i 7 9hows total
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 17/21
'or1 items2tas1s
3uidelines for tas1s
B -efine a task for each operation not already pro"ided by the platform (Python),
eg T#, T2
B -efine a task for implementing the interaction between ser and Program (ser
$nterface), eg T'
B -efine a task for implementing all operations re!uired by $, eg T%
B -etermine dependencies between tasks (eg T' V T% V T2 VT#, where V
means depends on)
B 9chedule items based on the dependencies between them
bottom up: schedule first task without dependencies, or with
dependencies scheduled before
T# Compute the greatest common di"isor of two integers (see g, i)
T2 5dd two rational numbers (see c, e, g, i)
T% $mplement calculator: init, add, and total
T' $mplement user interface
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 18/21
Tas1 $% Compute gcd
Test case
5 test case specifies a set of test inputs, e6ecution conditions, and e6pected results that you
identify to e"aluate a particular part of a program
$nput params: a, b gcd (a, b): c, where c is the greatest common di"isor of a and b
2 % #
2 ' 2
0 ' 2
7 2 2
2 7 2
2' 4 %
2 7 Ralue;rror
7 2 Ralue;rror
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 19/21
&f statement
The if statement is used for conditional e6ecution
gcd if statement and tests
def gcd(a, b): GGG 3eturn the greatest common di"isor of two positi"e integers 3aise Ralue;rror if the parameters are negati"e integers GGG if a FF 7: return b else: if b FF 7: return a
def testKgcd(): assert gcd(7, 2) FF 2 assert gcd(2, 7) FF 2 testKgcd()
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 20/21
'hile statement
The while statement is used for repeated e6ecution as long as an e6pression is true
gcd while statement and tests
def gcd(a, b): GGG 3eturn the greatest common di"isor of twopositi"e integers -oc test e6amples: GGG if a FF 7: return b else: if b FF 7:
return a else: while a XF b: if a V b: a F a b else: b F b a return a
def testKgcd(): assert gcd(7, 2) FF 2 assert gcd(2, 7) FF 2 assert gcd(2, %) FF # assert gcd(2, ') FF 2 assert gcd(0, ') FF 2 assert gcd(2', 4) FF %
testKgcd(
8/15/2019 Fundamentele Programarii CURS 1
http://slidepdf.com/reader/full/fundamentele-programarii-curs-1 21/21
References# The Python language reference http :// docs python org /py % k /reference /inde6 html
2 The Python standard library http :// docs python org /py % k /library /inde6 html % The Python tutorial http :// docs python org /tutorial /inde6 html