fundamentals of programming

44
Fundamentals of Programming -- A “starter” on the software development menu

Upload: atulnr

Post on 28-Jan-2015

273 views

Category:

Technology


0 download

DESCRIPTION

An introduction to What is programming and Writing simple programs. The slides here lets an aspiring software developer get on with programming without going through the rigours of Computer architecture, Algorithms/Data structures in their full glory. Material is not retricted to any particular programming language and deals mostly in generic terms -- how to design a program.

TRANSCRIPT

Page 1: Fundamentals of programming

Fundamentals of Programming

-- A “starter” on the software development menu

Page 2: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

2

What is programming

• Providing step by step instructions to computer for performing some task (functionality)

• Task can be– sorting words, searching for a txt pattern in a file– maintaining employee related information (CRUD)– Bank a/c transactions (Deposits, Withdrwals, Xfers)– Finding all/shortest/cheapest routes between cities– Optimally storing user data and facilitating Queries,

Updates on the data (e.g. MySQL and other RDBMSes)– etc., etc. (“limited only by imagination”)

Page 3: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

3

A simple program (using Java)import java.util.Date;

class Hello {

public static void main(String[] args) {

String s = args[0]; // the first argumentDate d = new Date(); // current time objectSystem.out.println("Hello " +s +"! Current date/time is " +d);

}}

• Above program listing only to show how program source code looks like.

• Most programs look similar (regardless of the language used)

Page 4: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

4

Overview of program development

• Broadly speaking two steps:– Writing source code for the program– Building executable binary code to run the program

• Focus here is the “Writing source code” part– Specifically on thinking through the source code

(“program design”)– Actual source code is language specific (e.g. Java, ‘C’)

• “Building executables” is simple using the tools provided by the Software Development Kit (SDK)

Page 5: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

5

“Program design” – general principles

• Programs deal with data items, operations on data, reading/writing data from files and other devices (Input, Display, Network, etc.)

• So, first step is thinking through the steps required to accomplish the program’s task

• Example: • Read words from the specified file on disk• Sort words alphabetically in program data area• Writing sorted word list to another specified file

Page 6: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

6

“Program design” – general principles (continued)

– Program source code include the steps to perform the required task (e.g. sorting words from a file)

– Each step instructs the computer to perform an operation on specified data items

– Example: String s = s1 + s2;• Above step instructs computer to create a new string ‘s’

by appending string s2 to s1

– Example: A = B + 100;• Instructs computer to add 100 to a number stored in

data ‘B’ and store result in data ‘A’

Page 7: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

7

Program design – general principles (continued)

• Other types of program statements include– Subroutine calls to other modules of the program

• Subroutines written as part of same program devlpmnt &• Subroutines available as part of external packages

– I/O statements (more typically I/O subroutine calls) for • reading from files and writing to files (storage devices)• Reading and writing from/to input/output devices

– E.g. Keyboard, Mouse, Display, Network devices

– subroutine calls for dynamic allocation/deallocation of data structures

Page 8: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

8

Subroutines and Libraries• Most programs require many, many (>>100) simple steps• Most programs use common steps for common sub-tasks like

printing, reading, etc.• For sake of Reusability, readability of source code and easier

maintenance, the common parts are built as “subroutines” and made available in packages

• Subroutines packaged into Libraries• Libraries can be external (system libraries or third-party libraries)

and/or• Libraries can be developed as part of program being developed• Lot of (Most of) program source code is calls to subroutines to

perform some common functionality (e.g. printing)

Page 9: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

9

Subroutines and Libraries

• The terminology used in these slides is not specific to any particular programming language

• Similarly, scope, packaging, call interfaces do vary across programming languages

• Important thing is to understand the concept and its purpose

Page 10: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

10

Available programming elements

• Following elements are made available by programming languages’ SDK– Functions and Classes (detailed discussion follows)– Data variables– Operations for different types of data– Controlled program termination (e.g. Exceptions, Returns)– Multiple iterations through a block of code (e.g. ‘for’ loop)– Conditional execution of blocks of code (e.g. if (..) then {..})– Subroutines libraries for

