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

Similar documents
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.

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.

Lecture 5 Representation Invariants

CSE 331 Software Design & Implementation

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

Lecture 5 Abstract Data Types

Representation Invariants and Abstraction Functions

CSE 331 Software Design & Implementation

CSE wi Midterm Exam 2/8/18 Sample Solution

Announcements. Representation Invariants and Abstraction Functions. Announcements. Outline

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

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

6.170 Lecture 7 Abstract Data Types MIT EECS

Solutions to Quiz 1 (March 14, 2016)

CSE 331 Software Design & Implementation

Implementing an ADT: Representation invariants

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE 331 Software Design & Implementation

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

Quiz 1 (March 14, 2016)

CSE 331 Midterm Exam 2/13/12

Solutions to Quiz 1 (October 25, 2017)

Representation invariants and abstraction functions CSE 331 UW CSE

CSE 331 Midterm Exam Sample Solution 2/13/12

Lecture 7: Data Abstractions

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

Solutions to Quiz 2 (December 3, 2018)

Modules and Representa/on Invariants

Solutions to Quiz 1 (March 22, 2019)

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

CSE331 Autumn 2011 Midterm Examination October 28, 2011

CSE 373: Data Structures and Algorithms More Asympto<c Analysis; More Heaps

Lecture 7 Abstraction Functions

6.005 Elements of Software Construction Fall 2008

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

CSE 331 Final Exam 3/16/15 Sample Solution

Vulnerability Analysis (III): Sta8c Analysis

Quiz 1 (October 25, 2017)

Top Down Design vs. Modularization

Assertions. Assertions - Example

Principles of So3ware Construc9on. A formal design process, part 2

CSE 331 Final Exam 6/5/17. Name UW ID#

Programming Languages and Techniques (CIS120)

CSE 331 Software Design & Implementation

Charlie Garrod Michael Hilton

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

CS/ENGRD 2110 SPRING 2018

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

Reasoning About Imperative Programs. COS 441 Slides 10

CSE332: Data Abstrac0ons Sec%on 1. Hye In Kim Winter 2014

A formal design process, part 2

CSE 331 Final Exam 12/14/15 Sample Solution. Question 1. (20 points, 1 each) Warmup. For each statement, circle T if it is true and F if it is false.

Programming Languages Seminar. Lecture in the subject of. Yair Moshe

Homework 1 Simple code genera/on. Luca Della Toffola Compiler Design HS15

Section 4: Graphs and Testing

Java. Package, Interface & Excep2on

Expected properties of equality

CSE 331 Software Design & Implementation

Agenda. Address vs. Value Consider memory to be a single huge array. Review. Pointer Syntax. Pointers 9/9/12

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

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

Solutions to Quiz 1 (October 19, 2015)

Programming Languages and Techniques (CIS120)

Announcements. Testing. Announcements. Announcements

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

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

Programming Languages and Techniques (CIS120)

CSE 331 Midterm Exam Sample Solution 2/18/15

Section 4: Graphs and Testing. Slides by Erin Peach and Nick Carney

What you see is what you get?

CSE331 Winter 2014, Midterm Examination February 12, 2014

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections

TAKS Mathematics Practice Tests Grade 6, Test B

Quiz 2 (December 3, 2018)

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

Representation Invariant, Abstraction Function

CSE 331 Final Exam 6/5/17 Sample Solution

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Midterm Exam 11/9/15 Sample Solution

Chapter 5 Lab Methods

The Dynamic Typing Interlude

Another interface: Comparable

a correct statement? You need to know what the statement is supposed to do.

You will need the following items: scissors, plate, 5 different colored pencils, protractor, paper to answer questions

SQL- Updates, Asser0ons and Views

JAVA MOCK TEST JAVA MOCK TEST II

How a computer runs a program. Binary Representa8on and Opera8ons on Binary Data. Opera8ng System 9/17/13. You can view binary file contents

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

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Java: advanced object-oriented features

Exam Review. CSE 331 Section 10 12/6/12. Slides by Kellen Donohue with material from Mike Ernst

Floa.ng Point : Introduc;on to Computer Systems 4 th Lecture, Sep. 10, Instructors: Randal E. Bryant and David R.

Programming Languages and Techniques (CIS120e)

Quiz 2 (November 20, 2015)

Sept 26, 2016 Sprenkle - CSCI Documentation is a love letter that you write to your future self. Damian Conway

Equality. Michael Ernst. CSE 331 University of Washington

Announcements. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Intuition: Type Signature is a Specification.

8/25/17. Demo: Create application. CS2110, Recita.on 1. Arguments to method main, Packages, Wrapper Classes, Characters, Strings.

Transcription:

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

Agenda Announcements HW1: due today at 23:59 pm Don t forget to commit/push your changes THIS INCLUDES TAGGING YOUR FINAL VERSION Abstract data types (ADT) Representa)on invariants (RI) HW2: Polynomial arithme)c (separate slides)

