Lecture 5 Abstract Data Types

Similar documents
CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

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

Lecture 5 Representation Invariants

Lecture 7: Data Abstractions

Representation Invariants and Abstraction Functions

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

Lecture 7 Abstraction Functions

Procedural Abstraction

EECS168 Exam 3 Review

Announcements. 1. Forms to return today after class:

CMSC131. Library Classes

hw6, BFS, debugging CSE 331 Section 5 10/25/12 Slides by Kellen Donohue

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

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

CSE 331 Software Design & Implementation

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

6.005 Elements of Software Construction Fall 2008

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

Abstraction Functions Lecture 9 Fall 2005

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Introduction to Nate Foster Spring 2018

Subclassing for ADTs Implementation

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

Software Design and Analysis for Engineers

Programming Languages and Techniques (CIS120)

Outline. Logistics. Logistics. Principles of Software (CSCI 2600) Spring Logistics csci2600/

Understanding an ADT implementation: Abstraction functions

Equality for Abstract Data Types

Software Design and Analysis for Engineers

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CSE / ENGR 142 Programming I

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

CS 4349 Lecture August 21st, 2017

Lecture 12: Abstraction Functions

Programming Languages

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CSE 373: Introduction, ADTs, Design Decisions, Generics. Michael Lee Wednesday Jan 3, 2017

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Lecture 3: Recursion; Structural Induction

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

CSE : Python Programming

What is an algorithm?

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

CSE 331 Software Design and Implementation. Lecture 14 Generics 1

Lecture 4: Procedure Specifications

CSE 143: Computer Programming II Summer 2015 HW6: 20 Questions (due Thursday, August 13, :30pm)

Array Based Lists. Collections

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

CS Introduction to Data Structures Week 1 Thursday

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Lecture 1. Course Overview Types & Expressions

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

3 ADT Implementation in Java

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSE 331 Software Design and Implementation. Lecture 4 Specifications

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

Where we are going (today)

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

Software Design and Analysis for Engineers

CE221 Programming in C++ Part 1 Introduction

Midterms Save the Dates!

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

Softwaretechnik. Lecture 08: Testing and Debugging Overview. Peter Thiemann SS University of Freiburg, Germany

MITOCW watch?v=kz7jjltq9r4

Programming Languages and Techniques (CIS120)

Algebraic Specifications

Announcements. Specifications. Outline. Specifications. HW1 is due Thursday at 1:59:59 pm

Announcements/Follow-ups

Where are we? Compiler. translating source code (C or Java) Programs to assembly language And linking your code to Library code

QUIZ. What is wrong with this code that uses default arguments?

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

Softwaretechnik. Lecture 08: Testing and Debugging Overview. Peter Thiemann SS University of Freiburg, Germany

Programming Languages

More on class design

More loops Ch

Welcome to... CS113: Introduction to C

COLLECTIONS & ITERATORS cs2420 Introduction to Algorithms and Data Structures Spring 2015

Starting to Program in C++ (Basics & I/O)

Announcements. Testing. Announcements. Announcements

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

Last time. Reasoning about programs. Coming up. Project Final Presentations. This Thursday, Nov 30: 4 th in-class exercise

Transcription:

CSE 331 Software Design and Implementation Lecture 5 Abstract Data Types Leah Perlmutter / Summer 2018

Platonic Forms Quote

Announcements

Announcements Section tomorrow! Loop reasoning useful for HW2 historically one of the most challenging concepts in 331 Development environment setup please install Eclipse and bring your laptop Eclipse installation instructions on the Course Website

Announcements Please attend the section that you are formally enrolled in Makes it possible to earn your section participation grade Makes your TAs lives much easier! HW0 feedback published on gradescope Reading assignment 2 posted, Quiz 2 coming soon! Due tomorrow: Thursday 6/28 at 10 pm HW2 is out! Due Monday 7/2 at 10 pm Topic is loop reasoning harder than HW1 so start early

What is an ADT?

Procedural and data abstractions lec04 Method Specification (abstraction) lec05 (today) Abstract Data Type (abstraction) IMPLEMENTS Method Body (concrete code) IMPLEMENTS Data Structure (concrete code)

Procedural and data abstractions Procedural abstraction: Abstract from details of procedures (e.g., methods) Specification is the abstraction Abstraction is the specification Satisfy the specification with an implementation Data abstraction: Abstract from details of data representation Also a specification mechanism A way of thinking about programs and design Standard terminology: Abstract Data Type, or ADT

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

The need for data abstractions (ADTs) Organizing and manipulating data is pervasive See also: CSE 332 Data Structures & Parallelism Start your design by designing data abstractions What is the meaning of the data? What operations will be permitted on the data by clients? Later, you can choose a data structure This means writing the implementation Decisions about data structures often made too early Very hard to change key data structures (modularity!)

An ADT is a set of operations ADT abstracts from the organization to meaning of data ADT abstracts from structure to use Here are two bad examples of how to implement a triangle class Why are they bad? class BadRightTriangle1 { float base, altitude; } class BadRightTriangle2 { float base, hypot, angle; } base altitude hypot angle base

