Oriented Programming Terminology. Using classes and instances to design a system. Example Instance Diagram

Similar documents
Space War Class Diagram. Elements of OOP. How to design interactions between objects. Space War Class Diagram with Inheritance

6.001, Fall Semester, 1998 Lecture Notes, October 27 { Object Oriented Programming 2 An environment diagram illustrating +define foo +cons 1 2,, +set-

Detection of methods (or missing methods): Cleaning up some details of our implementation. Better Method Convention (1) --self

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Lexical vs. Dynamic Scope

CS61A Midterm 2 Review (v1.1)

User-defined Functions. Conditional Expressions in Scheme

STUDENT LESSON A1 Introduction to Object-Oriented Programming (OOP)

6.001 Notes: Section 8.1

A Brief Introduction to Scheme (II)

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

CS 314 Principles of Programming Languages. Lecture 16

Turtles All The Way Down

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme Basics > (butfirst '(help!)) ()

Intro. Scheme Basics. scm> 5 5. scm>

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

CS 360 Programming Languages Day 14 Closure Idioms

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

COP4020 Programming Assignment 1 - Spring 2011

Discussion 4. Data Abstraction and Sequences

John McCarthy IBM 704

Common LISP-Introduction

A brief tour of history

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

Programming Languages

Sample Final Exam Questions

Putting the fun in functional programming

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

So far, we have seen two different approaches for using computing to solve problems:

ormap, andmap, and filter

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

6.001 Notes: Section 15.1

6.001 Notes: Section 17.5

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

Inheritance. OOP: Inheritance 1

bindings (review) Topic 18 Environment Model of Evaluation bindings (review) frames (review) frames (review) frames (review) x: 10 y: #f x: 10

Time : 3 hours. Full Marks : 75. Own words as far as practicable. The questions are of equal value. Answer any five questions.

2. Classes & Inheritance. Classes. Classes. Éric Tanter Objects and Design in Smalltalk. How do you create objects?

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

FP Foundations, Scheme

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

CS61A Notes Disc 11: Streams Streaming Along

Organization of Programming Languages CS3200/5200N. Lecture 11

Berkeley Scheme s OOP

What is an algorithm?

Simplifying Logical Formulas

CS 11 python track: lecture 4

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

Data abstraction, revisited

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Principles of Programming Languages

Scheme-Style Macros: Patterns and Lexical Scope

Functional Programming. Big Picture. Design of Programming Languages

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

CS450 - Structure of Higher Level Languages

CS 11 java track: lecture 3

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

CS 314 Principles of Programming Languages

Lecture 4. Part 1. More on "First-Class" Procedures

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science. Issued: Wed. 26 April 2017 Due: Wed.

Programming Systems in Artificial Intelligence Functional Programming

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

CS558 Programming Languages

Mid-Term 2 Grades

University of Massachusetts Lowell

Mobile Computing Professor Pushpedra Singh Indraprasth Institute of Information Technology Delhi Activity Logging Lecture 16

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Programming Languages. Function-Closure Idioms. Adapted from Dan Grossman's PL class, U. of Washington


Scheme: Strings Scheme: I/O

Object Oriented Programming: Based on slides from Skrien Chapter 2

Inheritance and Polymorphism

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Improving our Simple Cache

Func%onal Programming in Scheme and Lisp


Scheme Quick Reference

Documentation for LISP in BASIC

Discussion 11. Streams

Func%onal Programming in Scheme and Lisp

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Functional Programming. Pure Functional Programming

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Streams and Evalutation Strategies

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

Administrivia. Simple data types

Transcription:

Object- Oriented Programming Terminology Class Diagram Instance Diagram Class: specifies the common behavior of entities in scheme, a "maker" procedure Instance: A particular object or entity of a given class in scheme, an instance is a message-handling procedure made by the maker procedure class private state public messages PAIR x: y: CAR CDR PAIR? SET-CAR! SET-CDR! foo PAIR x: y: instance of pair PAIR x: y: instance of pair 2 (define (cons x y) (λ (msg)...)) (define foo (cons (cons 2))) /25 2/25 Using classes and instances to design a system Suppose we want to build a star wars simulator I can start by thinking about what kinds of objects do I want (what classes, their state information, and their interfaces) ships planets other objects I can then extend to thinking about what particular instances of objects are useful Millenium Falcon Enterprise Earth A Space- Ship Object (define (make-ship position velocity num-torps) (define (move) (set! position (add-vect position...))) (define (fire-torp) (cond ((> num-torps 0)...) (else 'FAIL))) (lambda (msg) (cond ((eq? msg ') position) ((eq? msg ') velocity) ((eq? msg ') (move)) ((eq? msg ') (fire-torp)) (else (error "ship can't" msg))))) /25 4/25 Space- Ship Class Example Instance Diagram (define enterprise (make-ship (make-vect 0 0) (make-vect 5 0) )) (define war-bird (make-ship (make-vect -0 0) (make-vect 0 0) 0)) enterprise war-bird pos: (vec -0 0) vel: (vec 0 0) 0 pos: (vec 0 0) vel: (vec 5 0) 5/25 6/25

Example Environment Diagram (define enterprise (make-ship (make-vect 0 0) (make-vect 5 0) )) (enterprise ) ==> DONE (enterprise ) ==> (vect 5 0) Some Extensions to our World Add a PLANET class to our world Add predicate messages so we can check type of objects GE 2 From internal definitions enterprise: (vec050) 0) (vec 5 0) move: fire-torp: Add display handler to our system Draws objects on a screen Can be implemented as a procedure (e.g. draw) -- not everything has to be an object! Add ' message to classes so objects will display themselves upon request (by calling draw procedure) par: msg body: (cond ) par: body: (set! position ) 7/25 8/25 Space- Ship Class Planet Implementation? PLANET PLANET? (define (make-planet position) (lambda (msg) (cond ((eq? msg PLANET?) #T) ((eq? msg ) position) ((eq? msg ) (draw...)) (else (error "planet can't" msg))))) 9/25 0/25 Further Extensions to our World Class Diagram Animate our World! Add a clock that moves time forward in the universe Keep track of things that can move (the *universe*) Clock sends ACTIVATE message to objects to have them update their state Add TORPEDO class to system TORPEDO TORPEDO?? PLANET PLANET? /25 2/25 2

Coordinating with a clock CLOCK The-time: callbacks: CLOCK? NAME THE-TIME TICK ADD-CALLBACK CALLBACK Object: message: Data: ACTIVATE object State info methods Sends object message and data /25 The Universe and Time (define (make-clock. args) (let ((the-time 0) (callbacks '())) (lambda (message) (case message ((CLOCK?) (lambda (self) #t)) ((NAME) (lambda (self) name)) ((THE-TIME) (lambda (self) the-time)) ((TICK) (lambda (self) (map (lambda (x) (ask x 'activate)) callbacks) (set! the-time (+ the-time )))) ((ADD-CALLBACK) (lambda (self cb) (set! callbacks (cons cb callbacks)) 'added)) 4/25 Controlling the clock ;; Clock callbacks ;; ;; A callback is an object that stores a target object, ;; message, and arguments. When activated, it sends the target ;; object the message. It can be thought of as a button that ;; executes an action at every tick of the clock. (define (make-clock-callback name object msg. data) (lambda (message) (case message ((CLOCK-CALLBACK?) (lambda (self) #t)) ((NAME) (lambda (self) name)) ((OBJECT) (lambda (self) object)) ((MESSAGE) (lambda (self) msg)) ((ACTIVATE) (lambda (self) (apply-method object object msg data))) 5/25 Implementations for our Extended World (define (make-ship position velocity num-torps) (define (move) (set! position (add-vect position...))) (define (fire-torp) (cond ((> num-torps 0) (set! num-torps (- num-torps )) (let ((torp (make-torpedo...)) (add-to-universe torp)))) (define (explode ship) (display "Ouch. That hurt.")) (ask clock 'ADD-CALLBACK (make-clock-callback moveit me )) (define (me msg. args) (cond ((eq? msg?) #T)... ((eq? msg ) (fire-torp)) ((eq? msg ) (explode (car args))) (else (error "ship can't" msg)))) ME) 6/25 Torpedo Implementation (define (make-torpedo position velocity) (define (explode torp) (display torpedo goes off! ) (remove-from-universe torp)) (define (move) (set! position...)) (ask clock 'ADD-CALLBACK (make-clock-callback moveit me )) (define (me msg. args) (cond ((eq? msg TORPEDO?) #T) ((eq? msg ) position) ((eq? msg ) velocity) ((eq? msg ) (move)) ((eq? msg ) (explode (car args))) ((eq? msg ) (draw...)) (else (error No method msg)))) Running the Simulation ;; Build some things (define earth (make-planet (make-vect 0 0))) (define enterprise (make-ship (make-vect 0 0) (make-vect 5 0) )) (define war-bird (make-ship (make-vect -0 0) (make-vect 0 0) 0)) ;; Start simulation (run-clock 00) ME) 7/25 8/25

Summary Introduced a new programming style: Object-oriented vs. Procedural Uses simulations, complex systems,... Object-Oriented Modeling Language independent! Class template for state and behavior Instances specific objects with their own identities OOPS Using objects to structure systems Behaviors of object oriented systems Designing object oriented systems Focus initially on conceptual plans Eventually show a Scheme implementation Next time: powerful ideas of inheritance and delegation 9/25 20/25 Elements of OOP Example: bank accounts Class: Behavior of accounts in general Object Smart data structure Instances: My account versus yours Set of state variables Set of methods for manipulating state variables Class: Specifies the common behavior of entities Focus here during design Instance: A particular object or entity of a given class Focus here during simulation Space War Class Diagram TORPEDO target: proximity-fuse: TORPEDO?? PLANET PLANET? Ships and torpedoes have some behavior that is the same is there are way to capture this commonality? 2/25 22/25 Space War Class Diagram with Inheritance is-a MOBILE-THING MOBILE-THING? is-a class is a specialization or sub-class of the MOBILE-THING class is-a MOBILE-THING inherits the state and behavior of MOBILE-THING MOBILE-THING class is a super-class of the and TORPEDO classes How to design interactions between objects Focus on classes objects Relationships between classes Kinds of interactions that need to be supported between instances of classes For now, assume the following interface to an object: (ask <object> <method> <arguments>) TORPEDO target: proximity-fuse: TORPEDO? has-a target? 2/25 24/25 4

(define person- (make-person Fred Jones)) (define professor- (make-professor sam smith)) (ask person- (hello there)) hello there (ask person- ) (ask professor- say (hello there)) hello there Fred 25/25 26/25 (ask professor- ) (define ap- (make-arrogantprof Ned Infallible)) Professor Smith (ask professor- (the sky is blue)) Therefore, the sky is blue (ask ap- (nice weather we are having)) nice weather we are having, obviously A professor should delegate part of the lecture method to a person s say method. 27/25 28/25 (define ap- (make-arrogantprof Ned Infallible)) (ask ap- (nice weather we are having)) Therefore, nice weather we are having (define ap- (make-arrogantprof Ned Infallible)) (ask ap- (nice weather we are having)) Therefore, nice weather we are having, obviously 29/25 0/25 5

(define stud (make-student bert sesame)) (ask stud (I do not understand)) Excuse me, but I do not understand /25 2/25 (ask stud question ap- (why does this code work)) this should be obvious to you (ask professor- question ap- (why does this code work)) Why are you asking me about why does this code work I thought you published a paper on that topic /25 4/25 (ask stud?) #t (ask professor-?) #t Lessons from our simple class hierarchy tagging of instances specifying class hierarchies and ensuring that instances creating superclass instances inheriting of methods from class hierarchies delegation of methods to other instances within a class hierarchy?? 5/25 6/25 6