CSE 331 Software Design & Implementation

Similar documents
CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

Lecture 5 Abstract Data Types

6.170 Lecture 7 Abstract Data Types MIT EECS

So far. Specifications, conclusion. Abstract Data Types (ADTs) Comparison by Logical Formulas. Outline

Announcements. Representation Invariants and Abstraction Functions. Announcements. Outline

CSE 331 Software Design & Implementation

Representation Invariants and Abstraction Functions

Lecture 7: Data Abstractions

CSE 331 Software Design & Implementation

Sec$on 2: Specifica)on, ADTs, RI WITH MATERIAL FROM MANY

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

CSI33 Data Structures

CSE 331 Software Design & Implementation

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Subclassing for ADTs Implementation

CMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

6.005 Elements of Software Construction Fall 2008

CSE 331 Software Design & Implementation

Lecture 5 Representation Invariants

Lecture 4: Procedure Specifications

Lecture 12: Abstraction Functions

CSE / ENGR 142 Programming I

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Understanding an ADT implementation: Abstraction functions

CSE 331 Software Design & Implementation

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Abstraction Functions Lecture 9 Fall 2005

3 ADT Implementation in Java

Expected properties of equality

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Lecture 5: Methods CS2301

Chapter 4 Defining Classes I

EECS168 Exam 3 Review

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

Lecture 24 Wrap Up. Final Logistics CSE 331. Today. CSE 331 Software Design and Implementation. Wednesday, 8:30-10:20 AM

Designing data types. Fundamentals of Computer Science I

CSE 331 Software Design and Implementation. Lecture 24 Wrap Up

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

CS 251 Intermediate Programming Methods and Classes

Warmup. A programmer s roommate tells him, Would you mind going to the store and picking up a loaf of bread. Also, if they have eggs, get a dozen.

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Classes, interfaces, & documentation. Review of basic building blocks

Lecture 7 Abstraction Functions

Object-Oriented Design Lecture 16 CS 3500 Fall 2010 (Pucella) Friday, Nov 12, 2010

CSE 341: Programming Languages

Homework 5 ML module language assigned Thursday 20 March, due Friday 4 April

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

MITOCW watch?v=kz7jjltq9r4

Designing data types. Overview. Object Oriented Programming. Alan Kay. Object Oriented Programming (OOP) Data encapsulation. Checking for equality

Procedural Abstraction

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Software Design and Analysis for Engineers

CSI33 Data Structures

Lecture 19: Signatures, Structures, and Type Abstraction

L6: Abstract Data Types

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Cardinality of Sets MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Cardinality of Sets Fall / 15

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Lecture 11 Subtypes and Subclasses

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CSE 331 Software Design and Implementation. Lecture 12 Subtypes and Subclasses

Data Handing in Python

Solutions to Quiz 1 (March 14, 2016)

c) And last but not least, there are javadoc comments. See Weiss.

Array Based Lists. Collections

The Dynamic Typing Interlude

Object-Oriented Concepts

CSE 331. Programming by contract: pre/post conditions; Javadoc

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

CSE 230 Computer Science II (Data Structure) Introduction

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

Lecture 3: Recursion; Structural Induction

CSE wi Midterm Exam 2/8/18 Sample Solution

CS 251 Intermediate Programming Methods and More

JAVA: A Primer. By: Amrita Rajagopal

CSE 331 Software Design & Implementation

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CSE331 Winter 2014, Midterm Examination February 12, 2014

CS112 Lecture: Primitive Types, Operators, Strings

Refresher: Interface Specifications. ADT Documentation. Set Represented as an Array. Representation Invariant, Abstraction Function

L8: Interpreters & Visitors

Warmup. A programmer s roommate tells him, Would you mind going to the store and picking up a loaf of bread. Also, if they have eggs, get a dozen.

(I m not printing out these notes! Take your own.)

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

+ Today. Lecture 26: Concurrency 3/31/14. n Reading. n Objectives. n Announcements. n P&C Section 7. n Race conditions.

Representation Invariant, Abstraction Function

Part 4 CLASSES AS ABSTRACT DATA TYPES AND INTERFACE IMPLEMENTATION. Data Structures and Algorithms 31632