• I/O from files and devices • Working with common data “structures” (e.g. strings, arrays, lists)

Page 11: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

11

Available programming elements (continued)

• The above list of programming elements is not comprehensive

• But is adequate to begin designing simple programs (e.g. to process data from files, read

• Typically most applications will use sophisticated tools that provide libraries of subroutines to accomplish complex tasks (e.g. online ticket reservations, Credit card usage, Telecom billing, Games, weather modeling, web servers, etc. )

Page 12: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

12

Programming elements explained

• The next few slides explain each of the programming elements listed on previous slides

• The “End of Programming elements explained” slide is the last of these slides

Page 13: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

13

Functions• Programming steps may (and usually are) combined in units that perform a

logically complete part of processing• E.g. reading a line from a specified file; writing a line to a file, sorting a list of

words• We are referring to these units as functions (other terms used are “methods”,

subroutines)• A typical program defines a lot of functions and each function may call few/many

other functions• Program is a collection of one/more functions

– Some developed as part of the program development– Some available as part of external libraries

• A program has one function that serves as “entry” point into the program – the first application function that is called on program startup.

• Java, ‘C’ languages require this entry point function to be named as “main” – not sure about other programming languages.

Page 14: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

14

Examples of “Functions”import java.util.Date;

class Hello {

public static void main (String[] args)

{ String s = args[0]; // the first argument

Date d = new Date(); // current time object

System.out.println("Hello " +s +"! Current date/time is " +d);

}}

• “main”, “Date” and “println” in above example are Functions• The example defines the “main” function• And, the “println” function is called from the “main” function

Page 15: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

15

Classes• Object Oriented Programming languages provide a more sophisticated

programming element called a ‘Class”• A “Class” allows encapsulating all interactions with its type of thing that the

program works with. It’s a template for one kind of things. (better definition reqd)• Example: a Banking application program works with Accounts. It allows account

holder to check balance, deposit, withdraw and transfer amounts– Thus, a class named “UserAccount” may be created to encapsulate all account interactions– The class may provide functions named balanceCheck, deposit, withdraw and transfer functionality

• Apart from Encapsulation there are other benefits of using OOP languages for better design (Inherit, polymorph, interfaces, access protected, etc.)

• Using OOP language does not automatically ensure better design/programming• Better design possible in non-OOP languages too. • OOP languages promote better design through ready-made constructs. • Java is OOP language; So is C#. Traditional ‘C’ is not.

Page 16: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

16

Data variables• Data is handled in programs through named elements called “variables”• So,

– a program reads data from a file into a data variable– Program writes data from data variable into a file– Program performs arithmetic operations on numeric data– Program performs text operations on text data– A program function sends data to another function– A function returns the result of processing to calling function– And so on…

• Variables may hold single values (e.g. a number or a word or a string of words) OR• Single valued variables have data types e.g. numeric, alphanumeric, decimal, boolean, etc.• Variables may hold complex structures which in turn contain other “single valued” variables or

structures– Example: variable “EmployeeInfo” defined as follows:– Struct EmployeeInfo {

• String Name;• String Age;• Struct Employee_last_3months_sal {

– Decimal last_mth_sal– Decimal last_to_last_mth_sal– Decila last_to_last_to_last_mth_sal; }

• String Designation; }

Page 17: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

17

Examples of Data variables

• Examples:– Single valued variable: String AccountName;– Struct EmployeeInfo {

String Name;String Age;Struct Employee_last_3months_sal {

Decimal last_mth_sal;Decimal last_to_last_mth_sal;Decimal last_to_last_to_last_mth_sal;

}String EmployeeDesignation;

}

Page 18: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

18

Operations

• Programming languages support operations on data variables• Operations supported are specific to data types. Some

operations are common across types• Examples:

• Arithmetic operations (+, -, *, /, **, %, etc.)• Text operations (concat, sizeof, etc.)• Logical operations on booleans (AND, OR, NOT,..)• Assignment operation (assign value of one variable to another)• Etc. etc.

• Parenthesis (brackets) may be used to combine operations and change precedence of operators (don’t worry for now – you will understand this as you learn)

Page 19: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

19

Examples of Operations• Integer X = 100; // Assignment operation

• Integer Y = X; // Assignment operation

• Integer A = B + 100; //arithmetic addition

• Integer A = A – 10; // arithmetic subtraction

• Boolean B = A && C // Logical AND operation

• X < Y //relational “less-than” operator ‘<‘

• String s = a || b // concatenation operator ‘||’ on two strings

• Syntax shown above not specific to any particular programming language. Examples only show kinds of operations supported by most languages

Page 20: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

20

Conditional execution of code block

beginIf: //labelled statements ok only in small code blocks. Generally not recommendedIf (I == 100) {

System.out.println(“variable I reached 100”);Return (1);

} else {System.out.println (“printing line “ +I +” to display”);I = I + 1;Go to beginIf;

}• Above source code shows Conditional execution depending on the value of