Stronger vs Weaker Specifica@ons Transi@on Rela@ons Which specifica)on is stronger? S1: /** *@spec.requires x > 0 *@return x **/ S2: /** *@return x if x > 0, -x if x <= 0 **/ A stronger specifica)on has a smaller transi)on rela)on

Stronger vs. Weaker Specifica@ons Transi@on Rela@ons Which specifica)on is stronger? S1: /** *@spec.requires x > 0 *@return x **/ Transi)on rela)ons (abbrev): (1, 1), (2, 2), (3, 3) S2: /** *@return x if x > 0, -x if x <= 0 **/ Transi)on rela)ons (abbrev): In domain of S2: (1, 1), (2, 2), (3, 3) S2 has a smaller transi)on rela)ons, so it is stronger than S1

Stronger vs. Weaker Specifica@ons Transi@on Rela@ons Which specifica)on is stronger? S1: /** *@spec.requires x > 0 *@return x **/ S2: /** *@return x if x > 0, -x if x <= 0 **/ Transi)on rela)ons (full): In domain of S2: (1, 1), (2, 2), (3, 3) (-1, 1), (-2, 2), (-3, 3) Transi)on rela)ons (full): (1, 1), (2, 2), (3, 3) (-1, 1), (-2, 2), (-3, 3) (-1, 0), (-2, 0), (-3, 0) (-1, null), (-2, null), (-3, null) Behavior for x<=0 is unspecified so could map to anything. S2 has a smaller transi)on rela)ons, so it is stronger than S1

Stronger vs. Weaker Specifica@ons Logical Formulas Which specifica)on is stronger? S1: /** *@spec.requires x > 0 *@return x **/ S2: /** *@return x if x > 0, -x if x <= 0 **/ A specifica)on is stronger than another specifica)on if its logical formula implies the logical formula of the weaker specifica)on

Stronger vs. Weaker Specifica@ons Logical Formulas Which specifica)on is stronger? S1: /** *@spec.requires x > 0 *@return x **/ Logical Formula: x > 0 => (Nothing is modified AND returns x) S2: /** *@return x if x > 0, -x if x <= 0 **/ Logical Formula: True => (Nothing is modified AND returns x If x >0 and x otherwise) S2 s logical formula implies S1 s logical formula, so S2 is stronger than S1

Abstract Data Types What is an ADT?

Abstract Data Types What is ADT? An ADT is a set of opera)ons Ex. RightTriangle create, getbase, getal)tude, getbo`omangle,

How to specify an ADT class TypeName { 1. overview 2. abstract fields 3. creators 4. observers 5. producers } 6. mutators

Mutable vs Immutable An immutable object is an object that cannot be altered once it is created. Mutable objects can be altered acer crea)on. Immutable ADTs don t have mutators Mutable ADTs rarely have producers

ADT Example: Circle Circle on the Cartesian coordinate plane.

Circle: Class Specifica@on What represents the abstract state of a Circle? How can we describe a circle? What are some proper)es of a circle we can determine? How can we implement this? What are some ways to break a circle?

Circle: Class Specifica@on What represents the abstract state of a Circle? Center Radius What are some proper)es of a circle we can determine? Circumference Area How can we implement this? #1: Center, radius #2: Center, edge (center, one point on outside) #3: Corners of diameter (two points on two sides of diameter) Break a circle : things may violate the defini)on of circle (nega)ve radius, etc)

Representa@on Invariants What are representa)on invariants? Why do we need representa)on invariants?

Representa@on Invariants What are representa)on invariants? Maps concrete representa$on of object boolean B Why do we need representa)on invariants? Indicates if an instance is well-formed or valid Defines the set of valid concrete values If the representa)on invariant is false/violated, the object is broken doesn t map to any abstract value For implementors/debuggers/maintainers of the abstrac$on: No object should ever violate the rep invariant

Ways to Avoid Representa@on Exposure 1. Exploit immutability 2. Make a copy (Both in and out) 3. Make an immutable copy

Circle Implementa@on 1 public class Circle1 { private Point center; private double rad; // Rep invariant: // } //...

Circle Implementa@on 1 public class Circle1 { private Point center; private double rad; // Rep invariant: // center!= null && rad > 0 } //...

Circle Implementa@on 2 public class Circle2 { private Point center; private Point edge; // Rep invariant: // } //...

Circle Implementa@on 2 public class Circle2 { private Point center; private Point edge; } // Rep invariant: // center!= null && // edge!= null && //!center.equals(edge) //...

Checking Rep Invariants Representa)on invariant should hold before and acer every public method Write and use checkrep() Call before and acer public methods Make use of Java s assert syntax! OK that it adds extra code Asserts won t be included on release builds Important for finding bugs If some checks are expensive, you can use a global boolean variable to condi)onally perform them

Takeaway for Rep Invariants

checkrep() Example with Asserts public class Circle1 { private Point center; private double rad; } private void checkrep() { } assert center!= null : This does not have a center ; assert radius > 0 : This circle has a negative radius ;

Circle Demo