29
Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Embed Size (px)

DESCRIPTION

Programming Techniques Procedural abstraction Abstract a single action using parameterization and specification Parameterization abstracts from the identity of the data being used, allowing the procedure to be used in more situations relevant: presence, number, types of parameters irrelevant: identity of parameters Specification abstracts from the realization of the action, allowing multiple implementations relevant: what is done irrelevant: how it is done

Citation preview

Page 1: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Lec03Procedural Abstraction

Software EngineeringFall 2005

Page 2: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Why use abstraction?

Recall that an abstraction is a many-to-one map, abstracting from details that are not relevant. Abstraction by specification allows multiple realizations of

the procedure. Locality: a particular implementation can be read or written

without examining implementations of other abstractions. Modifiability: any abstraction can be reimplemented without changing other abstractions that use it.

Page 3: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Procedural abstractionAbstract a single action using parameterization and

specificationParameterization abstracts from the identity of the data

being used, allowing the procedure to be used in more situations

relevant: presence, number, types of parameters irrelevant: identity of parametersSpecification abstracts from the realization of the action,

allowing multiple implementationsrelevant: what is doneirrelevant: how it is done

Page 4: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

3.1 Benefits of AbstractionBenefits of abstraction by specificationLocality:

* the implementation of an abstraction can be read or written without needing to examine the implementations of any other abstractions* the abstraction can be used without examining its implementation

Modifiability: an abstraction can be re-implemented without requiring changes to any abstractions that use it

Page 5: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

3.2 SpecificationsSpecifications should be preciseSpecifications can be written in a formal language or in an

informal languageFormal specification languages have a precise meaning and

can be checked automatically or even used to generate code (see cs304)

Informal specifications are easier to read or write but harder to give precise meaningWe will use informal English in comments to define specifications

Page 6: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

3.3 Specifications of procedural abstractions

Header is formal, specifiesvisibilityreturn typenameorder and type of parameters

Informal specifications:REQUIRES specifies use constraints MODIFIES lists modified inputs/side-effectsEFFECTS describes behavior for inputs complying to

requirements, outputs and modificationspublic float sqrt (float x) {

// REQUIRES: x should be positive// MODIFIES: nothing// EFFECTS: returns an approximation of

square root of x}

Page 7: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Arrayspublic class Arrays {

// OVERVIEW: stand-alone procedures for manipulating arrays of intspublic static int search (int [ ] a, int x)// EFFECTS: if x is in a, returns the index where x is stored// otherwise returns -1public static int searchSorted (int [ ] a, int x)// REQUIRES: a is sorted in ascending order// EFFECTS: if x is in a, returns the index where x is stored// otherwise returns -1public static void sort (int [ ] a)// MODIFIES: a// EFFECTS: Rearranges the elements of a in ascending order// e.g. if a = {3,1,6,1} , a_post={1,1,3,6}

}

Page 8: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

ArraysMODIFIES is omitted if no modifications happenExamples clarify specifications, e.g. sortREQUIRES is omitted if the procedure is total

searchSorted is partial -> EFFECTS are not specified if requirements are not met

If modifications occur, the state before the procedure is invoked should be related to the state after the procedure returns, e.g. a and a_post in searchSorted

Page 9: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

New objects and implicit inputs

public static int [ ] boundArray (int [ ], int n)// EFFECTS: Returns a new array containing the// elements of a in the order they appear in a except// that any elements of a > n are replaced by n

Returning a itself if no elements > n are found is an errorpublic static void copyLine()

// REQUIRES: System.in contains a line of text// MODIFIES: System.in and System.out// EFFECTS: Reads a line of text from System.in,

// advances cursor// in System.in to end of line, writes the line on ystem.out

Implicit input modifications should be described in specifications

Page 10: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

3.4 Implementing proceduresSpecifications are written firstProcedure bodies are added laterImplementations should modify only those inputs specified in

MODIFIES clauseImplementations should produce results according to EFFECTS

clause if REQUIRES clause holds

Page 11: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

