Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005.

Similar presentations


Presentation on theme: "Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005."— Presentation transcript:

1 Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005

2 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.

3 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

4 Programming Techniques 3.1 Benefits of Abstraction Benefits of abstraction by specification Locality: * 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

5 Programming Techniques 3.2 Specifications Specifications should be precise Specifications can be written in a formal language or in an informal language Formal 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 meaning We will use informal English in comments to define specifications

6 Programming Techniques 3.3 Specifications of procedural abstractions Header is formal, specifies visibility return type name order and type of parameters Informal specifications: REQUIRES specifies use constraints MODIFIES lists modified inputs/side-effects EFFECTS describes behavior for inputs complying to requirements, outputs and modifications public float sqrt (float x) { // REQUIRES: x should be positive // MODIFIES: nothing // EFFECTS: returns an approximation of square root of x }

7 Programming Techniques Arrays public class Arrays { // OVERVIEW: stand-alone procedures for manipulating arrays of ints public static int search (int [ ] a, int x) // EFFECTS: if x is in a, returns the index where x is stored // otherwise returns -1 public 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 public 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} }

8 Programming Techniques Arrays MODIFIES is omitted if no modifications happen Examples clarify specifications, e.g. sort REQUIRES 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

9 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 error public 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

10 Programming Techniques 3.4 Implementing procedures Specifications are written first Procedure bodies are added later Implementations should modify only those inputs specified in MODIFIES clause Implementations should produce results according to EFFECTS clause if REQUIRES clause holds

11 Programming Techniques SearchSorted public 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 search if (a == null) return -1; for (int i = 0; i x) return -1; return -1; } If a is unsorted => returns wrong result If a is null => returns -1 better to throw a null pointer exception (see chapter 4) Includes comment on algorithm used

12 Programming Techniques Sort public 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 = high) return; int mid = partition(a, low, high); quickSort(a,low,mid); quickSort(a,mid,high); }

13 Programming Techniques Partition private static int partition(int [ ] a, int i, int j) { // REQUIRES: a is not null and 0 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; } }

14 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-productive e.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

15 Programming Techniques Properties of procedures 1. Minimally constraining: a specification should constrain details of the procedure only to the extent necessary Only 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

16 Programming Techniques Properties of procedures 4. 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.

17 Programming Techniques Partial vs. total procedures 1.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

18 Programming Techniques 3.6 Summary A procedure is a mapping from inputs to outputs, with possible modification of inputs Its specification describes its behavior, providing a contract between users and implementors The specification does not change when the implementation changes, and provides locality and modifiability Specifications should be minimal and can be underdetermined and non-deterministic Desirable 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

19 Programming Techniques Homework Assignment Implement the methods specified in class Interval Due date: Turn in by sending an email to TA Preserve specifications Performance is not important Try to keep the code small Trust your own code (Interval is immutable so you have total control)

20 Programming Techniques Public class Interval OVERVIEW: this class implements Intervals Intervals are IMMUTABLE An Interval denotes a range of floating point numbers e.g. all numbers between -1 and +1, inclusive: [-1,1] Intervals have set operators: union, intersection, difference Complicated, 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]

21 Programming Techniques Constructors [0,1] denotes a closed interval (0,1) denotes an open interval [0,1) denotes a half-open interval the constructor is defined for closed intervals one can construct open and half-open intervals using set operators public 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 }

22 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 }

23 Programming Techniques Predicates public 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 }

24 Programming Techniques Set Operations: union & intersection public 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 () }

25 Programming Techniques Set Operations: difference public 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] }

26 Programming Techniques Arithmetic : add & subtract public 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] }

27 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 }

28 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

29 Programming Techniques Exercises 3.2 3.4


Download ppt "Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005."

Similar presentations


Ads by Google