Data Abstraction David Evans cs205: engineering software

Slides:



Advertisements
Similar presentations
Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.
Advertisements

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.
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
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.
Data Abstraction CS 201j: Engineering Software University of Virginia Computer Science Nathanael Paul
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
TECH Computer Science Data Abstraction and Basic Data Structures Improving efficiency by building better  Data Structure Object IN  Abstract Data Type.
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.
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
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)
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.
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.
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.
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.
Using the Java Collection Libraries COMP 103 # T2
EECE 310: Software Engineering
EECE 310: Software Engineering
Design David Evans cs205: engineering software
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
Lecture 15: Concurring Concurrently CS201j: Engineering Software
Concurring Concurrently
Classes and Data Abstraction
Introduction to Data Structures
Building Java Programs
Building Java Programs
Lecture 3: Abstraction by Specification CS201j: Engineering Software?
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Lecture 2: Implementing ArrayIntList reading:
Iteration Abstraction
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
Defining Classes and Methods
Data Structures and Algorithms for Information Processing
Lecture 7: Data Abstraction
CSE 331 Software Design & Implementation
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
EECE 310: Software Engineering
CSE 331 Software Design and Implementation
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
Lecture 13: Subtyping Rules Killer Bear Climber
Lecture 15: Quicker Sorting CS150: Computer Science
CSE 331 Software Design & Implementation
Type Abstraction SWE Spring 2013.
Reasoning about Data Abstractions
Lecture 3 – Data collection List ADT
Introduction to Classes and Objects
Presentation transcript:

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

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?

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

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

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

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

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)

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

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.

Generic Datatype public class Set<T> { ... 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)

Creating Specific Types public class Set<T> { ... public boolean add(T el) public T choose() public boolean contains(T el) } public class Set<String> { ... public boolean add(String el) public String choose() public boolean contains(String el) } Set<String>

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 { x1, …, xn }. Operations: procedural specifications for each operation (public methods and constructors); use the abstract notation introduced in overview.

Set Specification public class Set<T> { 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: thispost = thispre U { el } Returns true iff el was not an element of thispre.

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.

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

Implementing Abstract Data Types

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

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<T> rep; Easy to implement all methods Performance may be worse than for array

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

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

Is it correct? public int size () { // EFFECTS: Returns the number of // elements in this. return rep.size (); } public void insert (T el) { if (!contains (el)) rep.add (el); }

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?

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

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

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

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.

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

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

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

Charge PS3: out today, due next Monday Wednesday: Quiz 2 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