CS61A Lecture 15 2011-07-14 Colleen Lewis. Clicker poll How do you feel about the “talk to your neighbor” stuff during lecture A)Like it B)It is okay.

Slides:



Advertisements
Similar presentations
Being ready is important. When adults tell me to get ready, it is my job to do it. Being ready is my responsibility. Im ready!
Advertisements

Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
Inheritance Writing and using Classes effectively.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Chapter 5 Part 1 COSI 11a Prepared by Ross Shaull.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 8: Recursion.
OOP! POO! Spelled backwards. Intro to OOP What is OOP?  Stands for Object Oriented Programming  Create different types of objects which can do multiple.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 15: Procedures as Arguments.
Unit 1 Sections 1-6 Sentence Frames
CSCI-383 Object-Oriented Programming & Design Lecture 15.
PHP Workshop ‹#› PHP Classes and Object Orientation.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Kids Computer Club House
TELEPHONE ENGLISH.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
© 2011 wheresjenny.com Oh, we love drama in our neighbourhood!!
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
Welcome! Living the Golden Rule; Providing Excellent “CUSTO-HUMAN” SERVICE.
Safety! Jessica Brown. What Is the Point of Having an Online Space? It needs to be Safe Clean And Efficient.
Cues to Teach a Child to Express Angry Feelings
Sometimes we can tell how people are feeling by looking at them. How are they feeling?
CS61A Lecture Colleen Lewis. Clicker Query What else are you doing this summer? A)Taking another class B)Working C)Taking another class and.
© A Smith Safer Internet Day What is the Internet? When do we use it? Why do we have Safer Internet Day?
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
ClassesPHPMay-2007 : [‹#›] PHP Classes and Object Orientation.
ECE 122 March 24. Motivation I am tasked to write two classes, one for cat, one for sheep. That is easy. I roll up my sleeve and get it done!
ECE122 Feb. 22, Any question on Vehicle sample code?
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation.
© 2015 albert-learning.com Inviting Neighbor for Dinner Inviting neighbors for dinner.
A : Hello! I’m Jinsu. B : Hi! I’m Dami. A : What’s this? B : It’s an eraser.
What is gossip? When people spread rumors about another person it is called gossip. Gossip is talking about something that is not your problem.

Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Creating a GUI Class An example of class design using inheritance and interfaces.
Information was taken from POSITIVE DISCIPLINE by Nelsen, Lott, and Glenn.
Reading every day words (including some tricky ones)
CS61A Lecture Colleen Lewis. Clicker poll Where do you think you’re learning the most? I assume you’ll learn the most in lab & disc (so.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
CS61A Lecture Colleen Lewis 1. Clicker poll Are you allowed to talk about the midterm on piazza or in public yet? A)Yes B)No 2.
CS61A Lecture Colleen Lewis. Clicker poll Do you feel comfortable posting questions to piazza? A)Yes with my classmates and instructors.
CS61A Lecture Colleen Lewis. Clicker Test Are you coming to the potluck on Thursday on the 4 th floor of Soda from 6:30pm-9:00? A)Yes B)Maybe.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CS61A Lecture Colleen Lewis. Clicker poll How do you think the midterm compared to previous midterms online? A)Harder B)A little harder.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
My Thinking Side.
Hello. Hello. May I speak to…? This is … speaking. Learn to say 怎样打电话.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Generics, Lambdas, Reflections
OOP’S Concepts in C#.Net
My Thinking Side.
Interfaces and Abstract Classes
This is a picture of me. I will be in
Review of Previous Lesson
Staying on topic By: Heather Byrge.
Crushes Sarah Curtiss And Colleen Gibbons
Lecture 6 - Recursion.
Presentation transcript:

CS61A Lecture Colleen Lewis

Clicker poll How do you feel about the “talk to your neighbor” stuff during lecture A)Like it B)It is okay – but I don’t like it much C)Don’t like it D)Hate it

REVIEW: define the animal class STk> (define animal1 (instantiate animal 'fred)) animal1 STk> (ask animal1 'age) 0 STk> (ask animal1 'eat) yum STk> (ask animal1 'name) fred Do you want: A) live coding B) Chalk C) PowerPoint

