Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans www.cs.virginia.edu/cs205.

Slides:



Advertisements
Similar presentations
Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
Advertisements

Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.
COMPSCI 105 S Principles of Computer Science 12 Abstract Data Type.
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.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
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
Ranga Rodrigo. Class is central to object oriented programming.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
CompSci 105 SS 2005 Principles of Computer Science Lecture 4 Lecturer: Santokh Singh.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Data Abstraction CS 201j: Engineering Software University of Virginia Computer Science Nathanael Paul
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
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.
Two Parts of Every ADT An abstract data type (ADT)  is a type for encapsulating related data  is abstract in the sense that it hides distracting implementation.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
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.
Chapter 4 Data Abstraction: The Walls. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Abstract Data Types Modularity –Keeps the complexity of a.
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)
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Polymorphism (generics) CSE 331 University of Washington.
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Understanding ADTs CSE 331 University of Washington.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
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.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
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.
Fusion Design Overview Object Interaction Graph Visibility Graph Class Descriptions Inheritance Graphs Fusion: Design The overall goal of Design is to.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES Autumn 2011.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
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.
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.
CSCE 240 – Intro to Software Engineering Lecture 3.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
EECE 310: Software Engineering
EECE 310: Software Engineering
Design David Evans cs205: engineering software
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
CSE 331 Software Design & Implementation
Data Abstraction David Evans cs205: engineering software
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
EECE 310: Software Engineering
CSE 331 Software Design and Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
Type Abstraction SWE Spring 2013.
Reasoning about Data Abstractions
Presentation transcript:

cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans

2 cs205: engineering software Managing Complexity Modularity –Divided problem into procedures –Used specifications to separate what from how A big program can have thousands of procedures –How can we group them into modules?

3 cs205: engineering software Abstract Data Types Separate what you can do with data from how it is represented Client interacts with data through provided operations according to their specifications Implementation chooses how to represent data and implement its operations

4 cs205: engineering software Data Abstraction in Java A class defines a new data type Use private instance variables to hide the choice of representation –private declarations are only visible inside the class

5 cs205: engineering software Up and Down Abstract Type Concrete Representation class implementation clients down up The representation of an abstract data type is visible only in the class implementation. Clients manipulate an abstract data type by calling its operations (methods and constructors)

6 cs205: engineering software Cell State Representation private boolean alive; public boolean isAlive () { return alive; } Abstract Type Concrete Representation class implementation clients down up CellState cs.isAlive ()

7 cs205: engineering software Advantages/Disadvantages - More code to write and maintain - Run-time overhead (time to call method) + Client doesn’t need to know about representation + Suppose we want to add more states (e.g., question 2)

8 cs205: engineering software Set Example (ps2) Set abstract data type: represent a set of objects Operations: –Create an empty set –Mathematical set operations: add, contains, size, remove, union

9 cs205: engineering software Type Parameters We want to have sets of different types of objects How should we declare the Set methods? public boolean add(?? elem) public boolean contains(?? elem) public ?? choose() We don’t want just one Set datatype. We want different Sets for different element types.

10 cs205: engineering software Generic Datatype public class Set {... public boolean add(T el) public T choose() public boolean contains(T el)... } Note: Java did not support generic datatypes until version 1.5 (this is why the book doesn’t use them)

11 cs205: engineering software Creating Specific Types public class Set {... public boolean add(T el) public T choose() public boolean contains(T el)... } public class Set {... public boolean add(String el) public String choose() public boolean contains(String el)... } Set

12 cs205: engineering software Abstract Data Type Specifications Overview: what the type represents –Mutability/Immutability A Set is a mutable, unbounded set of objects of type T. –Abstract Notation A typical Set is { x 1, …, x n }. Operations: procedural specifications for each operation (public methods and constructors); use the abstract notation introduced in overview.

