Date post: | 16-Dec-2015 |
Category: |
Documents |
Upload: | melinda-parrish |
View: | 215 times |
Download: | 0 times |
Chapter 1.
The Phases of Software
Development
Data Structure 2
Chapter outline
Objectives Use Javadoc to write a method’s complete specification Recognize quadratic, linear, and logarithmic runtime be
havior 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 3
Data structure is
Collection of data, generally organized so that items can be stored and retrieved by some fixed techniques
Data Structure 4
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 5
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 6
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 meth
od (“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 7
How to write a specification
Tells what the 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 8
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 9
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 10
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 commentThe precondition and postcondition appear as comments in your program.s in your program.They are usually placed before the method implementatiThey are usually placed before the method implementation.on.
Data Structure 11
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 12
Exceptions
Exceptions Messages for serious programming errors
Throwing an exception The act of halting work and passing a message to the calli
ng 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 13
Exception
Exception handling
throw new ( “ “)
This is the type of exception we are throwing. All of our exceptionswill be the type IllegalArgumentException
This is an error message that willbe passed as part of the exceptionThe message should describe the error well
Data Structure 14
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 15
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 factorsSo, Count certain operations
Data Structure 16
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 17
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 18
Big-O Notation
Quadratic Time If the largest term is no more than a constant times n2, the algorit
hm is “big-O of n2, O(n2) Doubling the input size makes the number of operations increase f
our 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 t
wo fold
Logarithmic Time If the largest term is a constant times a logarithm of n, the algorith
m is “big-O of the logarithm of n, O(logn)
Data Structure 19
Big-O Notation
The Big-O notation of Three techniques Technique 1 : O(n) Technique 2 : O(n2) Technique 3: O(logn)
Order of the algorithm Big-O analysis loses some information about
relative times
Data Structure 20
Time analysis of java methods
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
Data Structure 21
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 22
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 23
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 24
Testing and Debugging
Assert statementsPublic 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;
}