Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.

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.
CS Data Structures II Review COSC 2006 April 14, 2017
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
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.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Chapter 7 Stacks II CS Data Structures I COSC 2006
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
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
CSC 205 Programming II The ADT Stack. Recap: ADT Abstract Data Type A collection of data (objects) A set of operations on that data Add Remove Retrieve.
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.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Polymorphism (generics) CSE 331 University of Washington.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
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.
Fusion Design Overview Object Interaction Graph Visibility Graph Class Descriptions Inheritance Graphs Fusion: Design The overall goal of Design is to.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES Autumn 2011.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
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.
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.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Section 3.7 Linked-Based Implementations
EECE 310: Software Engineering
Sections 3.4 Formal Specification
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Chapter 6: The Stack Abstract Data Type
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Chapter 6: The Stack Abstract Data Type
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
Lecture 5: Code Red, Ariane 5, and Gambling
Data Abstraction David Evans cs205: engineering software
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
EECE 310: Software Engineering
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
Reasoning about Data Abstractions
CHAPTER 3: Collections—Stacks
Presentation transcript:

cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans

Menu Implementing Data Abstractions Abstraction Function Representation Invariant

Recap: 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 variables are only visible inside the class public class StringStack { // Rep: private List rep; public class StringStack { // Rep: private List rep;

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)

StringStack Abstract Type Concrete Representation class implementation clients down up StringStack private List rep; s.push (“Hello”); public void push(String s) { rep.add(s); } public void push(String s) { rep.add(s); }

Advantages/Disadvantages of Data Abstraction - More code to write and maintain - Run-time overhead (time to call method, lost opportunities because of abstraction) + Client doesn’t need to know about representation + Can change rep without changing clients + Can reason about clients at abstract level

Choosing a Representation Representation must store the abstract state Think about how methods will be implemented A good representation choice should: Enable easy implementations of all methods Allow performance-critical methods to be implemented efficiently Use memory efficiently (if the data may be large) Choosing the rep is the most important decision in implementing a data abstraction

StringStack Representation Option 1: private String [] rep; – Recall Java arrays are bounded – Hard to implement push Option 2: private List rep; – Easy to implement all methods – Performance may be worse than for array

Implementing StringStack Is this implementation of push correct? public class StringStack { // Rep: private List rep; /** * MODIFIES: this * EFFECTS: Pushes s on the top of this. * For example, if this_pre = [ e_n-1, e_n-2,..., e_1, e_0 ], * this_post = [ s, e_n-1, e_n-2,..., e_1, e_0 ] */ public void push(String s) { rep.add(s); } Could this implementation of push be correct?

It depends… /** * MODIFIES: this * EFFECTS: If this is empty, throws EmptyStackException. Otherwise, * returns the element on top of this and removes that element from this. * For example, if this_pre = [ e_n-1, e_n-2,..., e_1, e_0 ], * this_post = [ e_n-2,..., e_1, e_0 ] and the result is e_n-1. */ public String pop() throws EmptyStackException { try { return rep.remove(0); } catch (IndexOutOfBoundsException e) { assert rep.size() == 0; throw new EmptyStackException(); } public String pop() throws EmptyStackException { try { return rep.remove(rep.size() - 1); } catch (IndexOutOfBoundsException e) { assert rep.size() == 0; throw new EmptyStackException(); } public String pop() throws EmptyStackException { try { return rep.remove(rep.size() - 1); } catch (IndexOutOfBoundsException e) { assert rep.size() == 0; throw new EmptyStackException(); }

Is it correct? 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 pop is correct? /** * MODIFIES: this * EFFECTS: If this is empty, throws EmptyStackException. Otherwise, * returns the element on top of this and removes that element from this. * For example, if this_pre = [ e_n-1, e_n-2,..., e_1, e_0 ], * this_post = [ e_n-2,..., e_1, e_0 ] and the result is e_n-1. */ public String pop() throws EmptyStackException { try { return rep.remove(0); } catch (IndexOutOfBoundsException e) { assert rep.size() == 0; throw new EmptyStackException(); } /** * MODIFIES: this * EFFECTS: If this is empty, throws EmptyStackException. Otherwise, * returns the element on top of this and removes that element from this. * For example, if this_pre = [ e_n-1, e_n-2,..., e_1, e_0 ], * this_post = [ e_n-2,..., e_1, e_0 ] and the result is e_n-1. */ public String pop() throws EmptyStackException { try { return rep.remove(0); } catch (IndexOutOfBoundsException e) { assert rep.size() == 0; throw new EmptyStackException(); }

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.

Abstraction Function for StringStack /** * OVERVIEW: A StringStack represents a last-in-first-out stack where all * elements are Strings. * A typical stack is [ e_n-1, e_n-2,..., e_1, e_0 ] where e_n-1 is the top * of the stack. */ public class StringStack { // Rep: private List rep;

Correctness of Push Use abstraction function to show push satisfies its specification: AF (rep_post) = [ AF String (s)] + AF (rep_pre) /** * MODIFIES: this * EFFECTS: Pushes s on the top of this. For example, if this_pre = [ e_n-1, e_n-2,..., e_1, e_0 ], this_post = [ s, e_n-1, e_n-2,..., e_1, e_0 ] */ public void push(String s) { rep.add(s); }

/** * MODIFIES: this * EFFECTS: Pushes s on the top of this. For example, if this_pre = [ e_n-1, e_n-2,..., e_1, e_0 ], this_post = [ s, e_n-1, e_n-2,..., e_1, e_0 ] */ public void push(String s) { rep.add(s); } How do we know rep is non-null?

Rep Invariant Representation Invariant: properties all legitimate objects of the ADT must satisfy I : C → boolean Function from concrete representation to boolean. Helps us reason about correctness of methods independently Limits the range of inputs for the Abstraction Function

Reasoning with Rep Invariants Prove all objects satisfy the invariant before leaving the implementation code Assume all objects passed in satisfy the invariant REQUIRES: Rep Invariant is true for this (and any other reachable ADT objects) EFFECTS: Rep Invariant is true for all new and any modified ADT object on exit. REQUIRES: Rep Invariant is true for this (and any other reachable ADT objects) EFFECTS: Rep Invariant is true for all new and any modified ADT object on exit. All non-private datatype operations have these specification implicitly!

Rep Invariant for StringStack /** * OVERVIEW: A StringStack represents a last-in-first-out stack where all * elements are Strings. * A typical stack is [ e_n-1, e_n-2,..., e_1, e_0 ] where e_n-1 is the top * of the stack. */ public class StringStack { // Rep: private List rep;

Graph public class Graph // OVERVIEW: A Graph is a mutable type that represents an undirected graph. It consists of // nodes that are named by Strings, and edges that connect a pair of nodes. // A typical Graph is: where // Nodes = { n_1, n_2, …, n_m } // Edges = { {a_1, b_1},..., {a_n, b_n} } (the elements of Edges are unordered sets). public Graph () // EFFECTS: Initializes this to a graph with no nodes or edges:. public void addNode (String name) throws DuplicateException // MODIFIES: this // EFFECTS: If name is in Nodes, throws DuplicateException. // Otherwise, adds a node named name to this: // this_post = public void addEdge (String s, String t) throws NoNodeException, DuplicateException // MODIFIES: this // EFFECTS: If s and t are not names of nodes in this, throws NoNodeException. If there is // already an edge between s and t, throws DuplicateEdgeException. Otherwise, adds an // edge between s and t to this: // this post =

Charge Problem Set 3: Designing and Implementing Data Abstractions: Due Tuesday