13 cs205: engineering software Set Specification public class Set { OVERVIEW: A Set is a mutable, unbounded set of objects of type T. A typical Set is {x_1,..., x_n }. public Set() EFFECTS: Initializes this to an empty set: { }. public boolean add(T el) MODIFIES: this EFFECTS: Adds el to the elements of this: this post = this pre U { el } Returns true iff el was not an element of this pre.

14 cs205: engineering software contains EFFECTS: Checks if el is an element of this and returns true if it is. EFFECTS: Returns true iff el is an element of this. EFFECTS: Returns el  this. EFFECTS: Returns el isIn this.

15 cs205: engineering software union public void union(Set t) MODIFIES: this EFFECTS: Adds the elements of t to this. MODIFIES: this EFFECTS: this_post = this_pre  t Specifications should be declarative (what the outcome is), not operational (how it does it).

16 cs205: engineering software Implementing Abstract Data Types

17 cs205: engineering software Choosing a Representation Need a concrete data representation to store the state –Think about how it maps to abstract state –Think about how methods will be implemented A good representation choice should: –Enable straightforward implementations of as many methods as possible –Allow performance-critical methods to be implemented efficiently

18 cs205: engineering software Set Representation Option 1: private T [] rep; –Recall Java arrays are bounded –Easy to implement most methods, hard to implement insert Option 2: private Vector rep; –Easy to implement all methods –Performance may be worse than for array

19 cs205: engineering software Implementing Set public class Set { // OVERVIEW: Sets are unbounded, mutable sets of elements of type T. // A typical Set is {x1,..., xn} // Representation: private Vector rep; public StringSet () { // EFFECTS: Initializes this to be empty: { } rep = new Vector (); } public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } rep.add (s); } Could this implementation of insert be correct?

20 cs205: engineering software It depends… public int size () { // EFFECTS: Returns the number of elements in this. Set unique = new Set (); for (T el : rep) { if (!unique.isIn (el)) { unique.add (current); } return unique.rep.size (); }

21 cs205: engineering software Is it correct? public int size () { // EFFECTS: Returns the number of // elements in this. return rep.size (); } public void insert (String s) { if (!contains (s)) rep.add (s); }

22 cs205: engineering software Reasoning About Data Abstractions How can we possibly implement data abstractions correctly if correctness of one method depends on how other methods are implemented? How can we possibly test a data abstraction implementation if there are complex interdependencies between methods?

23 cs205: engineering software What must we know to know if size is correct? This implementation is correct only if we know the rep does not contain duplicates public int size () { // EFFECTS: Returns the number of // elements in this. return rep.size (); }

24 cs205: engineering software To Reason about Operations We need to know: How the concrete rep maps to abstract values: Abstraction Function What values of the concrete rep are valid: Representation Invariant

25 cs205: engineering software Rep Invariant Predicate that all legitimate objects of the ADT must satisfy I : C → boolean Helps us reason about correctness of methods independently –Prove all objects satisfy the invariant before leaving the implementation code –Assume all objects passed in satisfy the invariant

26 cs205: engineering software Reasoning with Rep Invariants REQUIRES: Rep Invariant is true for this (and any other reachable ADT objects) EFFECTS: Rep Invariant is true for all new and modified ADT objects on exit. Every public datatype operation implicitly includes these preconditions and postconditions.

27 cs205: engineering software Rep Invariant for Set public class Set { // Representation: private Vector rep; // RepInvariant (c) = c contains no duplicates or // RepInvariant (c) = // forall i, j: rep[i].equals(rep[j]) // only when i == j

28 cs205: engineering software Implementing Insert? public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } rep.add (s); } Not a correct implementation: after it returns this might not satisfy the rep invariant!

29 cs205: engineering software Implementing Insert public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } if (!contains (s)) { rep.add (s); } } Possibly correct implementation: we need to know how to map rep to abstraction notation to know if this_post = this_pre U { s }

30 cs205: engineering software Abstraction Function The Abstraction Function maps a concrete state to an abstract state: AF : C → A Function from concrete representation to the abstract notation introduced in overview specification. What is the range of the Abstraction Function? Range is concrete states for which rep invariant is true

31 cs205: engineering software Abstraction Function for Set public class Set { // OVERVIEW: Sets are unbounded, // mutable sets of objects of type T. // A typical Set is {x1,..., xn} // Representation: private Vector rep; // AF (c) = // { AF T (c.rep.elementAt(i)) | 0 <= i < c.rep.size () }

32 cs205: engineering software Correctness of Insert public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } if (!contains (s)) { rep.add (s); } } Use abstraction function to show if add implements its specification, then AF (rep_post) = AF (rep_pre) U { AF String ( s)}

33 cs205: engineering software Reality Check Writing abstraction functions, rep invariants, testing code thoroughly, reasoning about correctness, etc. for a big program is a ridiculous amount of work! Does anyone really do this? –Yes (and a lot more), but usually only when its really important to get things right: Cost per line of code: –Small, unimportant projects: $1-5/line –WindowsNT: about $100/line –FAA’s Automation System ( ): $900/line

34 cs205: engineering software Charge PS3: out today, due next Monday –Reason about data types using abstraction functions and rep invariants –Implement the DirectedGraph abstract data type you used in PS2 Wednesday: Quiz 2 –Mostly on data abstraction –Chapter 5 and lectures –Maybe a question or two on testing