1 / 19

Data Abstraction

Data Abstraction. CMSC 1150 Introduction to Computer Programming October 2, 2002. Roadmap. Recap: Compound expressions & Procedural Abstraction Data Abstraction Motivation: Compound Data Abstract Interface: Constructors and Selectors Concrete Implementation with define-struct

Download Presentation

Data Abstraction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Abstraction CMSC 1150 Introduction to Computer Programming October 2, 2002

  2. Roadmap • Recap: • Compound expressions & Procedural Abstraction • Data Abstraction • Motivation: Compound Data • Abstract Interface: Constructors and Selectors • Concrete Implementation with define-struct • Abstraction Barriers • Good Design: Data definitions and contracts • Summary

  3. Administrivia • Add yourself to the course mailing list: • http://mailman.cs.uchicago.edu 11500-1 • TA office hours: • Leandro Cortes: Thurs 2-4, Ry178 • Yu “Tony” Hu: Fri 2-4, Ry 178 • Dinoj Surendran: Wed 3-4, Fri 10-11am, Eckhart 006 • First homework: Due Monday • Dr Scheme note: “Language” set to R5RS

  4. Recap: Expressions and Procedures • Primitives: E.g. integers • Primitive operators: +,-,*,.. • Primitive expressions: (+ 1 2), (* 3 4) • Compound expressions: • (* (+ 1 2) (+5 6)) • Procedural abstraction: (define (square x) (* x x)) • Conditionals: if, cond • Issues: Substitution model, Evaluation order

  5. Compounding & Abstraction • Procedures and Expressions • Combine primitives • Create more complex, powerful expressions • Abstractions: • Procedures as black boxes, apply to different input • E.g. square procedure: • (define (area r) (* pi (square r))) • Doesn’t matter how implement square as long as it returns the square of the input.

  6. Compound Data & Abstraction • Similar issues arise for data • Represent things more complex than integers • Similar to building more complex functions • Objects have many facets • Abstract away from implementation • Any structure that behaves correctly

  7. Compound Data: Motivation • Real world objects often have many facets • Example: Specifying a point in a plane • (x,y) coordinate pair • Create one data structure that holds BOTH • In use, often want x- or y- coordinate • E.g. to compute distance from origin, slope of line defined by two points, etc • Need accessors for each facet

  8. Compound Data: Structures • Example: (x,y) coordinates • (make-posn x y) • Takes two numbers • Combines into a posn structure • posn can be input or output of procedure • E.g. (distance-to-0 (make-posn 5 0)) => 5 • (distance-to-0 (make-posn 3 4)) => 5

  9. Accessing Components • To compute distance-to-0 • Need x and y coordinate values • Two procedures: posn-x, posn-y • (posn-x (make-posn 1 3)) => 1 • (posn-y (make-posn 1 3)) => 3 • (define (distance-to-0 a-posn) • (sqrt (+ (square (posn-x a-posn)) • (+ (square (posn-y a-posn))))

  10. Abstraction • How do make-posn, posn-x, and posn-y work? • As users, we don’t need to know • Key: make-posn combines x and y • posn-x, posn-y return coordinates • Any implementation with this behavior is fine

  11. Interface • Define an interface between abstract use and concrete implementation of structure • Two types of procedures: • Constructors: e.g. make-posn • Selectors: e.g. posn-x, posn-y • Implement abstract data via concrete representation

  12. A Concrete Implementation • (define-struct posn (x y)) • Structure definition creates 3 procedures: • Constructor: make-posn • Selector: posn-x • Selector: posn-y • Discussion: Other structures

  13. Example: Operations on Points • (distance-to-0 a-posn) • (line-length a-posn1 a-posn2) • (line-slope a-posn1 a-posn2) • etc….

  14. Abstraction Barriers • Separate programs that use abstraction from those that implement abstraction Points in problem domain Distance-to-0 line-length line-slope Points as x,y coordinates make-posn posn-x posn-y Points as structures define-struct However structures are implemented

  15. Why Abstraction? • Can make programs much easier to maintain and modify • Consider alternate implementations • Limit changes to small # of interface procedures • But only successful if maintain abstraction

  16. Good Design: Data Definition • Problem: (make-posn ‘dog ‘cat) • Clearly wrong • Would blow up in distance-to-0, line-length, etc • Issue: Guarantee that given correct types of input to constructor, processing output of selectors should yield intended result • Solution: Data Definition • Establish agreement between programmers and users

  17. Data Definition • What is it? • Combination of Scheme and English specifying • How to use a class of structures • How to construct elements of this class • Example: posn • A posn is a structure (make-posn x y) • where x and y are numbers • Discussion: Other definitions

  18. Summary • Compound data & abstraction • Parallel to use of compound expresssions • Capture multiple facets of objects • Interface: Constructors and Selectors • Abstraction: Allow users of data to ignore implementation • Abstraction barriers • Concrete Implementation with define-struct • Data definitions: Establish covenant between users and programmers of data for behavior and input

  19. Next Time • Today: Finite compound data • Friday: Infinite compound data • Lists: constructors and selectors • Recursion

More Related