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

Similar documents
Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

Software Construction

CSE 331 Spring 2018 Midterm

Representation Invariants and Abstraction Functions

Top Down Design vs. Modularization

6.170 Lecture 7 Abstract Data Types MIT EECS

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

Outline. applications of hashing equality and comparisons in Java sorting selection sort bubble sort insertion sort

Solutions to Quiz 1 (March 14, 2016)

Exam 1 CSCI 2600 Principles of Software October 6, 2015

Exam 1 CSCI 2600 Principles of Software October 6, 2015

Expected properties of equality

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

CSE 331 Midterm Exam Sample Solution 2/18/15

Equality. Michael Ernst. CSE 331 University of Washington

List ADT. Announcements. The List interface. Implementing the List ADT

Announcements. Equality. Lecture 10 Equality and Hashcode. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

Subclassing for ADTs Implementation

Understanding an ADT implementation: Abstraction functions

CSC 1052 Algorithms & Data Structures II: Lists

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Building Java Programs. Inheritance and Polymorphism

MIT EECS Michael Ernst Saman Amarasinghe

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

Java Review: Objects

Announcements. Representation Invariants and Abstraction Functions. Announcements. Outline

What is the Java Collections Framework?

Lecture 7: Data Abstractions

Equality. Michael Ernst. CSE 331 University of Washington

Solutions to Quiz 1 (October 25, 2017)

6.005 Elements of Software Construction Fall 2008

Introduction to Object-Oriented Programming

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

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

The class Object. Lecture CS1122 Summer 2008

JAVA MOCK TEST JAVA MOCK TEST II

CSE331 Autumn 2011 Midterm Examination October 28, 2011

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

CSE wi Midterm Exam 2/8/18. Name UW ID #

Ryerson University Department of Electrical & Computer Engineering COE618 Midterm Examination February 26, 2013

TeenCoder : Java Programming (ISBN )

L6: Abstract Data Types

Creating an Immutable Class. Based on slides by Prof. Burton Ma

Representation invariants and abstraction functions CSE 331 UW CSE

Array Based Lists. Collections

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Lecture 5 Representation Invariants

CompuScholar, Inc. 9th - 12th grades

Quiz 1 (October 25, 2017)

Java: advanced object-oriented features

Solutions to Quiz 1 (October 19, 2015)

Quiz 1 (March 14, 2016)

CSE wi Midterm Exam 2/8/18 Sample Solution

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

For this section, we will implement a class with only non-static features, that represents a rectangle

Lecture 7 Abstraction Functions

Objects and Iterators

Arrays. Chapter Arrays What is an Array?

CS11 Java. Winter Lecture 8

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

CS 231 Data Structures and Algorithms, Fall 2016

Quiz 2 (April 22, 2016)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Classes Classes 2 / 36

Representation Invariant, Abstraction Function

CS Introduction to Data Structures Week 1 Thursday

CS 520 Theory and Practice of Software Engineering Fall 2018

INTRODUCTION TO SOFTWARE SYSTEMS (COMP1110/COMP1140/COMP1510/COMP6710)

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

MIT AITI Lecture 18 Collections - Part 1

Classes Classes 2 / 35

CS 351 Design of Large Programs Programming Abstractions

CS 520 Theory and Practice of Software Engineering Fall 2017

Introduction to Programming Using Java (98-388)

CONTAİNERS COLLECTİONS

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

CS 520 Theory and Practice of Software Engineering Fall 2017

Programming Languages and Techniques (CIS120)

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Linked List. ape hen dog cat fox. tail. head. count 5

Building Java Programs

Solutions to Quiz 1 (March 22, 2019)

Chapter 4 Defining Classes I

MAT 3670: Lab 3 Bits, Data Types, and Operations

Programming II (CS300)

Lecture 6: ArrayList Implementation

Exercise 10 Object Structures and Aliasing November 27, 2015

CSE 331 Midterm Exam 2/13/12

java.lang.object: Equality

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

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

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

DM503 Programming B. Peter Schneider-Kamp.

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

Programming Languages and Techniques (CIS120)

Java Fundamentals (II)

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Transcription:

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

Limits of procedural abstractions Isolate implementation from specification Dependency on the types of parameters representation (implementation) interpretation Local changes of a type => global changes in all procedures Hard to modify/maintain software Example: representation change: list -> binary tree interpretation change: sort order Need to isolate representation from specification

