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

Slides:



Advertisements
Similar presentations
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Advertisements

Identity and Equality Based on material by Michael Ernst, University of Washington.
Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.
Slides adapted from Alex Mariakakis, with material from Krysta Yousoufian, Mike Ernst, and Kellen Donohue Section 4: Graphs and Testing.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
try { Assert(Life. Real); Assert(Life
Slides adapted from Alex Mariakakis, with material from Krysta Yousoufian, Mike Ernst, Kellen Donohue Section 5: HW6 and Interfaces.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.
Abstraction Functions. Announcements Exam 1 on Tuesday March 3 rd Closed book/phone/laptop 2 cheat pages allowed (handwritten or typed) 1 double-sided.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
3.4 Area and Circumference 1 Circle A circle is a plane figure that consists of all points that lie the same distance from a fixed point. The fixed point.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Slides by Vinod Rathnam with material from Alex Mariakakis Kellen Donohue, David Mailhot, and Hal Perkins Midterm Review.
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Information and Computer Sciences University of Hawaii, Manoa
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Data Abstractions EECE 310: Software Engineering.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Polymorphism (generics) CSE 331 University of Washington.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Chapter 7 Queues Introduction Queue applications Implementations.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
SLIDES ADAPTED FROM ALEX MARIAKAKIS, WITH MATERIAL FROM KRYSTA YOUSOUFIAN, MIKE ERNST, KELLEN DONOHUE Section 5: HW6 and Interfaces Justin Bare and Deric.
Section 6: Interfaces, Midterm Slides by Vinod Rathnam and Geoffrey Liu (with material from Alex Mariakakis, Kellen Donohue, David Mailhot, and Hal Perkins)
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
9-Mar-16 Sparse arrays. 2 About sparse arrays A sparse array is simply an array most of whose entries are zero (or null, or some other default value)
Slides with material from Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section 3: HW4, ADTs, and more.
slides adapted from Marty Stepp and Hélène Martin
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
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.” The programmer.
Slides by Alex Mariakakis Section 5: Midterm Review.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst and David.
Section 4: Graphs and Testing
They both stop working when you open Windows!
Building Java Programs
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
List Representation - Array
Section 5: HW6 and Interfaces
Chapter 10: An Array Instance Variable
Section 5: HW6 and Interfaces
Building Java Programs
Section 6: Midterm Review
Lecture 7: Linked List Basics reading: 16.2
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Lecture 4: Data Abstraction CS201j: Engineering Software
The programmer returns with 12 loaves of bread.
Section 3 Graphs & Testing
Section 5: HW6 and Interfaces
Section 4: Graphs and Testing
Warmup ["hip","hip"] = Hip Hip Array! ???.
Section 5: HW6 and Interfaces
Recursive Objects Singly Linked Lists.
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
slides created by Marty Stepp
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

A programmer’s wife tells him, “Would you mind going to the store and picking up a loaf of bread. Also, if they have eggs, get a dozen.” The programmer returns with 12 loaves of bread.

Slides by Vinod Rathnam with material from Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section 3: HW4, ADTs, and more

 Announcements  HW3: due yesterday  HW4: due next Wednesday April 22nd  Polynomial arithmetic  Abstract data types (ADT)  Representation invariants (RI)  Abstraction Functions  Further information found in Calendar/info & docs/handouts link on website

 Problem 0: Write pseudocode algorithms for polynomial operations  Problem 1: Answer questions about RatNum  Problem 2: Implement RatTerm  Problem 3: Implement RatPoly  Problem 4: Implement RatPolyStack  Problem 5: Try out the calculator

 RatNum  ADT for a Rational Number  Has NaN  RatTerm  Single polynomial term  Coefficient (RatNum) & degree  RatPoly  Sum of RatTerms  RatPolyStack  Ordered collection of RatPolys

(5x 4 + 4x 3 - x 2 + 5)(3x 5 - 2x 3 + x – 5) +

5x 4 + 4x 3 - x 2 0x + 5 3x 5 0x 4 - 2x 3 0x 2 + x – 5 + (5x 4 + 4x 3 - x 2 + 5)(3x 5 - 2x 3 + x – 5) +

3x 5 0x 4 - 2x 3 0x 2 + x – 5 + (5x 4 + 4x 3 - x 2 + 5)(3x 5 - 2x 3 + x – 5) + 5x 4 + 4x 3 - x 2 0x + 5

3x 5 + 5x 4 - 2x 3 - x 2 + x + 0 3x 5 0x 4 - 2x 3 0x 2 + x – 5 + (5x 4 + 4x 3 - x 2 + 5)(3x 5 - 2x 3 + x – 5) + 5x 4 + 4x 3 - x 2 0x + 5

3x 5 0x 4 - 2x 3 0x 2 + x – 5 - (5x 4 + 4x 3 - x 2 + 5)(3x 5 - 2x 3 + x – 5) -

5x 4 + 4x 3 - x 2 0x + 5 (5x 4 + 4x 3 - x 2 + 5)(3x 5 - 2x 3 + x – 5) - 3x 5 0x 4 - 2x 3 0x 2 + x – 5 -

-3x 5 + 5x 4 + 6x 3 - x 2 - x x 4 + 4x 3 - x 2 0x + 5 (5x 4 + 4x 3 - x 2 + 5)(3x 5 - 2x 3 + x – 5) - 3x 5 0x 4 - 2x 3 0x 2 + x – 5 -

(4x 3 - x 2 + 5)(x – 5) *

4x 3 - x x – 5 * (4x 3 - x 2 + 5)(x – 5) *

-20x 3 + 5x 2 – 25 4x 3 - x x – 5 * (4x 3 - x 2 + 5)(x – 5) *

-20x 3 + 5x 2 – 25 4x 4 -x 3 + 5x 4x 3 - x x – 5 * (4x 3 - x 2 + 5)(x – 5) *

+ 4x 4 -21x 3 + 5x 2 + 5x x 3 + 5x 2 – 25 4x 4 -x 3 + 5x 4x 3 - x x – 5 * (4x 3 - x 2 + 5)(x – 5) *

(5x 6 + 4x 4 – x 3 + 5)(x 3 - 2x – 5) /

5x 6 + 4x 4 – x x 3 - 2x – 5 (5x 6 + 4x 4 – x 3 + 5)(x 3 - 2x – 5) /

5x x + 24 (5x 6 + 4x 4 – x 3 + 5)(x 3 - 2x – 5) /

5x x x x x 3 - 2x – 5 (5x 6 + 4x 4 – x 3 + 5)(x 3 - 2x – 5) /

Suppose we want to make a Line class that represents lines on the Cartesian plane. See ions.html ions.html for more.

/** * This class represents the mathematical concept of a line segment. * * A line is an immutable line segment on the 2D plane that has endpoints p1 * and p2 */ public class Line { … }

 Constrains an object’s internal state  Maps concrete representation of object to a boolean  If representation invariant is false/violated, the object is “broken” – doesn’t map to any abstract value

 Circle on the Cartesian coordinate plane.

What represents the abstract state of a Circle?  Center  Radius What are some properties of a circle we can determine?  Circumference  Area How can we implement this?  #1: Center, radius  #2: Center, edge  #3: Corners of diameter

public class Circle1 { private Point center; private double rad; // Rep invariant: // //... }

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

public class Circle2 { private Point center; private Point edge; // Rep invariant: // //... }

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

public class Circle3 { private Point corner1, corner2; // Rep invariant: // //... }

public class Circle3 { private Point corner1, corner2; // Rep invariant: // corner1 != null && // corner2 != null && // !corner1.equals(corner2) //... }

Representation invariant should hold before and after every public method  Write and use checkRep()  Call before and after 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

public class Circle1 { private Point center; private double rad; private void checkRep() throws RuntimeException { if (center == null) { throw new RuntimeException(“This does not have a center"); } if (radius <= 0) { throw new RuntimeException(“This circle has a negative radius”); }

public class Circle1 { private Point center; private double rad; private void checkRep() throws RuntimeException { assert center != null : “This does not have a center”; assert radius > 0 : “This circle has a negative radius”; } A lot neater!

 To enable asserts: Go to Run->Run Configurations…->Arguments tab-> input –ea in VM arguments section  Do this for every test file  Demo!

 Abstraction function: a mapping from internal state to abstract value  Abstract fields may not map directly to representation fields  Circle has radius but not necessarily private int radius;  Internal representation can be anything as long as it somehow encodes the abstract value  Representation Invariant excludes values for which the abstraction function has no meaning

public class Circle1 { private Point center; private double rad; // Abstraction function: // AF(this) = a circle c such that // c.center = // c.radius = // Rep invariant: // center != null && rad > 0 //... }

public class Circle1 { private Point center; private double rad; // Abstraction function: // AF(this) = a circle c such that // c.center = this.center // c.radius = this.rad // Rep invariant: // center != null && rad > 0 //... }

public class Circle2 { private Point center; private Point edge; // Abstraction function: // AF(this) = a circle c such that // c.center = // c.radius = // Rep invariant: // center != null && edge ! null && !center.equals(edge) //... }

public class Circle2 { private Point center; private Point edge; // Abstraction function: // AF(this) = a circle c such that // c.center = this.center // c.radius = sqrt((center.x-edge.x)^2 + (center.y-edge.y)^2) // Rep invariant: // center != null && edge ! null && !center.equals(edge) //... }

public class Circle3 { private Point corner1, corner2; // Abstraction function: // AF(this) = a circle c such that // c.center = // c.radius = // Rep invariant: // corner1 != null && corner2 != null && !corner1.equals(corner2) //... }

public class Circle3 { private Point corner1, corner2; // Abstraction function: // AF(this) = a circle c such that // c.center = // c.radius = (1/2)*sqrt((corner1.x- corner2.x)^2 + (corner1.y- corner2.y)^2) // Rep invariant: // corner1 != null && corner2 != null && !corner1.equals(corner2) //... }

public class NonNullStringList { // Abstraction function: // ?? // Rep invariant: // ?? public void add(String s) {... } public boolean remove(String s) {... } public String get(int i) {... } }

public class NonNullStringList { // Abstraction function: // Index i in arr contains the ith element in the list // Rep invariant: // RI = [0,count-1] != null private String[] arr; private int count; public void add(String s) {... } public boolean remove(String s) {... } public String get(int i) {... } } Problems?

public class NonNullStringList { // Abstraction function: // Value in the nth node after head contains the nth item in the list // Rep invariant: // RI = Head has size nodes after it, each whose value is non-null, no cycle in ListNodes public int size; public ListNode head; public void add(String s) {... } public boolean remove(String s) {... } public String get(int i) {... } }