Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions."— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions

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

3 11 September 2003CS 201J Fall 20033 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 11 September 2003CS 201J Fall 20034 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 11 September 2003CS 201J Fall 20035 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 11 September 2003CS 201J Fall 20036 StringSet Abstract Type Concrete Representation class implementation clients down up { “test”, “okay” } private Vector res; s.isIn () public boolean isIn () { … }

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

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

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

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

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

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

14 11 September 2003CS 201J Fall 200314 Implementing Abstract Data Types

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

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

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

18 11 September 2003CS 201J Fall 200318 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 (); }

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

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

21 11 September 2003CS 201J Fall 200321 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 (); }

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

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

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

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

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

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


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

Similar presentations


Ads by Google