David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.

Slides:



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

1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
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.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Concurrent Programming.
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.
Understanding class definitions Looking inside classes.
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 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
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
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
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.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Comp 302: Software Engineering Data Abstractions.
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)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Views of Data Data – nouns of programming world the objects that are manipulated information that is processed Humans like to group information Classes,
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.
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.
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.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
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.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
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.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
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.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
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
Classes and Data Abstraction
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Lecture 5: Code Red, Ariane 5, and Gambling
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
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
CSE 331 Software Design & Implementation
Software Specifications
Reasoning about Data Abstractions
Presentation transcript:

David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions

11 September 2003CS 201J Fall Menu Data Abstraction Specifying Abstract Data Types Implementing Abstract Data Types

11 September 2003CS 201J Fall 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

11 September 2003CS 201J Fall 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

11 September 2003CS 201J Fall 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)

11 September 2003CS 201J Fall StringSet Abstract Type Concrete Representation class implementation clients down up { “test”, “okay” } private Vector res; s.isIn () public boolean isIn () { … }

11 September 2003CS 201J Fall Advantages/Disadvantages -More code to write and maintain -Run-time overhead (time to call method) +Client doesn’t need to know about representation +Localize impact of changes

11 September 2003CS 201J Fall StringSet Example StringSet abstract data type: represent a set of strings Support mathematical set operations: insert, isIn, size Create an empty set

11 September 2003CS 201J Fall Specifying Abstract Data Types Overview: what does the type represent –Mutability/Immutability A StringSet is a mutable set of Strings. –Abstract Notation A typical StringSet is { x 1, …, x n }. Operations: specifications for constructors and methods clients use –Describe in terms of abstract notation introduced in overview.

11 September 2003CS 201J Fall StringSet Specification public class StringSet // OVERVIEW: StringSets are unbounded, mutable sets of // Strings. A typical StringSet is { x1,..., xn } public StringSet () // EFFECTS: Initializes this to be empty: { } public void insert (String s) // MODIFIES: this // EFFECTS: Adds x to the elements of this: // this_post = this_pre U { s } public boolean isIn (String s) { // EFFECTS: Returns true iff s is an element of this. public int size () // EFFECTS: Returns the number of elements in this.

11 September 2003CS 201J Fall Components of Data Abstractions Ways to create objects of the data type –Creators: create new objects of the ADT from parameters of other types –Producers: create new objects of the ADT from parameters of the ADT type (and other types) Ways to observe properties: observers Ways to change properties: mutators

11 September 2003CS 201J Fall StringSet Operations Creators StringSet () Producers none Observers isIn, size Mutators insert public class StringSet // OVERVIEW: StringSets are unbounded, mutable sets of // Strings. A typical StringSet is { x1,..., xn } public StringSet () // EFFECTS: Initializes this to be empty: { } public void insert (String s) // MODIFIES: this // EFFECTS: Adds x to the elements of this: // this_post = this_pre U { s } public boolean isIn (String s) { // EFFECTS: Returns true iff s is an element of this. public int size () // EFFECTS: Returns the number of elements in this.

11 September 2003CS 201J Fall Using Abstract Data Types PS1, PS2 Client interacts with data type using the methods as described in the specification Client does not know the concrete representation

11 September 2003CS 201J Fall Implementing Abstract Data Types

11 September 2003CS 201J Fall Choosing a Representation Need a concrete data representation to store the 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

11 September 2003CS 201J Fall StringSet Representation Option 1: private String [] 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

11 September 2003CS 201J Fall Implementing StringSet public class StringSet { // OVERVIEW: StringSets are unbounded, mutable sets of Strings. // A typical StringSet 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?

11 September 2003CS 201J Fall It depends… public int size () { // EFFECTS: Returns the number of elements in this. StringSet uniqueels = new StringSet (); for (int i = 0; i < rep.size (); i++) { String current = (String) rep.elementAt (i); if (uniqueels.isIn (current)) { ; } else { uniqueels.insert (current); } return uniqueels.rep.size (); }

11 September 2003CS 201J Fall Is it correct? public int size () { // EFFECTS: Returns the number of // elements in this. return rep.size (); } public void insert (String s) { if (!isIn (s)) rep.add (s); }

11 September 2003CS 201J Fall 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?

11 September 2003CS 201J Fall What must we know to know 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 (); }

11 September 2003CS 201J Fall Rep Invariant The Representation Invariant expresses 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

11 September 2003CS 201J Fall 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 modified ADT object on exit.

11 September 2003CS 201J Fall Rep Invariant for StringSet public class StringSet { // OVERVIEW: StringSets are unbounded, // mutable sets of Strings. // A typical StringSet is {x1,..., xn} // Representation: private Vector rep; // RepInvariant (c) = //c contains no duplicates //&& c != null

11 September 2003CS 201J Fall 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!

11 September 2003CS 201J Fall Implementing Insert public void insert (String s) { // MODIFIES: this // EFFECTS: Adds s to the elements of this: // this_post = this_pre U { s } if (!isIn (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 }

11 September 2003CS 201J Fall Tomorrow in Section Return PS2 Consider how to implement the Graph datatype specified in your notes Think about possible representations before tomorrow’s class