CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)

Slides:



Advertisements
Similar presentations
Identity and Equality Based on material by Michael Ernst, University of Washington.
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.
Abstraction Functions. Announcements Exam 1 on Tuesday March 3 rd Closed book/phone/laptop 2 cheat pages allowed (handwritten or typed) 1 double-sided.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Lecture 9 Concepts of Programming Languages
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.
Ranga Rodrigo. Class is central to object oriented programming.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Chapter 3 Introduction to Collections – Stacks Modified
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION MIDTERM REVIEW Autumn 2011.
EECE 310: Software Engineering Iteration Abstraction.
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.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Comp 302: Software Engineering Data Abstractions.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Data Abstractions EECE 310: Software Engineering.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Understanding ADTs CSE 331 University of Washington.
Representation invariants and abstraction functions CSE 331 UW CSE.
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.
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 7: A Tale of Two Graphs (and.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
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.
Click to edit Master text styles Stacks Data Structure.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
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
EECE 310: Software Engineering
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Lecture 9 Concepts of Programming Languages
CSE 331 Software Design and Implementation
Representation Invariants and Abstraction Functions
Abstraction functions, Reasoning About ADTs
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 & Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
Computer Science 340 Software Design & Testing
Software Specifications
Lecture 9 Concepts of Programming Languages
Method Verification Paul Ammann.
Presentation transcript:

CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin) 1

ADT operations and mutation Creators/Producers Creators: return new ADT values (e.g., Java constructors). Effects, not modifies Producers: ADT operations that return new values Mutators: Modify a value of an ADT Observers: Return information about an ADT Mutable ADTs: creators, observers, and mutators Immutable ADTs: creators, observers, and producers 2

Three examples A primitive type as an (immutable) ADT An immutable type as an ADT A mutable type as an ADT 3

Primitive data types are ADTs int is an immutable ADT: creators: 0, 1, 2,... producers: + - * /... observer: Integer.toString(int) Peano showed we can define int with only one creator Would this be a good programming language design choice? Why might we want to do this? 4

Poly, an immutable datatype: 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: Always state whether mutable or immutable Define an abstract model for use in operation specifications Often difficult and always vital! Appeal to math if appropriate Give an example (reuse it in operation definitions) In all ADTs, the state in specifications is abstract, not concrete (coefficients above refer to specification, not implementation.) 5

Poly: creators // effects: makes a new Poly = 0 public Poly() // effects: makes a new Poly = cx n // throws: NegExponent if n < 0 public Poly(int c, int n) Creators New object, not part of pre-state: effects, not modifies Overloading: distinguish procedures of same name by parameters (Example: two Poly constructors) Footnote: slides omit full JavaDoc comments to save space; style might not be perfect either – focus on main ideas 6

Poly: observers // returns: the degree of this, // i.e., the largest exponent with a // non-zero coefficient. // Returns 0 if this = 0. public int degree() // returns: the coefficient of the term // of this whose exponent is d public int coeff(int d) 7

Notes on observers Observers Used to obtain information about objects of the type Return values of other types Never modify the abstract value Specification uses the abstraction from the overview this The particular Poly object being accessed The target of the invocation Also known as the receiver Poly x = new Poly(4, 3); int c = x.coeff(3); System.out.println(c); // prints 4 8

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

Notes on producers Operations on a type that create other objects of the type Common in immutable types like java.lang.String String substring(int offset, int len) No side effects Cannot change the abstract value of existing objects 10

IntSet, a mutable datatype: overview and creator // Overview: An IntSet is a mutable, // unbounded set of integers. A typical // IntSet is { x1,..., xn }. class IntSet { // effects: makes a new IntSet = {} public IntSet() 11

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

IntSet: mutators // modifies: this // effects: this post = this pre  {x} public void add(int x) // modifies: this // effects: this post = this pre - {x} public void remove(int x) 13

Notes on mutators Operations that modify an element of the type Rarely modify anything other than this Must list this in modifies clause (if appropriate) Typically have no return value (sometimes return “old” value that was replaced) Mutable ADTs may have producers too, but that is less common 14

Quick recap The examples focused on the abstract specification – with no connection at all to a concrete implementation To connect them we need the abstraction function (AF) that maps values of the concrete implementation of the ADT into abstract values in the specification The representation invariant (RI) ensures that values in the concrete implementation are well-defined – i.e., the RI must hold for every element in the domain of the AF 15

The abstraction function is a function Why do we map concrete to abstract and not vice versa? It’s not a function in the other direction. E.g., lists [a,b] and [b,a] each represent the set {a, b} It’s not as useful in the other direction. We can manipulate abstract value through abstract operations 16

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

Benevolent side effects Different implementation of member: boolean member(Character c1) { int i = elts.indexOf(c1); if (i == -1) return false; // move-to-front optimization Character c2 = elts.elementAt(0); elts.set(0, c1); elts.set(i, c2); return true; } Move-to-front speeds up repeated membership tests Mutates rep, but does not change abstract value AF maps both reps to the same abstract value 18 rr’ a op  AF

 Creating the concrete object must establish the representation invariant  Every concrete operation must maintain the rep invariant  Creating the abstraction object must establish the abstraction function  Every abstract operation must maintain the AF to provide consistent semantic meaning to the client  If things are right, either red arrow above will give the same result

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: easy For more complex abstractions: give names to fields or derived values AF defines the value of each “specification field” “derived specification fields” more complex The overview section of the specification should provide a way of writing abstract values A printed representation is valuable for debugging 20

ADTs and Java language features Java classes Make operations in the ADT public Make other ops and fields of the class private Clients can only access ADT operations Java interfaces Clients only see the ADT, not the implementation Multiple implementations have no code in common Cannot include creators (constructors) or fields Both classes and interfaces are sometimes appropriate Write and rely upon careful specifications Prefer interface types instead of specific classes in declarations (e.g., List instead of ArrayList for variables and parameters) 21

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 22

Representation exposure 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 Lesson: storing a mutable object in an immutable collection can expose the representation 23 Point p1 = new Point(); Point p2 = new Point(); Line line = new Line(p1,p2); p1.translate(5, 10); // move point p1

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 think about in a program can be a huge benefit 24