CS 231 Data Structures and Algorithms, Fall 2016

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

CSSE 220 Day 3. Check out UnitTesting and WordGames from SVN

The list abstract data type defined a number of operations that all list-like objects ought to implement:

Lecture 13 Hash Dictionaries

Transcription:

CSE 331 Software Design & Implementation Kevin Zatloukal Fall 2017 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, Dan Grossman, David Notkin, Hal Perkins, Zach Tatlock)

Outline This lecture: 1. What is an Abstract Data Type (ADT)? 2. How to write a specification for an ADT 3. Design methodology for ADTs Next lecture: Documenting an implementation of an ADT representation invariants abstraction functions 2

Procedural and data abstractions Procedural abstraction: abstract from implementation details of procedures (methods) specification is the abstraction satisfy the specification with an implementation Data abstraction: abstract from details of data representation also a specification mechanism way of thinking about programs and design standard terminology: Abstract Data Type or ADT invented by Barbara Liskov in the 70s one of the fundamental ideas of computer science 3

Why we need Data Abstractions (ADTs) Organizing and manipulating data is pervasive inventing and describing algorithms is less common Often best to start your design by designing data what operations will be permitted on the data by clients later decide how data be organized (data structures) see CSE 332 & CSE 344 4

Bad programmers worry about the code. Good programmers worry about data structures and their relationships. -- Linus Torvalds Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won t usually need your flowcharts; they ll be obvious. -- Fred Brooks 5

Why we need Data Abstractions (ADTs) Organizing and manipulating data is pervasive inventing and describing algorithms is less common Often best to start your design by designing data structures how will relevant data be organized what operations will be permitted on the data by clients see CSE 332 & CSE 344 Potential problems with choosing a data abstraction: hard to know ahead of time what to optimize programmers are notoriously bad at this (Liskov) if not done properly, hard to change key data structures 6

An ADT is a set of operations ADT abstracts from the organization to meaning of data ADT abstracts from data structures to use Representation should not matter to the client so hide it from the client Alternative representations of a right triangle: class RightTriangle { float base, altitude; } class RightTriangle { float base, hypot, angle; } Instead, think of a type as a set of operations create, getbase, getaltitude, getbottomangle, Force clients to use operations to access data 7

Are these classes the same? class Point { class Point { public float x; public float r; public float y; public float theta; } } Different Details: cannot replace one with the other in a program Same Concept: both classes implement the concept 2D point Goal of Point ADT is to express the sameness: clients should depend only on the concept 2D point achieve this by specifying operations not the representation write clients that can work with either representation 8

Benefits of ADTs If clients respect or are forced to respect data abstractions For example, it s a 2D point with these operations Can fix bugs by changing how ADT is implemented Can change algorithms For performance In general or in specialized situations Can delay decisions on how ADT is implemented We talk about an abstraction barrier a good thing to have and not cross (a.k.a. violate) 9

Abstract data type = objects + operations rest of program clients Implementation is hidden Point x y r theta translate scale_rot abstraction barrier implementation The only operations on objects are those provided by the abstraction 10

