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

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.
CSE503: SOFTWARE ENGINEERING COMPLEXITY, PROVING ADTS David Notkin Spring 2011.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION TESTING II Autumn 2011.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION MIDTERM REVIEW Autumn 2011.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Data structures Abstract data types Java classes for Data structures and ADTs.
12-1 Priority Queues, Sets, and Bags Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 12 Monday, March 12 Handout #22.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Abstraction ADTs, Information Hiding and Encapsulation.
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)
Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Object-Oriented Principles Applications to Programming.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
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.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Abstraction Functions (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. Alan Perlis Lecture 15: Data Abstraction.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
CSE 331 Software Design & Implementation Hal Perkins Winter 2015 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, Dan Grossman,
CompSci 100E 19.1 Getting in front  Suppose we want to add a new element  At the back of a string or an ArrayList or a …  At the front of a string.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
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.
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.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst and David.
Modular Decomposition, Abstraction and Specifications
EECE 310: Software Engineering
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Abstraction Functions and Representation Invariants
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
Data Abstraction David Evans cs205: engineering software
CSE 331 Software Design & Implementation
SWE 619 Software Construction Last Modified, Fall 2015 Paul Ammann
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
Lecture 7: Linked List Basics reading: 16.2
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
slides created by Ethan Apter
Software Specifications
Presentation transcript:

CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES Autumn 2011

What is an ADT?  Recall procedural abstraction  Abstracts from the details of procedures  A specification mechanism  Satisfying the specification with an implementation  Data abstraction (Abstract Data Types or ADTs):  Abstracts from the details of data representation  A specification mechanism + a way of thinking about programs and designs  Satisfying the specification with an implementation CSE 331 Autumn

Why we need Abstract Data Types  Organizing and manipulating data is pervasive  Often crucial to start by designing data structures  Potential problems with choosing a data structure  Decisions about data structures are made too early  Very hard to change key data structures later on CSE 331 Autumn

An ADT is a set of operations  ADT abstracts away from a specific representation to focus on the semantic meaning of the data  In what sense are the following two definitions different?  Although the representations differ, the client should instead consider a Point as a set of operations to create and manipulate 2D points on the plane  By restricting the client to only call operations to access data, the potential for modifying the representation (and supporting algorithms) remains CSE 331 Autumn class Point { float x, y; } class Point { float r, theta; }

2D point class Point { // A 2-d point exists somewhere in the plane,... public float x(); public float y(); public float r(); public float theta(); //... can be created,... public Point(); // new point at (0,0) //... can be moved,... public void translate(float delta_x, float delta_y); public void scaleAndRotate(float delta_r, float delta_theta); } CSE 331 Autumn

ADT = objects + operations  The only operations on objects of the type are those provided by the abstraction  The implementation is hidden CSE 331 Autumn Point x y r theta translate scale_rot rest of program abstraction barrier clients implementation

ADTs and specifications  Specification: only in terms of the abstraction  Never mentions the representation  Abstraction function: Object  abstract value  What the data structure means as an abstract value Ex: where in the plane is that 2D point?  Representation invariant: Object  boolean  Indicates whether the Object – the representation in the implementation – is well-formed  Only well-formed representations in the implementation can be mapped to abstract values

Implementing an ADT  To implement a data abstraction  Select the representation of instances, the “rep”  Implement operations in terms of that rep  In Java, you do this in a class – in fact, you’ve done it many times before  Choose a representation so that  It is possible to implement operations  The most frequently used operations are efficient

CharSet specification Finite mutable set of Characters // effects: creates an empty CharSet public CharSet ( ) // modifies: this // effects: this post = this pre  {c} public void insert (Character c); // modifies: this // effects: this post = this pre - {c} public void delete (Character c); // returns: (c  this) public boolean member (Character c); // returns: cardinality of this public int size ( );

A CharSet implementation class CharSet { private List elts = new ArrayList (); public void insert(Character c) { elts.add(c); } public void delete(Character c) { elts.remove(c); } public boolean member(Character c) { return elts.contains(c); } public int size() { return elts.size(); } CharSet s = new CharSet(); Character a = new Character(‘a’); s.insert(a); s.delete(a); if (s.member(a)) // print “wrong”; else // print “right”;

Where Is the Error?  Perhaps delete is wrong  It should remove all occurrences  Perhaps insert is wrong  It should not insert a character that is already there  How can we know?  The representation invariant tells us

The representation invariant  States data structure well-formedness  Must hold before and after every operation is applied – and after initialization  Two ways of writing the CharSet rep invariant (as part of the comments for the CharSet class)  // Rep invariant: elts has no nulls and no duplicates private List elts;   indices i of elts. elts.elementAt(i) ≠ null  indices i, j of elts. i ≠ j   elts.elementAt(i).equals(elts.elementAt(j))

Now we can locate the error class CharSet { // Rep invariant: elts has no nulls and no duplicates private List elts = new ArrayList (); public void insert(Character c) { elts.add(c); } public void delete(Character c) { elts.remove(c); } … } CharSet s = new CharSet(); Character a = new Character(‘a’); s.insert(a); s.delete(a); if (s.member(a)) // print “wrong”; else // print “right”;

Listing the elements of a CharSet  Consider adding the following method to CharSet  // returns: a List containing the members of this public List getElts();  Consider this implementation  // Rep invariant: elts has no nulls and no duplicates public List getElts() { return elts; }  Does the implementation of getElts preserve the rep invariant?  Kind of, sort of, not really…

Representation exposure  Consider the client code CharSet s = new CharSet(); Character a = new Character(‘a’); s.insert(a); s.getElts().add(a); s.delete(a); if (s.member(a)) …  The client sees the representation and (in this case) can even manipulate it directly – makes it hard to maintain the rep invariant!  The client is no longer constrained to only manipulate the representation through the specification  Representation exposure is external access to the rep; it is almost always evil (so if you do it, document why and how, and feel guilty about it!) – more on avoiding rep exposure next lecture

New implementation of insert public void insert(Character c) { Character cc = new Character(encrypt(c)); if (!elts.contains(cc)) elts.addElement(cc); } }  This maintains the representation invariant for the class  The rep invariant only considers structure – well-formedness – not meaning  In this case, there is still an error – consider this client code CharSet s = new CharSet(); Character a = new Character(‘a’); s.insert(a); if (s.member(a)) print “right” else print “wrong”; OOPS

Abstraction function to the rescue  The abstraction function maps the representation to the abstract value it represents  AF(CharSet this) = { c | c is contained in this.elts }  Or the “set of Characters contained in this.elts ”  The abstraction function lets us reason about behavior from the client perspective  The AF is typically not executable  Do we satisfy the specification of insert ? // modifies: this // effects: this post = this pre  {c} public void insert (Character c); CSE 331 Autumn

Helps identify problem  Applying the abstraction function to the result of the call to insert yields AF(elts)  {encrypt(‘a’)}  So when member is checked, the implementation looks for ‘a’ rather than the encrypted value of ‘a’ – from the client’s view, an inserted element is no longer found, even though it has not been deleted  What if we used this abstraction function? AF(this) = { c | encrypt(c) is contained in this.elts } AF(this) = { decrypt(c) | c is contained in this.elts }

Recap: the CharSet representation  Ex: [‘a’,’b’,’c’], [‘b’,’a’,’c’,’d’], [‘9’,’1’,’6’]  How do we know that these represent sets of characters to the client?  How do we know that they don’t represent hexadecimals numbers [ABC 16 = , BACD 16 = , = ]?  Or even unary numbers [ABC = 3, BACD = 4, 916 = 3]?  It is the AF and the specification that make this explicit CSE 331 Autumn

 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

Next steps CSE 331 Autumn  Assignment 1  Due tonight 11:59PM  Assignment 2  out later today  due in two parts (M 11:59PM and F 11:59PM)  Lectures  Abstract data types (M)  Modular design (W)

CSE 331 Autumn