cs205: engineering software

university of virginia fall 2006

Data Abstraction

www.cs.virginia.edu/cs205

David Evans

Managing Complexity • Modularity – Divided problem into procedures – Used specifications to separate what from how • A big program can have thousands of procedures cs205: engineering software 2 .

Data Abstraction • We need new data types. not just procedures – Could you implement PS2 without the DirectedGraph type? – We could make procedures. but what would you pass to them? • Organize program around abstract data types cs205: engineering software 3 .

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 cs205: engineering software 4 .

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 cs205: engineering software 5 .

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

} cs205: engineering software 7 . // either alive or dead.Example: CellState public class CellState { // OVERVIEW: A CellState is an immutable // object that represents the state of a cell. static public CellState createDead () // EFFECTS: Returns a dead cell state. public boolean isAlive () // EFFECTS: Returns true iff this is alive. static public CellState createAlive() // EFFECTS: Returns an alive cell state.

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

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

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

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. cs205: engineering software 11 . We want different Sets for different element types.

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

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

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

unbounded set of objects of type T. x_n }.. A typical Set is {x_1. cs205: engineering software 15 ..Set Specification public class Set<T> implements Iterable<T>. . public Set() EFFECTS: Initializes this to an empty set: { }. Collection<T> { OVERVIEW: A Set is a mutable.. 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.

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 cs205: engineering software 16 .

Set Operations public class Set<T> implements Iterable<T>. public boolean contains(Object el) EFFECTS: Returns true iff el is an element of this. A typical Set is {x_1... unbounded set of objects of type T. public Set(Set<T> s) EFFECTS: Initializes this to a set containing the same elements as the set s (a shallow copy). public Set() EFFECTS: Initializes this to an empty set: { }. MODIFIES: this EFFECTS: Adds el to the elements of this: public boolean isEmpty() this_post = this_pre U { el } EFFECTS: Returns true iff this has no elements. x_n }.. public int size() public boolean add(T el) EFFECTS: Returns the number of elements in this. . public void union(Set<T> t) MODIFIES: this EFFECTS: this_post = this_pre U t public boolean remove(Object el) MODIFIES: this EFFECTS: Removes el from this: this_post = this_pre .{ el } Returns true iff el is in this_pre cs205: engineering software 17 . Collection<T> { OVERVIEW: A Set is a mutable. public T choose() REQUIRES: this has at least one element EFFECTS: Returns an element of this. Returns true iff el was not an element of this_pre.

PS2 • Several datatype implementations provided with specifications • Client interacts with data type using the methods as described in the specification • Client does not know the concrete representation • Your code should work correctly with any correct implementation of the specified datatype cs205: engineering software 18 .

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.