240 likes | 345 Views
Chapter 1. The Phases of Software Development. Chapter outline. Objectives Use Javadoc to write a method’s complete specification Recognize quadratic, linear, and logarithmic runtime behavior in sample algorithm Create and recognize test data that is appropriate for a problem Contents
E N D
Chapter 1. The Phases of Software Development
Chapter outline • Objectives • Use Javadoc to write a method’s complete specification • Recognize quadratic, linear, and logarithmic runtime behavior in sample algorithm • Create and recognize test data that is appropriate for a problem • Contents • Specification, Design, Implementation • Running Time Analysis • Testing and Debugging Data Structure
Data structure is • Collection of data, generally organized so that items can be stored and retrieved by some fixed techniques Data Structure
The Phase of Software Development • Problem analysis understand the problem • Requirements definition specify what program will do • High- and low-level design how it meets requirements • Implementation of design code it • Testing and verification detect errors, show correct • Delivery turn over to customer • Operation use the program • Maintenance change the program • Obsolescence Data Structure
An Algorithm is • A procedure or sequence of instructions for solving a problem • Expressed in many different ways • In English, in a particular programming language • In a pseudo code Data Structure
Design Technique • Decomposing the problem • Break down a task into a few subtask • Then decompose each subtask into smaller subtasks • Each subtask is implemented as a separate Java method (“function” or “Procedures”) • Produces a good final program • What makes a good decomposition? • Subtasks should help you produce short pseudocode • What are good subtasks? • Potential for code reuse • Possibility of future changes Data Structure
How to write a specification • Tells whatthe method does, but not how it does its work • Information hiding : “knows only as much as you need, but no more…” • Procedural abstraction • Method specification Includes • Short introduction • Parameter description • Precondition • Returns condition or postcondition • “Throws” List Data Structure
How to write a specification • Example) celsiusToFahrenheit public static double celsiusToFahrenheit(double c) Convert a temperature from Celsius degrees to Fahrenheit degrees Parameters: c – a temparature in Celsius degrees Preconditon: c >= -273.16 Returns: the temperature c converted to Fahrenheit degrees Throws:IllegalArgumentException Indicates that c is less than the smallest Celsius temperature (-273.16) Data Structure
Precondition and Postcondition • Precondition • Is a statement giving the condition that is supposed to be true when a method is called • Postcondition • Is a statement describing what will be true when a method call is completed. • If the method is correct and the precondition was true when the method was called, then the method will complete Data Structure
Precondition and Postcondition • Example // Precondition: x >= 0. // Postcondition: The square root of x has // been written to the standard output. public void writeSqrt( double x) ... The precondition and postcondition appear as comments in your program. They are usually placed before the method implementation. Data Structure
Precondition and Postcondition • Another Example // Precondition: letter is an uppercase or // lowercase letter (in the range 'A' ... 'Z' or 'a' // ... ‘z') . // Postcondition: The value returned by the method is // true if letter is a vowel; otherwise the value// returned by the method is false. public boolean isVowel( char letter ) ... “Always make sure the precondition is valid . . . . . . so the postcondition becomes true at the method end.” Data Structure
Exceptions • Exceptions • Messages for serious programming errors • Throwing an exception • The act of halting work and passing a message to the calling program Public static double celToFah(double c) { final double MINIMUM_CELSIUS = -273.16 ; if( c < MINIMUM_CELSIUS) throw new IllegalArgumentException(“Argument” + c + “is too small.”); return (9.0/5.0) * c + 32; } Data Structure
Exception • Exception handling throw new ( “ “) This is the type of exception we are throwing. All of our exceptions will be the type IllegalArgumentException This is an error message that will be passed as part of the exception The message should describe the error well Data Structure
Running Time Analysis • Time analysis • Consists of reasoning about an algorithm’s speed • Does the algorithm work fast enough for my needs? • How much longer does the algorithm take when the input gets larger? • The Stair-Counting Problem • You and Judy at the top of the Eiffel Tower • “How many steps there are to the bottom?” • There are Three Techniques for this problem ! Data Structure
Three techniques for the stair-counting problem • Technique 1: Walk Down and keep a Tally • Each time you take a step down, mark on the sheet of paper • Technique 2: Walk Down, but Let Judy keep the Tally • Each time you take a step down, Judy mark on the sheet of paper (step down, lay a hat, step up) • Technique 3: Jervis to the Rescue • Use the Jervis’s sign “There are 2689 steps” Actual elapsed time and vary depending on other factors So, Count certain operations Data Structure
Analysis for the stair-counting problem • Certain operations for analysis • Walk up or down : one operation • Mark on the paper : one operation • Total operations of three techniques • Technique 1: 3 * 2689 • Technique 2: downward steps = (1 + 2 + … + 2689) upward steps = (1 + 2 + … + 2689) marks made = 2689 7,236,099 • Technique 3: 4 Data Structure
Time analysis • Similar to the analysis of the stair-counting techniques • Counts the number of operations • Depends on the program’s input • The time expressions for three techniques • Technique 1 : 3n • Technique 2 : n2 + 2n • Technique 3 : log10n + 1 Data Structure
Big-O Notation • Quadratic Time • If the largest term is no more than a constant times n2, the algorithm is “big-O of n2, O(n2) • Doubling the input size makes the number of operations increase four fold • Linear Time • If the largest term is a constant times n, the algorithm is “big-O of n, O(n) • Doubling the input size makes the number of operations increase two fold • Logarithmic Time • If the largest term is a constant times a logarithm of n, the algorithm is “big-O of the logarithm of n, O(logn) Data Structure
Big-O Notation • The Big-O notation of Three techniques • Technique 1 : O(n) • Technique 2 : O(n2) • Technique 3: O(logn) Orderof the algorithm Big-O analysis loses some information about relative times Data Structure
public static boolean search(double[] data, double target) { int i; for ( i=0; i<data.length; i++ ) { if(data[i] == target) return true; } return false; } Analysis parts When the for-loop starts Execute the body of loop After the loop finishes Analysis results Total operations : Kn + 3 O(n) Worst-case analysis Average-case analysis best-case analysis Time analysis of java methods Data Structure
Testing and Debugging • Program testing • Occurs when run a program and observe its behavior • How the program works for that particular input • How long the program takes to complete • Properties of Good test data • Must know output of each test input • Test inputs that are most likely to cause errors • Boundary Values • “One step away from different behavior” • Example) 0, 1 & -1 Data Structure
Testing and Debugging • Fully Exercising code • Each line of code is executed at least once • Profiler • Help fully exercise code • Indicates how many times each method was called • Using a Debugger • Track down the reason why a test case is failing Data Structure
Testing and Debugging • Assert statements • Is boolean expressions that can be checked for validity while a program is running assert : “ “; This is boolean expression that we want to make sure is true at this point in the program This is an error message that will Be generated if the boolean expression is false Data Structure
Testing and Debugging • Assert statements Public static int max0f3( int a, int b, int c ) { int answer; answer = a; if ( b > answer ) answer = b; if ( c > answer ) answer = c; assert (answer == a)|| (answer == b)|| (answer == c) : “max0f3 answer is not equal to one of the arguments”; assert (answer >= a)&&(answer >= b)&&(answer >= c) : “max0f3 answer is not equal to the largest argument”; return answer; } Data Structure