data variable ‘I’• Used to execute one of multiple code blocks depending on runtime condition• Other conditional execution constructs available:

– Switch ( ) {case…:{…}; case…:{…}; break endswitch // for evaluation against multiple values and corresponding codeblock execution – exit the switch using the ‘break’ statement

Page 21: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

21

Iterations – “loops”

• Programs often need to execute a block of code, multiple times in a loop – until an exit condition is encountered.

• Example: calculating ‘n’th power of a number • The “for” loop is a programming element for this purpose• for (i=0; i<100; i++) {… code block ….}• The code block in the statement above is executed for values

of i from 0 to 99.• There are other forms of iteration programming mechanisms• ‘while loops’, foreach loops, do while loops

Page 22: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

22

Controlled program termination

• The language SDK provides a way to exit a program while returning an exit code

• E.g. exit(-23)• Used to immediately exit a program on encountering a

particular condition• Also, when a program encounters an error condition it can

“throw” an exception event which is subsequently “caught” by calling method (this is adequately accurate for now) and handled as coded (programmed) in the application.– Language exception handling mechanism is a little involved and the learner

may chose to drop this for later (but this is a very important mechanism that any programmer needs to be comfortable with)

Page 23: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

23

File i/o subroutines• Most SDKs provide subroutines for reading files and writing files to the machine’s file system• Common functions provided include:

• Create• Open• Read• Seek• write.• Close• Delete

• The SDK also provides similar functions for accessing other devices (keyboard, mouse, network card, etc.)

• A typical application usually uses a subroutine library that provides GUI, Network APIs that are directly/indirectly based on the core subroutines listed above. Such libraries are also called framework and provide a “high level” value like GUI construction, HTTP access, FTP access, etc.

• Also typical applications today use some RDBMS (e.g. Oracle, MySQL) for data storage and access. When doing so, they use the SDK provided by the RDBMS. This SDK implements SQL programming model

Page 24: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

24

Subroutines for working with data structures

• Some language SDKs also provide subroutines for working with common data structures like Arrays, Lists, Maps, queues and so on

• Arrays example: array of employee-info structures for company employees. Useful when number of elements is fixed (more-or-less)

• Linked lists: useful when the number of elements keeps changing rapidly. . New elements are allocated – deleted elements are deallocated. Each element points to next one.

• Stack: used for LIFO (last in first out) situations. Push-pop model• Queue: used for FIFO (first in first out) implementations. Very

common for many real life situations (bus queue, ATM queue)• These subroutines make programming easier as the developer can

use ready code for the required functionality.

Page 25: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

25

Subroutines used

• In these slides we have only touched upon very few subroutine libraries that are commonly seen in programs

• In reality, many (most) applications use a number of external tools, platforms, products. And most of these external softwares provide APIs for integration with applications.

• The APIs are packaged as subroutine libraries.• Examples: JDK, Oracle RDBMS, Tuxedo, BRIO,

Page 26: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

26

End of Programming elements explained

• Last slide of the “Programming elements explained” group of slides.

Page 27: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

27

Pseudo code, flowcharts,

• Combine the available programming elements to construct program logic

• Document your program logic (program design) – atleast during initial stages of learning

• Flowcharts and Pseudo code are commonly understood documentation for program logic

• Don’t be hung up on pseudo code syntax as this is basically for human reading (not machine reading)

• But don’t “create” syntax that no one understands

Page 28: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

28

4 Examples of program design

• Word count program• Word sorting program• Employee information maintenance (CRUD)• Simple JDBC example

Page 29: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

29

Word count program• Program requirement: Read specified file and count number of words in the file. Print each word

found on separate line and finally the overall word count to standard output

• Design:– Will use a separate function named getword() for reading words (one at a time). Getword() will

return -1 on EOF» Main() {

• Validate command line (should specify file name to be used)• Open specified file • While getword() call does not return “-1 “

• Print the current_word set by getword• Add 1 to word count

• endWhile• Print current_word if not blanks; // this is the last word• Print word count;

» }» Getword() {

• Read one character at a time till oneword is complete or EOF• On word completion, return 0 with the word set in the current_word variable• On EOF return -1 with the current_word set

» }

Page 30: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

30

Word sorting (alphabetic) program• Data structure design:

• Last example used only single valued data fields• This example needs a sorted word list for which the order of words may need be changed on

every new word read• For simplicity lets assume we know max number of words and are willing to work with a fixed

array of as many elements• Still moving the words around within the array is expensive in terms of CPU, memory --

imagine shifting all the words “down” for the case where the last word read needs to be at the top of the array. Also, shifting words on every new word read is expensive

• Would be easier if we have next-word-reference stored along with each word. SO, first word has reference (array-index) to second word which in turn references third word and so on

• Then we only need to adjust the reference when a new word is to be inserted in the “middle” of the list

• So our data structure is as follows:Struct word_info {

String word; // the wordInt next _word; // index of “next” word in the array.

}Struct word_info Sorted_list[MAX_WORD_COUNT];

Page 31: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

31

Word sorting (alphabetic) program

• Algorithm Design:1. Read a word (reuse the getword() function from last example)2. Store first word directly into our word list3. Store second word above/below first word depending on its value. 4. Now we have a sorted list of two words5. Read next word from file6. Call get_correct_place() to get the correct place for the new word.7. Modify our sorted word list to put the new word in the correct place8. Repeat steps beginning step 5 until no more words to read

• Exercise: design the algorithm for get_correct_place(). Hint: compare new word with two halves (top and bottom) and identify the correct half where word should be placed. Repeat the comparison within the identied half to get the correct half-of-half. Repeat this procedure until you identify the correct position within the sorted list where the new word should be placed.

Page 32: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

32

Employee information maintenance (CRUD) – program requirements

– “CRUD” stands for ‘Create’, ‘Read’, ‘Update’, ‘Delete’ – common terminology in industry

– Requirements:» Should support CRUD functions» Use a text file as database.» One line per employee record» Record format:

• Employee id (use value -1 for deleted records and reuse)• Employee full name• Employee date of birth (YYYYMMDD format)• Employee designation code (level ‘1’ to ‘6’)• Employee full address

» Fields a record separated by a semi-colon character (SO, semicolon cannot be accepted as part of a field value)

Page 33: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

33

Employee information maintenance (CRUD) – program requirements» Program command line required:

• emp <opcode> <emp-id> <opcode specific params>• Where,• <opcode> indicates operation code (C, R, U, or D)• <emp-id> is employee ID number. Not reqd for

opcode ‘C’ (create). For create op, Emp-id is returned

• <opcode specific params> are parameters specific to the opcode being requested• emp C <name> <DOB> <Desig code>

<address>• emp R <emp-id>• emp U <emp-id> <same params as for C>• emp D <emp-id>

Page 34: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

34

Employee information maintenance (CRUD) – data structure design

• Struct empInfo {Int emp-id;String name;Date DOB // date of birth YYYYMMDDInt emp-designation // {1-6}String Address

}

Page 35: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

35

Employee Information Maintenance (CRUD) – program design

• This example demonstrates modularity design(although for a simple case)

• One can see that there are some common processing requirements across the request opcodes– Command line processing to determine request opcode– Reading employee record– Writing employee record– Locating employee record– Validating employee record field values (also checking for

duplicates)– Adding employee record

Page 36: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

36

Employee Information Maintenance (CRUD) – program design

• We will use two classes:Class eim { // Defines following methods:

Main() to drive the program execution by calling required functionsParse_command_line() to validate command line and determine opcode

}Class empInformation { // everything about employee

Int empId;String empName;…… // see slide on correspondnig data structure for other data variablesValidate(); // validate parameters when creating new objectLocate(); // search thru text file for employee record of spec’d empIdSearch_empty_slot(); // search thru file for emp record line not in useOverwrite(); // overwrite specified line in file with specifie\d emp recordAppend(); // append emp record to fileDelete(); // mark line as not in useRead(); // read record for spec’d emp ID and return field values as string

}

Page 37: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

37

Employee Information Maintenance (CRUD) – program design

• main () //drives program by calling reqd functions– Initialization (open the database file and perform init tasks)– Call parse_command_line( ) to validate command line and get

the parameters to the program» If invalid command line parameters display error and exit

– If Create operation requested {» Call locate() to check for already existing employee ID» If employee record already exists show error msg and exit» Search_empty_slot» If slot found, overwrite the record with new record info» Else append new record info to database file» Print success messge and Exit

– }

Page 38: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

38

Employee Information Maintenance (CRUD) – program design

» If Read operation requested {• Locate record for specified employee ID in database• Read record info into a employeeInfo structure• Print the structure• Exit

» } » If Update operation requested {

• Locate record for specified employee ID in database• Overwrite record info with update parameters• Print success message• Exit

» }» If Delete operation requested {

• Locate record for specified employee ID in database• Mark record as deleted (overwrite with emp ID = -1)• Print success message• Exit

» }

Page 39: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

39

Employee Information Maintenance (CRUD) – program design

• Exercise: Improve the design of the Employee Information Maintenance program by checking for error conditions at various points and proper error handling.

• Hint:– Error condition if Employee ID does not exist for

Read/Update/delete requests– Database read failure (due to disk failure, bad

connection, etc.)– Identify other error conditions and handle appropriately

Page 40: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

40

Exercise: Write the source code for the 4 examples

• Well, atleast the first 3 examples…

Page 41: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

41

Software development process– So far we focused on program design of individual programs– Overall software development process of a complete software

application is more involved– Outline of the phases involved in software development

» Requirements gathering and specification» Functional design and specification» Architecture and design

• Architecture decisions (web-based, client-server, multithreaded, etc.)

• Choosing primary platforms (J2EE, .NET, PHP, SQL, etc.)

• Identifying subsystems in the application• DB design, UI design, • Designing Classes, methods, variables

Page 42: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

42

Software development process (continued)

» Test plan, test cases design (unit testing and system tests)» Documentation planning and begin documentation » Creating build scripts (e.g. using Ant)» Writing test cases in some details (scenarios, test items,

approach, etc.)» Source code writing» Compiling modules and unit testing» Building complete application and system testing

– Many of the steps above are conducted in parallel and undergo multiple iterations

– Typically the process is split across team (size depends on scope of application)

Page 43: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

43

Software design

• More involved than designing a relatively simple program• Example: online ticket reservation system• In addition to the main software functionality required, a

developer has to design for – Performance (response time, peak load)– Security– Scalability– Manageability of running the application– Usability (User experience design)– Maintainability (readability, modularity, extensibility, reusability)– And so on

• This is adequate for now

Page 44: Fundamentals of programming

Copyright by A.N.Ranpise ([email protected]) 2012. All Rights Reserved.

44

End of slides• This set of slides focused on Fundamentals of Programming

– What is software programming– How to design simple programs– Programming elements provided by SDKs – How to combine the provided programming elements to develop a useful program– A brief note on software development process– Considerations for non-trivial software design

• Should be followed up by a course on programming using a specific language (e.g. Java)

• Most useful applications will use tools for GUI, DB, and other purposes. Separate training required to learn those tools (e.g. HTML 5, CSS, Javascript, MySQL). A practical approach is to learn one tool in one/more categories and use the learnings there when working with other tools in that category

• Having completed the above learnings you should be ready to embark upon a journey in software development. Good luck!!