animal Solution (define-class (animal name) (instance-vars (age 0)) (method (eat) 'yum))

Inheritance Set another class as a parent and then use all of their methods!

dogs inherit from animals (& can call parent methods) (define-class (dog) (parent (animal 'doggy-name))) STk> (define dog1 (instantiate dog)) dog1 STk> (ask dog1 'eat) yum I don’t have an 'eat method. Let me ask my parent Can call methods of parent on instances of the child class

(define-class (dog) (parent (animal 'doggy-name))) STk> (define dog1 (instantiate dog)) dog1 STk> (ask dog1 'age) 0 I don’t have an 'age method. Let me ask my parent Can call automatically generated methods in the parent class with instances of the child class dogs inherit from animals (& can call parent’s automatically generated methods)

(define-class (dog) (parent (animal 'doggy-name))) STk> (define dog1 (instantiate dog)) dog1 STk> (ask dog1 'name) doggy-name I don’t have an ‘name method. Let me ask my parent dogs inherit from animals (& can call parent’s automatically generated methods for instantiation variables)

Children can not access parent’s instance-vars directly (define-class (dog) (parent (animal 'doggy-name)) (method (say-name) (se 'woof name))) BAD BAD BAD! This doesn’t work!

self A way to ask yourself to call methods

define-class (dog) (parent (animal 'doggy-name)) (method (say-name) (se 'woof (ask self 'name)))) I don’t have an ‘name method. Let me ask my parent You can ask self things

Excessively tricky case (define-class (tricky) (instance-vars (x 3)) (method (weird x) (* x (ask self 'x)))) (define trick (instantiate tricky)) STk> (ask trick 'weird 4) A) 9 B) 16 C) 12 D) Other

You can do recursion with methods (define-class (math-wiz) (method (factorial n) (if (< n 2) 1 (* n (ask self 'factorial (- n 1))))))

Overriding methods

The surfer1 class overrides the parent’s say method (define-class (person) (method (say sent) (se sent '!))) (define-class (surfer1) (parent (person)) (method (say sent) (se sent 'dude)))

Creating a person object (define-class (person) (method (say sent) (se sent '!))) STk> (define p1 (instantiate person)) p1 STk> (ask p1 'say '(happy birthday)) (happy birthday !)

Creating a surfer1 object (define-class (surfer1) (parent (person)) (method (say sent) (se sent 'dude))) STk> (define s1 (instantiate surfer1)) s1 STk> (ask s1 'say '(happy birthday)) (happy birthday dude) I want it to work more like the parent

usual Explicitly call the parent’s method

Call the usual method (the one you had overridden) (define-class (person) (method (say sent) (se sent '!))) (define-class (surfer2) (parent (person)) (method (say sent) (usual 'say (se sent 'dude)))) ???

(define-class (surfer2) (parent (person)) (method (say sent) (usual 'say (se sent 'dude)))) STk> (define s2 (instantiate surfer2)) s2 STk> (ask s2 'say '(happy birthday)) (happy birthday dude !) Call the usual method (the one you had overridden)

Would this have worked? (define-class (person) (method (say sent) (se sent '!))) (define-class (surfer2) (parent (person)) (method (say sent) (ask self 'say (se sent 'dude)))) A) Yes B) No C) Sometimes

Calling an overridden method in a parent class (define-class (person) (method (say sent) (se sent '!)) (method (meet someone) (ask self 'say (se 'hi someone)))) STk> (define p1 (instantiate person)) p1 STk> (ask p1 'meet 'eric) (hello eric !)

Calling an overridden method in a parent class (define-class (person) (method (say sent) (se sent '!)) (method (meet someone) (ask self 'say (se 'hi someone)))) STk> (define s2 (instantiate surfer2)) s2 STk> (ask s2 'meet 'kevin) A) (hello kevin dude) B) ERROR C) (hello kevin !) D)(hello kevin) E) (hello kevin dude !)

default-method Will run if there is no match to the message passed to ask

Writing a default-method (define-class (polite-person) (parent (person)) (default-method (se '(sorry I do not have a method named) message))) (define pp (instantiate polite-person)) STk> (ask pp 'whatz-up?) (sorry i do not have a method named whatz-up?) STk> (ask pp 'whatz-up? 'dude) (sorry i do not have a method named whatz-up?)

The doubler class (define-class (doubler) (method (say stuff) (se stuff stuff))) Class name Method name Method body Method argument variable

Creating objects & calling methods STk> (define d (instantiate doubler)) d STk> (ask d 'say '(how are you?)) (how are you? how are you?) Class name Creates an instance of a class Call a method On this instance of a class Call this method With this argument

instance-vars (define-class (counter) (instance-vars (count 0) ) (method (welcome) (se 'my 'count 'is count))) Create these variables for each new instance Instance variable name Initial value Could add another variable here. E.g. (x 3) Can be accessed

Initializing class-vars (define-class (beach-bum name) (class-vars (surfer-names '())) (initialize (set! surfer-names (se name surfer-names))) (method (say stuff) (se stuff 'dude))) class variables are shared with all instances of the class This is the FIRST initial value do this after you make sure all the class-vars exist

Rewriting a let as a lambda

Let review (define (sum-sq a b) (let ((a2 (* a a)) (b2 (* b b))) (+ a2 b2))) STk> (sum-sq 2 3) What does this return? A) 9 B) 10 C) 11 D) 12 E)13

Let review (define (sum-sq a b) (let ((a2 (* a a)) (b2 (* b b)) ) (+ a2 b2) )) (define (sum-sq a b) ( (lambda (a2 b2) (+ a2 b2)) (* a a) (* b b) )

Rewrite the let with lambda (define (funct x) (let ((a 3) (b 4) (c 6)) (+ a b c x))) (define (funct2 x) ((lambda (a b c) (+ a b c x)) 3 4 6))