Transcript Document
Software Development 1. Requirement Specification 2. Analysis 3. Algorithm Design 4. Implementation 5. Testing 6. Documentation Slide 1 1. Requirement Specification The first, but the most important step in the process of problem solving We have to understand exactly what the problem is:What is needed to solve it ? What the solution should provide ? Whether there is any constraints or special conditions ? Slide 2 1. Requirement Specification (cont‘d) How precisely we can define a problem depends on our degree of familiarity with the problem domain If we are not familiar with the problem domain, acquire the knowledge the soonest Example : your friend want you to develop a program to calculate his salary tax ... Slide 3 1. Requirement Specification (cont‘d) Having acquired the information from the web site of HK government, you know that for the fiscal Year 2004/2005, the tax of a person is calculated on the basis of both (a) and (b):- (www.ird.gov.hk) (a) Progressive Tax rate NET Income/Cumulative First 30,000 Next 30,000 Next 30,000 Remainder Rate 2% 8% 14% 20% Tax/Cumulative 600 2,400 4,200 7,200 Slide 4 1. Requirement Specification (cont‘d) (b) Standard Tax rate which is simple formulae Tax = Income × 16% Tax charged by the HK government is thus equal to either (a) or (b), WHICHEVER LOWER where Net income = Income - Sum of all entitled allowances Slide 5 1. Requirement Specification (cont‘d) where the allowances are as follows: (i) Basic Personal Allowance 100,000 (ii) Dependent (Grand)/Parent Allowance 30,000 per Head It should be noted that (i) is always entitled; whereas (ii) is determined by the total no. of Dependent (Grand)/Parents of the taxpayer As a simple example, we assume a constraint on the problem domain: this program is for single taxpayers only Slide 6 1. Requirement Specification (cont‘d) Then we come up with the following requirement specification for our problem: (1) A single taxpayer can determine his salary tax by calculating his income and all entitled allowances; (2) After that, his tax is calculated on the basis of (a) Progressive Tax Rate and (b) Standard Tax Rate; (3) Then the chargeable tax will be either (a) or (b), whichever lower. Slide 7 1. Requirement Specification (cont‘d) We have to develop a program to do the following: 1. Prompt the user to interactively enter his (annual) income * 2. Prompt the user to interactively enter the no. of his Dependent (Grand)/Parents * 3. Calculate the chargeable tax as above mentioned 4. Output the result including his Income, Net Income, Progressive Tax, Standard Tax, and Chargeable Tax *subject to validation Slide 8 2. Analysis In the analysis phase we should identify the following: 1. Inputs to the problem, their form and the input media; 2. Outputs from the problem, their form and the input media; 3. Any special constraints or conditions; 4. Formulas or equations to be used. Slide 9 2. Analysis (cont’d) For the sample problem, we have the following: 1. Input -- consists of the income and no. of dependent (grand)/parents. All inputs will be interactively captured through the keyboard 2. Output -- four output items are expected : Net Income, Progressive Tax, Standard Tax, and Chargeable Tax 3. Constraints -- The program should not accept any input data with values beyond the reasonable range. Generally speaking, the upper limit for income and no. of Dependent (Grand)/Parents should be10,000,000 and 6. For both input data, their lower limits are 0. 4. Formulae -- given in the requirements specification Slide 10 3. Algorithm Design 1. An Algorithm is a sequence of a finite number of steps arranged in a specific logical order, which, when executed, produce the solution for a problem 2. Alternatively, an algorithm is a procedure that takes a set of input values and TRANSFORMs them into a set of output values Slide 11 3. Algorithm Design (cont’d) An algorithm must satisfy some criteria: 1. Unambiguousness -- Computer cannot cope with unambiguousness. Every step in an algorithm must be clear as to what it is supposed to do 2. Generality -- Take inch-to-cm conversion as example; a procedure that can only prints “1 inch is 2.54 cm” is no algorithm at all ! 3. Correctness -- it must solve correctly the problem for which it is designed 4. Finiteness -- a never-ending algorithm is unacceptable Slide 12 3. Algorithm Design (cont’d) 1. For a given problem, we have to design an algorithm and verify its correctness 2. An algorithm design must be done on paper, not just visualise in our minds 3. Therefore, we should be able to represent and document our algorithms using an appropriate representation tool 4. Any natural language, is no good for representing algorithms due to their ambiguousness 5. Pseudo-code and Flow-chart are appropriate tools for representing algorithms Slide 13 3.1 Representation of Algorithms On top of designing algorithms, Pseudo-code and Flow-chart can be used for 1. Communicating algorithms to computer laymen, e.g. users 2. Implementing algorithms as programs 3. Debugging logic errors in program 4. Documenting programs for future maintenance and/or expansion Slide 14 3.2 Pseudo-coding A Pseudo-code language is a semiformal, English-like language with a limited vocabulary that can be used to design and describe algorithms Regardless of their complexity, all algorithms can be described using only three type of basic control constructs: (1) sequence, (2) selection, & (3) repetition Slide 15 3.2.1 Sequence Control Structure The Sequence Control Structure is a series of steps that are executed in the order in which they are written in an algorithm, e.g., in our sample example 1. 2. 3. 4. 5. 6. Read_Annual_Income Read_No_of_Parents Calculate_Net_Income Calculate_Progressive_Tax Calculate_Standard_Tax Print_Chargeable_Tax Slide 16 3.2 .1 Sequence Control Structure (cont’d) It is useful to mark the beginning and end of a block of statement using ‘begin’ and ‘end’. Statements in the blocks are indented to improve readability, e.g., in the following procedure: Calculate_Net_Income Begin Parent_Allowance = No_of_Parents×30000 Net_Income = Annual_Income - Parent_Allowance End Slide 17 3.2.2 The Structure Selection Control The Selection Control Structure defines two courses of action, depending on the outcome of a condition. A condition is an expression that, when evaluated, computes to either TRUE or FALSE. The Selection structure requires the use of the keywords ‘if’, ‘else’ and ‘end_if’ as in the following format: if <condition> then-statements 1,2,3,… else else-statements 5,6,7,… end_if Slide 18 3.2.2 The Selection Control Structure (cont’d) if Progressive_Tax <= Standard_Tax begin Tax_Payable = Progressive_Tax Display Tax _Payable end else begin Tax _Payable = Standard_Tax Display Tax _Payable end Slide 19 3.2.2 The Selection Control Structure (cont’d) Indentation helps to visually differentiate the thenstatements and else-statements from those that follow the end_if. In certain decision-making situations, the elsestatements may be missing. The format of the selection structure becomes: if <condition> then-statements end_if Slide 20 3.2.2 The Selection Control Structure (cont’d) This structure means that if the condition is FALSE, NO ACTION will be taken at all. The program execution will continue with the statement that follows ‘end_if’. How about more than 2 different possibilities ? Slide 21 3.2.2 Nested Selection Structure The if-else selection structure is useful for 2-way decision-making. When we have more than 2 courses of action, we can use nested selection structures. Consider the calculation of Progressive Tax in our sample example. It can be expressed as pseudo-code which defines 4 separate courses of actions by nesting the if-else structures to 4 levels: Slide 22 3.2.2 Nested Selection Structure (cont’d) if Net_income <= 30000 Progressive_Tax = Net_income x 2% else if Net_income <= 60000 Progressive_Tax = 600 + (Net_income - 30000) x 8% else if Net_income <= 90000 Progressive_Tax = 3000 + (Net_income - 60000) x 14% else Progressive_Tax = 7200 + (Net_income - 90000) x 20% end_if Slide 23 3.2.2 Nested Selection Structure (cont’d) Nested Selection Structure can be confusing if the nesting is excessively deep. It is possible to express a multi-way selection structure in a series of independent 2-way selection structures equivalently: if Net_income <= 30000 Progressive_Tax = Net_income 2% end_if Slide 24 3.2.2 Nested Selection Structure (cont’d) if (Net_income > 30000) and (Net_income <= 60000) Progressive_Tax = 600 + (Net_income - 30000) x 8% end_if if (Net_income > 60000) and (Net_income <= 90000) Progressive_Tax = 3000 + (Net_income - 60000) x 14% end_if if Net_income > 90000 Progressive_Tax = 7200 + (Net_income - 90000) x 20% end_if Slide 25 3.2.3 The Repetition Control Structure The Repetition Control Structure specifies a block of one or more statements that are repeatedly executed until a condition is satisfied. Several forms of repetition control structures can be used For the time being, we describe only one of them, viz., the WHILE repetition structure, with the following format: while <condition> loop-statements end_while Slide 26 3.2.3 The Repetition Control Structure (cont’d) Here the loop-statement(s) will be executed as long as the <condition> is TRUE. The repeated execution of loop-statement(s) terminates when the <condition> becomes FALSE. Obviously, something inside the loop-statement(s) must be able to change the <condition> to FALSE Otherwise, we will have an infinite loop and the algorithm will never terminate To use our sample example to illustrate a ‘while’ repetition control structure Slide 27 3.2.3 The Repetition Control Structure (cont’d) display “Enter your annual income; should be a positive number” read Annual_Income while Annual_Income <= 0 display “Invalid annual income; it should be a positive number: please enter again” read Annual_Income end_while Slide 28 3.2.3 The Repetition Control Structure (cont’d) In this example, the first statement is a display statement, which prompt the user for input value The second statement reads that value and stores it in ‘Annual_Income’ The condition becomes TRUE if Annual_Income is less than or equal to 0, then the statements inside the whileloop will be executed one after one If the value supplied for Annual_Income is positive, the condition becomes FALSE and the algorithm control will drop down to the statement after’end_while’ Otherwise, the while-loop will be executed once again Slide 29 3.2.4 Pseudo-coding Conventions Each pseudo-code statement includes keywords that describe the operations and operands Input Output : Read, Enter, Display, Print, … Change values : Set, Initialise, Add, Subtract, …, or use = with a formulae Do not use any unambiguous words such as ‘handle’, ‘process’, … etc. Each statement should be written on a separate line. If a statement requires more than one line, the continuation lines should be indented Slide 30 3.3 Flowcharting A flowchart is a graph consisting of geometric shapes that are connected by flow lines The flow lines show the order in which the statements are executed Four geometric shapes are used Rectangle represents process Diamond represents decision Parallelogram represents input or output Oval represent the starting or finishing of the algorithm Slide 31 3.3 Flowcharting (cont’d) Display Chargeable_Tax Progressive_Tax < Standard Tax ? Process Decision Read Annual_Income Input or Output Start Terminal Four common symbols used in flowcharts Slide 32 3.3 Flowcharting (cont’d) Sequence Structure Statement 1 Statement 2 Statement 3 Slide 33 3.3 Flowcharting (cont’d) Selection Structure False elsestatement(s) Condition True thenstatement(s) Slide 34 3.3 Flowcharting (cont’d) Repetition Structure Condition True Loopstatement(s) False Slide 35 3.4 Pseudo-coding Vs Flow-charting Pseudo-coding Flow-charting Nature Verbal Graphical Vocabulary Unlimited Limited Size Reasonable Typically Large Preparation Effort Distinctive Feature Reasonable Typically Heavy Detail of Operation Flow of Control Slide 36 4. Implementation Translate each step of the Algorithm into a statement in a programming language and end up with a Computer Program; Definition — a Computer Program is a sequence of statements expressed in a programming language in a specific order that, when executed, produce the solution for a problem. Slide 37 4.1 Implementation — Programming Errors Three types of programming errors: Design errors — may occur because (i) an incorrect algorithm is chosen; or (ii) mistakes are made in translating an algorithm into a program; or (iii) erroneous data is designed for the program. Design errors can be removed by careful review of problem analysis, program design, translation, and test data. Slide 38 4.2 Implementation — Programming Errors (cont’d) Compilation errors — occurred during implementation phase and are detected by the compiler; Compilation errors are violations of syntax rules, hence a.k.a. Syntax errors. Depending on how serious the violation is, the compiler issues diagnostic message which may be either (i) Warning messages or (ii) Error messages. Slide 39 4.3 Implementation — Programming Errors (cont’d) Run-time errors — detected by the computer while the program is being executed. They are caused by program statements that require the computer to do something illegal. For example, attempting to store inappropriate data or divide a number by zero. Slide 40 5. Testing Program Testing is the process of executing a program to demonstrate its correctness A program must be tested using a sufficiently large sample of carefully designed test data sets such that every logical path in the program is traversed at least once In our example, we should test the algorithm with Net_Income = 0, 30000, 30001, 60000, 60001, 90000, and 90001. Slide 41 6. Documentation Now we have a workable, fully tested program, but we still have to document it because: 1. You may return to this program in the future to use the whole of a part of it again; 2. Other programmer of end-user will need some information about your program for reference or maintenance; 3. You may someday have to modify the program, or may discover some errors in your program. Slide 42 6. Documentation (cont’d) Program documentation consists of: Concise requirement specification; Description of Input/Output, Constraints, Formula, etc.; Pseudo-code or Flowchart of the algorithm; Source program listing; Hardcopy of sample test run. Slide 43 Unit 2 Lecture : Summary The method used for Problem Solving with computers is the Software Development Method, which consists of: (1) Requirement Specification; (2) Analysis; (3) Algorithm Design; (4) Implementation; (5) Testing; and (6) Documentation. Now you are able to solve problems starting from specifying the requirement, analyse them, and design algorithms using pseudocode and flowcharts. Slide 44 Unit 2 Lecture : Summary (cont‘d) Requirement Specification Analysis Software Development Method Pseudocode Algorithm Design Implementation Flow-chart Testing Documentation Slide 45