An ADT is a set of operations ADT abstracts from the organization to meaning of data ADT abstracts from structure to use Here are two bad examples of how to implement a triangle class Why are they bad? class BadRightTriangle1 { float base, altitude; } class BadRightTriangle2 { float base, hypot, angle; } Instead, we should think of a type as a set of operations create, getbase, getaltitude, getbottomangle, Force clients to use operations to access data

An ADT is a set of operations class BadRightTriangle1 { float base, altitude; } class BadRightTriangle2 { float base, hypot, angle; } class RightTriangle { // fields don t matter to client! // Not part of ADT private float...; } // Operations are the important stuff. // Same ops, regardless of which fields we use public RightTriangle create(); public float getbase(); public float getaltitude(); public float getbottomangle();...

Are these classes the same? class BadPoint1 { class BadPoint2 { public float x; public float r; public float y; public float theta; } }

Are these classes the same? class BadPoint1 { class BadPoint2 { public float x; public float r; public float y; public float theta; } } Different: cannot replace one with the other in a program Same: both classes implement the concept 2-d point Goal of ADT methodology is to express the sameness: Analogy with Platonic Forms Clients depend only on the concept 2-d point

Are these classes the same? class Point1 { class Point2 { private float x; private float r; private float y; private float theta; // public ops.. // public ops.. } }

Concept of 2-d point, as an ADT Informal notation warning class Point { // A 2-d point exists in the plane,... public float x(); public float y(); public float r(); Observers 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); Creators/ Producers Mutators

Benefits of ADTs Suppose clients respect our data abstractions For example, it s a 2-D point with these operations Then, as the implementer, we can do these good things: Can delay decisions on how ADT is implemented Can fix bugs by changing how ADT is implemented Can change algorithms For performance In general or in specialized situations Debuggable Flexible We talk about an abstraction barrier A good thing to have and not cross

Abstract data type = objects + operations rest of program clients public operations Point x y r theta translate scale_rot abstraction barrier private fields implementation Implementation is hidden Only operations on objects of the type are provided by abstraction

Specifying an ADT

Specifying a data abstraction An abstract state Not the (concrete) representation in terms of fields, objects, Does not exist but used to specify the operations Excludes concrete state that implements the abstract state (more in upcoming lecture)

Abstract vs. Concrete State Example Possible Concrete State of an int list: Linked list of BigInteger Abstract State of an int list: Ordered sequence of integer values BigInteger(1) BigInteger(2) BigInteger(17) BigInteger(42) 1, 2, 17, 42 One Abstract State to rule them all! generalize across Possible Concrete State of an int list: Array of primitive ints [1, 2, 17, 42] Many more possible Concrete States...!

Specifying a data abstraction An abstract state e.g. the fact that an int list is a sequence of integer values Not the (concrete) representation in terms of fields, objects, Does not exist but used to specify the operations Excludes concrete state that implements the abstract state (more in upcoming lecture) A collection of procedural abstractions aka operations; aka method specs Excludes code e.g. a well specified set of list operations on an int list Each operation described in terms of creating, observing, producing, or mutating No operations other than those in the specification

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) Producers: ADT operations that return new values Mutators: Modify a value of an ADT Observers: Return information about an ADT

Implementing an ADT To implement a data abstraction (e.g., with a Java class): See next two lectures This lecture is just about specifying an ADT Nothing about the concrete representation appears in spec

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 (specification fields) 1. Overview: English description, states whether mutable or immutable 2. Define Abstract State for use in operation specifications Difficult and vital! Appeal to math if appropriate Give an example (reuse it in operation definitions) Excludes concrete state

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) 3. Creators New object, not part of pre-state: in effects, not modifies Overloading: distinguish procedures of same name by parameters (Example: two Poly constructors) Informal Notation Warning: slides omit full JavaDoc comments to save space; style might not be perfect either focus on main ideas

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)

Notes on observers 4. Observers Used to obtain information about objects of the type Return values of other types Never modify the abstract value Specification uses the abstraction from the overview this The particular Poly object being accessed Target of the invocation Also known as the receiver Poly x = new Poly(4, 3); int c = x.coeff(3); System.out.println(c); // prints 4

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()

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

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()

IntSet: observers // returns: true if and only if x Î 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()

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)

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 (if appropriate) 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

Mutable/Immutable ADTs (revisited) 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) Producers: ADT operations that return new values Mutators: Modify a value of an ADT Observers: Return information about an ADT

Why immutable? If you are curious, read Effective Java! Minimize Mutability (EJ2: 39; EJ3: 50)

Procedural and data abstractions lec04 Method Specification (abstraction) lec05 (today) Abstract Data Type (abstraction) IMPLEMENTS Method Body (concrete code) IMPLEMENTS Data Structure (concrete code)

Coming up Very related next lectures: Representation invariants Abstraction functions Distinct, complementary ideas for ADT reasoning

Closing

Closing Section tomorrow! install eclipse and bring laptop Quiz 2 due Thursday HW2 due Monday