CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.

Slides:



Advertisements
Similar presentations
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 8.
Advertisements

Identity and Equality Based on material by Michael Ernst, University of Washington.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Abstraction Functions. Announcements Exam 1 on Tuesday March 3 rd Closed book/phone/laptop 2 cheat pages allowed (handwritten or typed) 1 double-sided.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Slides by Vinod Rathnam with material from Alex Mariakakis Kellen Donohue, David Mailhot, and Hal Perkins Midterm Review.
Computer Science 340 Software Design & Testing Design By Contract.
Ranga Rodrigo. Class is central to object oriented programming.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION MIDTERM REVIEW Autumn 2011.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Comp 302: Software Engineering Data Abstractions.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
Data Abstractions EECE 310: Software Engineering.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Understanding ADTs CSE 331 University of Washington.
Representation invariants and abstraction functions CSE 331 UW CSE.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Abstraction Functions (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
Iteration Abstraction SWE Software Construction Fall 2009.
Abstraction, Specification and Verification. Comp 302, Spring Abstraction Hiding or overlooking detail that is not relevant for the task at hand.
CSE 331 Software Design & Implementation Hal Perkins Winter 2015 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, Dan Grossman,
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES Autumn 2011.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 6 Representation Invariants.
Representation Invariants and Abstraction Functions.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Slides by Alex Mariakakis Section 5: Midterm Review.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst and David.
EECE 310: Software Engineering
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Type Abstraction SWE Spring 2009.
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
CSC 480 Software Engineering
Representation Invariants and Abstraction Functions
Abstraction functions, Reasoning About ADTs
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Type Abstraction Liskov, Chapter 7.
CSE 331 Software Design and Implementation
Data Abstraction David Evans cs205: engineering software
CSE 331 Software Design & Implementation
Iteration Abstraction
SWE 619 Software Construction Last Modified, Fall 2015 Paul Ammann
Method Verification CS/SWE 332 Paul Ammann.
Lecture 4: Data Abstraction CS201j: Engineering Software
CSE 331 Software Design & Implementation
EECE 310: Software Engineering
CSE 331 Software Design and Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
CSE 331 Software Design & Implementation
Computer Science 340 Software Design & Testing
Type Abstraction SWE Spring 2013.
Software Specifications
Method Verification Paul Ammann.
Presentation transcript:

CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011

Kinds of ADT operations (abstract)  Mutable ADTs: creators, observers, and mutators  Immutable ADTs: creators, observers, and producers

Three examples  A primitive type as an (immutable) ADT  A mutable type as an ADT  An immutable type as an ADT CSE 331 Autumn 2011

Primitive data types are ADTs  int is an immutable ADT  creators 0, 1, 2,...  producers + - * /...  observer Integer.toString(int)  Peano showed we only need one creator for basic arithmetic – why might that not be the best programming language design choice? CSE 331 Autumn 2011

Poly: overview /** * A Poly is an immutable polynomial with * integer coefficients. A typical Poly is * c 0 + c 1 x + c 2 x **/ class Poly { …  Overview states whether mutable or immutable  Defines abstract model for use in specs of operations  Often difficult and always vital! Appeal to math if appropriate  Give an example (reuse it in operation definitions)  State in specification is abstract not concrete (in the Poly spec above, the coefficients are the abstract state) CSE 331 Autumn 2011

Poly: creators // effects: makes a new Poly = 0 public Poly() // effects: makes a new Poly = cx n // throws: NegExponent when n < 0 public Poly(int c, int n)  New object, not part of pre-state: in effects, not modifies  Overloading: distinguish procedures of same name by parameters ( Ex: two Poly constructors) CSE 331 Autumn 2011

Poly: observers // returns: the degree of this: the largest // exponent with a non-zero coefficient; if // no such exponent exists, returns 0 public int degree() // returns: the coefficient of // the term of this whose exponent is d public int coeff(int d) // Poly x = new Poly(4, 3); // x.coeff(3) returns 4 // x.degree() returns 3 CSE 331 Autumn 2011

Notes on observers  Observers return values of other types to discriminate among values of the ADT  Observers never modify the abstract value  They are generally described in terms of this – the particular object being worked on (also known as the receiver or the target of the invocation) CSE 331 Autumn 2011

Poly: producers // returns: this + q (as a Poly) public Poly add(Poly q) // returns: the Poly = this * q public Poly mul(Poly q) // returns: -this public Poly negate() // Poly x = new Poly(4, 3); // Poly y = new Poly(3, 7); // Poly z = x.add(y); // z.degree() returns 7 // z.coeff(3) returns 4 // (z.negate()).coeff(7) returns -3 CSE 331 Autumn 2011

Notes on producers  Common in immutable types like java.lang.String  Ex: String substring(int offset, int len)  No side effects  That is, they can affect the program state but cannot have a side effect on the existing values of the ADT CSE 331 Autumn 2011

IntSet, a mutable datatype // Overview: An IntSet is a mutable, unbounded // set of integers { x 1,..., x n }. class IntSet { // effects: makes a new IntSet = {} public IntSet() … CSE 331 Autumn 2011

IntSet: observers // returns: true if x  this // else returns false public boolean contains(int x) // returns: the cardinality of this public int size() // returns: some element of this // throws: EmptyException when size()==0 public int choose() CSE 331 Autumn 2011

IntSet: mutators // modifies: this // effects: this post = this pre  {x} public void add(int x) // insert an element // modifies: this // effects: this post = this pre - {x} public void remove(int x) CSE 331 Autumn 2011

Notes on mutators  Operations that modify an element of the type  Rarely modify anything other than this  Must list this in modifies clause if modified  Typically have no return value  Mutable ADTs may have producers too, but that is less common CSE 331 Autumn 2011

Quick Recap  The examples focused on the abstraction specification – with no connection at all to a concrete implementation  To connect them we need the abstraction function (AF), which maps values of the concrete implementation of the ADT (for 331, instances of a Java class) into abstract values in the specification  The representation invariant (RI) ensures that values in the concrete implementation are well-defined – that is, the RI must hold for every element in the domain of the AF CSE 331 Autumn 2011

The AF is a function  Why do we map concrete to abstract rather than vice versa?  It’s not a function in the other direction.  Ex: lists [a,b] and [b,a] both represent the set {a,b} in CharSet  It’s not as useful in the other direction – we can manipulate abstract values through abstract operations CSE 331 Autumn 2011

Brief example Abstract stack with array and “top” index implementation new()000 push(17)1700 Top=1 push(-9)17-90 Top=2 Top=0 stack = <> stack = pop()17-90 stack = Top=1 Abstract states are the same stack = = Concrete states are different ≠ AF is a function AF -1 is not a function CSE 331 Autumn 2011

Writing an abstraction function  The domain: all representations that satisfy the rep invariant  The range: can be tricky to denote  For mathematical entities like sets: relatively easy  For more complex abstractions: give them fields AF defines the value of each “specification field”  The overview section of the specification should provide a way of writing abstract values  A printed representation is valuable for debugging CSE 331 Autumn 2011

Checking the rep invariant public void delete(Character c) { checkRep(); elts.remove(c) checkRep(); } … /** Verify that elts contains no duplicates. */ /* throw an exception if it doesn’t */ private void checkRep() { for (int i = 0; i < elts.size(); i++) { assert elts.indexOf(elts.elementAt(i)) == i; } From Friday’s CharSet example CSE 331 Autumn 2011

Alternative  repOK() returns a boolean  Callers of repOK() check the return value  Why do this instead of checkRep() ?  More flexibility if the representation is invalid CSE 331 Autumn 2011

Checking rep invariants  Should code always check that the rep invariant holds?  Yes, if it’s inexpensive (in terms of run-time)  Yes, for debugging (even when it’s expensive)  It’s quite hard to justify turning the checking off  Some private methods need not check – why? CSE 331 Autumn 2011

Practice defensive programming  Assume that you will make mistakes – if you’re wrong in this assumption you’re (a) superhuman and (b) ahead of the game anyway  Write code designed to catch them  On entry: check rep invariant and check preconditions  On exit: c heck rep invariant and check postconditions  Checking the rep invariant helps you discover errors  Reasoning about the rep invariant helps you avoid errors  Or prove that they do not exist!  More about reasoning later in the term CSE 331 Autumn 2011

Representation exposure redux  Hiding the representation of data in the concrete implementation increases the strength of the specification contract, making the rights and responsibilities of both the client and the implementer clearer  Defining the fields as private in a class is not sufficient to ensure that the representation is hidden  Representation exposure arises when information about the representation can be determined by the client CSE 331 Autumn 2011

Representation exposure: example  Is Line mutable or immutable?  It depends on the implementation!  If Line creates an internal copy: immutable  If Line stores a reference to p1, p2 : mutable  So, storing a mutable object in an immutable collection can expose the representation Point p1 = new Point(); Point p2 = new Point(); Line line = new Line(p1,p2); p1.translate(5, 10); // move point p1 CSE 331 Autumn 2011

Ways to avoid rep exposure  Exploit immutability – client cannot mutate Character choose() { // Character is immutable return elts.elementAt(0); }  Make a copy – mutating a copy in the client is OK List getElts() { return new ArrayList (elts); }  Make an immutable copy – client cannot mutate List getElts() { return Collections.unmodifiableList (elts); } CSE 331 Autumn 2011

Benevolent side effects: example  Alternative implementation of member – an observor boolean member(Character c1) { int i = elts.indexOf(c1); if (i == -1) return false; // move-to-front to // speed up repeated member tests Character c2 = elts.elementAt(0); elts.set(0, c1); elts.set(i, c2); return true; }  Mutates rep, but not abstract value – AF maps both r and r’ to abstract value a  Nor does it violate the rep invariant  Arguably, the client can learn something about the representation – at the same time, this is a relatively benign case of rep exposure rr’ a member impl  AF CSE 331 Autumn 2011

A half-step backwards  Why focus so much on invariants (properties of code that do not – or are not supposed to – change)?  Why focus so much on immutability (a specific kind of invariant)?  Software is complex – invariants/immutability etc. allow us to reduce the intellectual complexity to some degree  That is, if we can assume some property remains unchanged, we can consider other properties instead  Simplistic to some degree, but reducing what we need to focus on in a program can be a huge benefit CSE 331 Autumn 2011

Next steps CSE 331 Autumn 2011  Assignment 2(a)  Due tonight 11:59PM  Assignment 2(b)  Out tomorrow AM  Due Friday 11:59PM  Lectures (swap from original plan)  Subtyping/subclassing (W)  Modular design (F)

CSE 331 Autumn 2011