Data abstractions abstract from implementation abstract data type (ADT) = <objects, operations> storage structures - type representation applicable operations - behavior access the representation through operations independence from representation locality of change kinds: create, modify, inspect defer choice of representation built-in vs. user-defined types (primitive vs. classes)

Data abstraction specifications define behavior more important than representation specification => what the operations do structure: overview description through known concepts (math) constructors - initialize methods - access mutable vs. immutable data types abstract data type in Java => class: name, constructors, methods (public) belong to objects (class instances) this implicit argument for all methods

List class example: /** * A mutable ordered collection of objects L (sequence), * for example L = <O 1,,O n >. * A list L is a mapping: L : {O 1, O n } -> {0,,n-1}, where * the i-th element is denoted as elem(i) */ public class List { public List (); public void insert (int i, Object e); public void set(int i, Object e); public void remove(int i); public int size(); public Object get(int i); public List sublist(int i, int j); }

Classes of operations creators: create new objects of a type (some constructors) producers: create new objects from existing objects (constructors, methods - e.g. concat, substring) mutators: modify objects (methods) observers: provide information about objects (methods) in summary (where: t other types, T current ADT) : creator: t -> T producer: T -> T mutator: T, t -> void observer: T, t -> t

List class example: public void get(int i) throws IndexOutOfBoundsException; // effects: throws IndexOutOfBoundsException, if i < 0, i >= n // else returns i-th element of this public void insert(int i, Object e) throws IndexOutOfBoundsException; // modifies: this // effects: throws IndexOutOfBoundsException if i < 0, i > n, // else changes this to <O 1,,e i,,o n > public void set(int i, Object e) throws IndexOutOfBoundsException; // modifies: this // effects: throws IndexOutOfBoundsException if i < 0, i >= n // else elem(i) = e public List sublist(int i, int j) throws IndexOutOfBoundsException; // effects: throws IndexOutOfBoundsException if i < 0, i > j, j >= n

Immutable: class typename { 1. overview 2. creators 3. observers 4. producers } Mutable: class typename { 1. overview 2. creators 3. observers 4. mutators } Designing abstract data types Mutability modeled concept (integers vs. sets) safety, sharing, and performance Operations - simple building blocks, easy to combine Adequacy - sufficient set of operations depends on potential uses fully populated types - obtain all possible states minimal set of operations

Implementing data types choice of representation, e.g. linked list - faster insert array - faster get, set representation independence, errors: exposing the representation return mutable objects use mutable objects In Java instance variables (mutable vs. immutable) internal methods records - collections of fields (no abstraction, no special operations) language support for independence

Language support visibility private - local to class package - local to package (protected - later) interfaces - declare only operations public interface List { void add (int i, Object e); void set (int i, Object e); void remove (int i); int size (); Object get (int i); } public class LinkedList implements List {...} public class ArrayList implements List {...} List lst = new LinkedList();

Standard operations Inherited from Object equals() - behavioral equivalence clone() independent copies copy object: o.clone.equals(o) == false shallow vs. deep tostring() - represent current object state as text hashcode() map objects to integers used in hash tables May have to redesign for new classes

Object identity Compare by reference or by state? Mutable objects - == Immutable objects - equals Object default - by reference Wrong for immutable types - need deep equals Properties of equals symmetric: a.equals(b) => b.equals(a) reflexive: a.equals(a) == true transitive: a.equals(b) && b.equals(c) => that a.equals(c) null preserving: if a!= null a.equals(null) is false consistent: if a.equals(b) now a.equals(b) later if neither has been modified

Understanding and reasoning about data abstractions Not always clear what is represented Not all values for a representation are reasonable abstraction function (AF) specify the interpretation of objects mapping: AF : Object -> AbstractObject rep invariant (RI) characterize well-formed instances mathematical formula: RI : Object -> boolean

Abstraction function Sets Lists many-to-one implemented as tostring

Representation invariant Integral part of representation Formal or informal description Can be violated inside methods LinkedList example: Add size field size == n Express all constraints on which methods depend Can be violated inside methods Implemented as a method repok Can be used in assertions

Reasoning about data types Modular reasoning Inductive reasoning - ensure that constructors produce valid objects => mutators and producers preserve invariants Assume input is valid