SearchSortedpublic static int searchSorted (int [ ] a, int x) {

// REQUIRES: a is sorted in ascending order// EFFECTS: if x is in a, returns the index where x is stored// otherwise returns -1// uses linear searchif (a == null) return -1;for (int i = 0; i < a.length; i++) if (a[i] == x) return i; else if (a[i] > x) return -1;return -1;}

If a is unsorted => returns wrong resultIf a is null => returns -1

better to throw a null pointer exception (see chapter 4)Includes comment on algorithm used

Page 12: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Sortpublic static void sort (int [ ] a)// MODIFIES: a// EFFECTS: Rearranges the elements of a in ascending order

// e.g. if a = {3,1,6,1} , a_post={1,1,3,6}if (a == null) return;quicksort (a, 0, a.length-1);

}private static void quickSort(int [ ] a, int low, int high) {// REQUIRES: a is not null and 0 <= low and high < length// MODIFIES: a// EFFECTS: Sorts a[low],a[low+1],…,a[high] in ascending //order

if (low >= high) return;int mid = partition(a, low, high);quickSort(a,low,mid);quickSort(a,mid,high);

}

Page 13: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Partitionprivate static int partition(int [ ] a, int i, int j) {// REQUIRES: a is not null and 0 <= i < j < a.length// MODIFIES: a// EFFECTS: Reorders the elements in two contiguous groups,// a[i],…,a[res] and a[res+1],…,a[j] such that each element in// the second group is at least as large as each element in the// the first group// returns resint x = a[i];while (true) { while (a[j] > x) j--; while (a[i] < x) i++; if (i < j) { // need to swap int temp = a[i]; a[i]=a[j]; a[j] = temp; j--; i++; } else return j;}}

Page 14: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

3.5 Designing Procedural Abstractions

Procedures are introduced to shorten the calling code, clarify structure, and reduce code duplication

Partition is introduced because it has a well-defined purpose and allows to separate details of partitioning from controlling the partitioning

Further decomposition is counter-productivee.g. the loop body in partition could be made into a procedure, but its purpose would be difficult to state

Rule of thumb #1: if you have trouble naming the procedure, then it probably shouldn’t be separated

Rule of thumb #2: if you see almost identical code repeated twice, then it is probably useful to define a procedure

Page 15: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Properties of procedures1. Minimally constraining: a specification should constrain details of

the procedure only to the extent necessaryOnly details that matter to the user are constrained to leave the implementor the greatest freedom for efficient implementations

2. Procedures can be underdetermined: for certain inputs, a set of acceptable outputs is possible instead of only one, e.q. search and searchSorted are underdetermined:what index is returned if x occurs more than once ?Binary search returns a different result than linear search, but both are correct

3. Procedures usually have a deterministic implementation:for the same inputs, the same result is produced. Underdetermined procedures can be non-deterministic, but it usually takes extra program effort

Page 16: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Properties of procedures4. Generality: a specification is more general if it can handle a larger

class of inputs, e.q. it works with any size of array instead of fixed size

5. Simplicity: procedures should have a well-defined purpose, easy to explain, independent of context of use. If it is hard to name the procedure there may be a problem.

Page 17: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Partial vs. total procedures1.A procedure is total if its behavior is specified for all legal inputs.

Otherwise it is partial. 2.Partial procedures are always specified with a REQUIRES clause.

They are less safe than total procedures, and should only be used - when the context of use is limited (private helper procedures), e.g. quickSort, or - when they enable a substantial benefit such as better performance, e.g. it is not cost-effective to check that the array is sorted in searchSorted

3.Whenever possible, an implementation should check to see if the requires clause is satisfied. If a requirement is not satisfied, throw an exception.

Library procedures intended for general use should always be total

Page 18: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

3.6 SummaryA procedure is a mapping from inputs to outputs, with possible

modification of inputsIts specification describes its behavior, providing a contract between

users and implementorsThe specification does not change when the implementation changes,

and provides locality and modifiabilitySpecifications should be minimal and can be underdetermined and

non-deterministicDesirable properties include simplicity and generality.Implementations should be total when possible, and may be partial

when the context of use is limited and controlled, such as for private helper procedures

Page 19: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Homework Assignment Implement the methods specified in class IntervalDue date: Turn in by sending an email to TA

Preserve specificationsPerformance is not importantTry to keep the code smallTrust your own code

(Interval is immutable so you have total control)

Page 20: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Public class IntervalOVERVIEW: this class implements Intervals

Intervals are IMMUTABLE

An Interval denotes a range of floating point numberse.g. all numbers between -1 and +1, inclusive: [-1,1]

Intervals have set operators: union, intersection, differenceComplicated, fragmented intervals can be constructed

using set operators e.g. the union of [-1,1] and [3,4] gives [-1,1][3,4] e.g. the difference of [-1,1] and [0,0.5] gives [-1,0)(0.5,1] e.g. the intersection of [-1,1] and [0,2.5] gives [0,1]

Page 21: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Constructors [0,1] denotes a closed interval(0,1) denotes an open interval[0,1) denotes a half-open intervalthe constructor is defined for closed intervalsone can construct open and half-open intervals using set operatorspublic Interval () {

// EFFECTS: constructs the empty Interval ()}

public Interval (float lowerBound, float upperBound) {// EFFECTS: constructs Interval [lowerBound,upperBound]// If upperBound < lowerBound, the empty Interval is constructed}

Page 22: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Methods defined in Object public String toString () {

// EFFECTS: returns a String representation of the Interval// e.g. the empty interval: ()// e.g. a closed interval: [-1,1]// e.g. a half-open interval: (0,1]// e.g. a fragmented interval: [-1,0)(0,1]}

public boolean equals (Object o) {// EFFECTS: returns whether the Interval is equal to the object o// Two Intervals are equal if they have identical boundaries// or they are both empty}

Page 23: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Predicatespublic boolean isIn (float value) {

// EFFECTS: returns whether the value is included in the Interval// e.g. for Interval [-1,1] and value 0 this gives true // e.g. for Interval [-1,1) and value 1 this gives false }

Page 24: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Set Operations: union & intersectionpublic Interval union (Interval other) {

// EFFECTS: returns the union of this Interval and the other Interval// if any of the two Intervals is empty, return the other// e.g. the union of [-1,1] and [3,4] gives [-1,1][3,4]// e.g. the union of [-1,3] and [1,4] gives [-1,4]// e.g. the union of [-1,1][3,4] and (1,3) gives [-1,4]}

public Interval intersection (Interval other) {// EFFECTS: returns intersection of this Interval and other Interval// if any of the two Intervals is empty, return the empty Interval// e.g. the intersection of [-1,1] and [0,2.5] gives [0,1]// e.g. the intersection of [-1,0] and [1,2.5] gives ()}

Page 25: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Set Operations: differencepublic Interval difference (Interval other) {

// EFFECTS: returns difference of this Interval minus other Interval// if this Intervals is empty, return this Interval// if the other Interval is empty, return this Interval// e.g. the difference of [-1,1] and [0,2] gives [-1,0)// e.g. the difference of [0,2] and [-1,1] gives (1,2]// e.g. the difference of [-1,1] and [0,0.5] gives [-1,0)(0.5,1]}

Page 26: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Arithmetic : add & subtractpublic Interval add (Interval other) {

// EFFECTS: returns Interval of all numbers you can obtain by // adding any number in this Interval to any number in other Interval// e.g. (1,2] + [3,5] gives (4,7]// e.g. [1,2][3,4] + [1,3] gives [2,7]// e.g. [1,2][3,4] + [1,3](3,5] gives [2,9]}

public Interval subtract (Interval other) {// EFFECTS: returns the Interval of all numbers you can obtain // subtracting any number in the other Interval from any in this // Interval // e.g. (1,2] - [3,5] gives (-4,-1]// e.g. [1,2][3,4] - [1,3] gives [-2,3]}

Page 27: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Arithmetic Operations: multiply & divide

public Interval multiply (Interval other) {// EFFECTS: returns the Interval of all numbers you can obtain by // multiplying any number in this Interval with any number in the // other Interval// e.g. (1,2] * [3,5] gives (3,10]// e.g. [1,2][3,4] * [1,3] gives [1,12]// e.g. [-5,2] * [-1,2] gives [-10,5]}

public Interval divide (Interval other) {// EFFECTS: returns the Interval of all numbers you can obtain by // dividing any number in this Interval by any number in other // Interval// e.g. (1,2] / [3,5] gives (0.2,0.6666]// e.g. [1,2][3,4] / [1,3] gives [0.3333,4]// e.g. [-5,2] / [-1,-2] gives [-2,5]// e.g. [1,2] / [-1,2] is undefined because [-1,2] includes 0 // -> returns the empty Interval}

Page 28: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Test code public static void main(String args[]) {

// EFFECTS: test Interval functionality}

Notes on implementation:- efficiency is not a consideration- choose your representation wisely- make sure you handle all exceptional cases: e.g. empty intervals, negative numbers, open intervals- define as many private helper functions as you need e.g. a function to break up a fragmented interval in simple pieces e.g. a function to simplify a fragmented, overlapping interval e.g. a function to multiply two simple intervals

Page 29: Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

Programming Techniques

Exercises3.23.4