Concept of 2D point, as an ADT class Point { // A 2D point exists in the plane,... public float x(); public float y(); public float r(); public float theta(); } //... can be created,... public Point(); // new point at (0,0) public Point centroid(set<point> points); //... can be moved,... public void translate(float delta_x, float delta_y); public void scaleandrotate(float delta_r, float delta_theta); Observers / Getters Creators/ Producers Mutators 11

Specifying an ADT Immutable 1. overview 2. abstract state 3. creators 4. observers 5. producers 6. mutators Mutable 1. overview 2. abstract state 3. creators 4. observers 5. producers (rare) 6. mutators Creators: return new ADT values (e.g., Java constructors) Observers / Getters: Return information about an ADT Producers: ADT operations that return new values Mutators: Modify a value of an ADT 12

Implementing an ADT Next lecture will be about implementations of ADTs This lecture is about the ADTs themselves these are specifications should have no information about the implementation (latter called the concrete representation ) 13

Specifying a data abstraction A collection of procedural abstractions not a collection of procedures! Need a way write specifications for these procedures need a vocabulary for talking about what the operations do need to avoid referencing the actual implementation Use math to specify these procedures mathematical description of a state is called an abstract state describes what the state means not the implementation give clients an abstract way to think about the state each operation described in terms of creating, observing, producing, or mutating the abstract state 14

Poly, an immutable datatype: overview /** * A Poly is an immutable polynomial with * integer coefficients. A typical Poly is * c 0 + c 1 x + c 2 x 2 +... **/ class Poly { Abstract state Overview: state if immutable (default not) define abstract states for use in operation specifications difficult and vital! appeal to math if appropriate give an example (reuse it in operation definitions) make no reference to concrete representation 15

Poly: creators // effects: makes a new Poly = 0 public Poly() // effects: makes a new Poly = cx n // throws: NegExponent if n < 0 public Poly(int c, int n) Creators new object, so no pre-state: only effects, no modifies overloading: distinguish procedures of same name by parameters use with care (see Effective Java) will see alternative design patterns later on (Note: Javadoc above omits many details.) 16

Poly: observers // returns: the degree of this, // i.e., the largest exponent with a // non-zero coefficient. // Returns 0 if this = 0. public int degree() // returns: the coefficient of the term // of this whose exponent is d // throws: NegExponent if d < 0 public int coeff(int d) (Note: Javadoc above omits many details.) 17

Notes on observers Observers used to obtain information about objects of that type return values of other types never modify the abstract state specification uses the abstraction from the overview this abstract value of particular Poly object being accessed target of the method call (object on which the call was made) Poly x = new Poly(4, 3); int c = x.coeff(3); System.out.println(c); // prints 4 18

Poly: producers // returns: this + q (as a Poly) public Poly add(poly q) // returns: the Poly equal to this * q public Poly mul(poly q) // returns: -this public Poly negate() (Note: Javadoc above omits many details.) 19

Notes on producers Operations on a type that create other objects of the same type Common in immutable types like java.lang.string String substring(int offset, int len) No side effects never modify the abstract value of existing objects 20

IntSet, a mutable datatype: overview and creator // Overview: An IntSet is a mutable, // unbounded set of integers. A typical // IntSet is { x1,..., xn }. class IntSet { // effects: makes a new IntSet = {} public IntSet() (Note: Javadoc above omits many details.) 21

IntSet: observers // returns: true if and only if x in this public boolean contains(int x) // returns: the cardinality of this public int size() // returns: some element of this // throws: EmptyException when size()==0 public int choose() (Note: Javadoc above omits many details.) 22

IntSet: mutators // modifies: this // effects: this post = this pre + {x} public void add(int x) // modifies: this // effects: this post = this pre - {x} public void remove(int x) (Note: Javadoc above omits many details.) 23

Notes on mutators Operations that modify an element of the type Rarely modify anything (available to clients) other than this list this in modifies clause Typically have no return value do one thing and do it well (sometimes return old value that was replaced) Mutable ADTs may have producers too, but that is less common 24

Example: Text File Use case is writing an editor for an IDE: 25

Example: Text File Overview: telling users how to think about what this is Option 1: sequence of characters & colors Option 2: sequence of lines, each of which is a... sequence of characters & colors Both will probably require a method to take (line, col) to character Key difference: Option 1 suggests you can remove, e.g., chars 100 200, which may span multiple lines That is not natural in Option 2 (Option 1 makes more sense for Microsoft Word.) 26

Example: Text File Will use a sequence of lines. What is each line? Option 1: pair (sequence of characters, sequence of colors) Option 2: sequence of pairs (character, color) Option 3: sequence of pairs (sequence of characters, color) Key differences: Option 1 must make clear that the sequences are same length Option 1 & 2 should let you insert (char, color) at given column Option 3 should let you find the (text, color) token containing a given column and then change its text to include a new char 27

Example: Text File // Overview: Represents a text file, which is a sequence // of lines of text. Each line of text is a sequence of // (character, color) pairs. // // Example: [[( a, black), ( b, red)], [( c, green)]] // is the text: // ab // c // (on two lines), where a is black, b is red, & c is green public class TextFile { } //... 28