Programming Techniques Lec03 Procedural Abstraction Software Engineering Fall 2005.

Slides:



Advertisements
Similar presentations
Chapter 1 Writing a Program Fall Class Overview Course Information –On the web page and Blackboard –
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Chapter 10 Introduction to Arrays
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
CS 201 Functions Debzani Deb.
Testing an individual module
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Chapter 2: Algorithm Discovery and Design
Introduction SWE 619. Why Is Building Good Software Hard? Large software systems enormously complex  Millions of “moving parts” People expect software.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Topic 3 The Stack ADT.
Chapter 3 Introduction to Collections – Stacks Modified
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
AITI Lecture 20 Trees, Binary Search Trees Adapted from MIT Course 1.00 Spring 2003 Lecture 28 and Tutorial Note 10 (Teachers: Please do not erase the.
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
Today’s Agenda  Reminder: HW #1 Due next class  Quick Review  Input Space Partitioning Software Testing and Maintenance 1.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Abstraction, Specification and Verification. Comp 302, Spring Abstraction Hiding or overlooking detail that is not relevant for the task at hand.
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Data Structures Arrays and Lists Part 2 More List Operations.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Programming Fundamentals Enumerations and Functions.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Recursion Riley Chapter 14 Sections 14.1 – 14.5 (14.6 optional)
Modular Decomposition, Abstraction and Specifications
Chapter 3 of Programming Languages by Ravi Sethi
EECE 310: Software Engineering
Searching.
Higher-Order Procedures
Describing algorithms in pseudo code
Data Abstraction David Evans cs205: engineering software
Coding Concepts (Basics)
Lecture 4: Data Abstraction CS201j: Engineering Software
Introduction to Data Structure
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Software Specifications
Presentation transcript:

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.

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

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

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

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 }

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

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

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

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

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

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); }

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

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

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

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.

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

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

Programming Techniques Homework Assignment Implement the methods specified in class Interval Due date: Turn in by sending an 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)

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]

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 }

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 }

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 }

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 () }

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

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

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 }

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

Programming Techniques Exercises