David Evans CS150: Computer Science University of Virginia Computer Science Lecture 23: Programming with Objects.

Slides:



Advertisements
Similar presentations
Complex Numbers Any number in form a+bi, where a and b are real numbers and i is imaginary. What is an imaginary number?
Advertisements

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 26: Proving Uncomputability Visualization.
Complex Numbers.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance.
Lecture 24: Gödel’s Proof CS150: Computer Science
5.4 Complex Numbers Until now, you have always been told that you can’t take the square root of a negative number. If you use imaginary units, you can!
+ Using Properties of Exponents EQ: How do we use properties of exponents? M2 Unit 5a: Day 1 Wednesday, October 07, 2015.
Fraction and Mixed Number Review (Add & Subtract).
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
2-6 Multiplying Rational Numbers Objective: To multiply rational numbers.
Section 8.8.  In this lesson you will learn to add, subtract, multiply, and divide rational expressions. In the previous lesson you combined a rational.
 Multiply rational expressions.  Use the same properties to multiply and divide rational expressions as you would with numerical fractions.
Definition: two fractions with the same value How do you find them? Multiply or divide by a “Giant One”. Example: x = One reason to use them: to add and.
Adding & Subtracting Whole Number and Fractions
5.4 Complex Numbers. Let’s see… Can you find the square root of a number? A. E.D. C.B.
I can find fractions of whole numbers.. Pop Quiz Just kidding…not today. Today we will be discussing ways to find fractions of whole numbers. For example.
Introduction to Complex Numbers Adding, Subtracting, Multiplying And Dividing Complex Numbers SPI Describe any number in the complex number system.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 20: Objects I invented the term Object-
Measurement Multiplying Fractions by Whole Numbers.
Class 25: Python, Objects, Bombs, and Inheritance University of Virginia cs1120 David Evans.
Unit 2: Integers Unit Review. Multiplying Integers The product of two integers with the same sign is a positive. Eg: (+6) x (+4) = +24; (-18) x (-3) =
4-8 Complex Numbers Today’s Objective: I can compute with complex numbers.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.
Complex Numbers warm up 4 Solve the following Complex Numbers Any number in form a+bi, where a and b are real numbers and i is imaginary. What is an.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 22: Objects I invented the term Object-
Complex Number System Reals Rationals (fractions, decimals) Integers (…, -1, -2, 0, 1, 2, …) Whole (0, 1, 2, …) Natural (1, 2, …) Irrationals.
6.1 Properties of Exponents Use properties of exponents Use negative and zero as an exponent EQ: What are the general rules involving properties of exponents?
Chapter 4 Section 8 Complex Numbers Objective: I will be able to identify, graph, and perform operations with complex numbers I will be able to find complex.
Same Signs Different Signs 1) =+7 Objective- To solve problems involving operations with integers. Combining.
Complex Number System Reals Rationals (fractions, decimals) Integers (…, -1, -2, 0, 1, 2, …) Whole (0, 1, 2, …) Natural (1, 2, …) Irrationals.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
Chapter 4.6 Complex Numbers. Imaginary Numbers The expression does not have a real solution because squaring a number cannot result in a negative answer.
Imaginary Numbers Review Imaginary Numbers Quadratic Forms Converting Standard Form.
Improper Fractions and Mixed Number.  An improper fraction is a fraction in which the numerator is larger than the denominator. Example: 7/3 The numerator.
Introduction to Complex Numbers Adding, Subtracting, Multiplying And Dividing Complex Numbers SPI Describe any number in the complex number system.
2.3 Adding and Subtracting Fractions. How do I add/subtract fractions?
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 21: Inheritance.
SOL Warm Up 1) C 2) B 3) (4x + y) (2x – 5y) 4) x = 7 ½ and x = -1/2 Answers.
Section 7.1 Rational Exponents and Radicals.
Multiplying Fractions by Whole Numbers
Multiplication and Division of Powers
Multiplying Fractions by Whole Numbers
Activator When I take five and add six, I get eleven, but when I take six and add seven, I get one. Who/What am I?
When the denominators are the same,
Applying GCF and LCM to Fraction Operations
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Class 22: Inheritance CS150: Computer Science University of Virginia
Fractions: Adding and Subtracting Like Denominators
Complex Numbers.
Lecture 21: Inheritance CS200: Computer Science University of Virginia
Lesson How do you add and subtract fractions?
Complex Numbers Any number in form a+bi, where a and b are real numbers and i is imaginary. What is an imaginary number?
9-5 Complex Numbers.
Fractions: Adding and Subtracting Like Denominators
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Complex numbers Math 3 Honors.
Section 4.6 Complex Numbers
Lecture 26: The Metacircular Evaluator Eval Apply
Lesson 2.4 Complex Numbers
Homework Help.
Fractions - Vocabulary
Multiplying fraction and whole number
Lecture 3: Rules of Evaluation CS200: Computer Science
Fractions Learn to love them!!!.
Introduction to Complex Numbers
If an equation contains fractions, it may help to multiply both sides of the equation by the least common denominator (LCD) to clear the fractions before.
Changing Data: (Continued)
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 23: Programming with Objects

2 Reminder Start thinking of ideas of PS9 and discussing them on the forum –You can also vote in the “should we have a quiz Monday” poll

3 Lecture 23: Programming with Objects Problem-Solving Strategies PS1-PS4: Functional Programming –Focused on procedures –Break a problem into procedures that can be combined to solve it PS5: Imperative Programming –Focused on data –Design data for representing a problem and procedures for updating that data

4 Lecture 23: Programming with Objects Problem-Solving Strategies PS6: “Object-Oriented Programming” –Focused on objects: package procedures and state –Model a problem by dividing it into objects –Lots of problems in real (and imaginary) worlds can be thought of this way

5 Lecture 23: Programming with Objects Counter Object (define (make-counter) (let ((count 0)) (lambda (message) (cond ((eq? message ’reset!) (set! count 0)) ((eq? message ’next!) (set! count (+ 1 count))) ((eq? message ’current) count) (else (error "Unrecognized message")))))) Instance variable Methods

6 Lecture 23: Programming with Objects Defining ask > (define bcounter (make-counter)) > (ask bcounter ' current) 0 > (ask bcounter 'next) > (ask bcounter 'current) 1 (ask Object Method) (define (ask object message) (object message))

7 Lecture 23: Programming with Objects Inheritance

8 Lecture 23: Programming with Objects There are many kinds of numbers… Whole Numbers (0, 1, 2, …) Integers (-23, 73, 0, …) Fractions (1/2, 7/8, …) Floating Point (2.3, , ) But they can’t all do the same things –We can get the denominator of a fraction, but not of an integer

9 Lecture 23: Programming with Objects make-fraction (define make-fraction (lambda (numerator denominator) (lambda (message) (cond ((eq? message 'value) (lambda (self) (/ numerator denominator)) ((eq? message 'add) (lambda (self other) (+ (ask self 'value) (ask other 'value))) ((eq? message ‘get-numerator) (lambda (self) numerator)) ((eq? message ‘get-denominator) (lambda (self) denominator)) ))))) Same as in make-number Note: our add method evaluates to a number, not a fraction object (which would be better).

10 Lecture 23: Programming with Objects Why is redefining add a bad thing? Cut-and-paste is easy but… There could be lots of number methods (subtract, multiply, print, etc.) Making the code bigger makes it harder to understand If we fix a problem in the number add method, we have to remember to fix the copy in make-fraction also (and real, complex, float, etc.)

11 Lecture 23: Programming with Objects make-fraction (define (make-fraction numer denom) (let ((super (make-number #f))) (lambda (message) (cond ((eq? message 'value) (lambda (self) (/ numer denom))) ((eq? message 'get-denominator) (lambda (self) denom)) ((eq? message 'get-numerator) (lambda (self) numer)) (else (super message))))))

12 Lecture 23: Programming with Objects Making Subobjects (define (make-fraction numer denom) (make-subobject (make-number #f))) (lambda (message) (cond ((eq? message 'value) (lambda (self) (/ numer denom))) ((eq? message 'get-denominator) (lambda (self) denom)) ((eq? message 'get-numerator) (lambda (self) numer)) (else #f)))))

13 Lecture 23: Programming with Objects Implementing make-subobject (define (make-subobject super imp) (lambda (message) (if (eq? message ’super) (lambda (self) super) (let ((method (imp message))) (if method method (super message))))))

14 Lecture 23: Programming with Objects Using Fractions > (define half (make-fraction 1 2)) > (ask half 'value) 1/2 > (ask half 'get-denominator) 2 > (ask half 'add (make-number 1)) 3/2 > (ask half 'add half) 1

15 Lecture 23: Programming with Objects > (trace ask) > (trace eq?) > (ask half 'add half) |(ask # add # ) | (eq? add value) | #f | (eq? add get-denominator) | #f | (eq? add get-numerator) | #f | (eq? add value) | #f | (eq? add add) | #t | (ask # value) | |(eq? value value) | |#t | 1/2 | (ask # value) | |(eq? value value) | |#t | 1/2 |1 1

16 Lecture 23: Programming with Objects > (trace ask) > (trace eq?) > (ask half 'add half) |(ask # add # ) | (eq? add value) | #f | (eq? add get-denominator) | #f | (eq? add get-numerator) | #f | (eq? add value) | #f | (eq? add add) | #t | (ask # value) | |(eq? value value) | |#t | 1/2 | (ask # value) | |(eq? value value) | |#t | 1/2 |1 1 make-number make-fraction

17 Lecture 23: Programming with Objects Inheritance Inheritance is using the definition of one class to make another class make-fraction uses make-number to inherit the behaviors of number

18 Lecture 23: Programming with Objects Speaking about Inheritance Fraction inherits from Number. Fraction is a subclass of Number. The superclass of Fraction is Number. Number Fraction

19 Lecture 23: Programming with Objects PS6 Make an adventure game programming with objects Many objects in our game have similar properties and behaviors, so we use inheritance.

20 Lecture 23: Programming with Objects PS6 Classes sim-object physical-object place mobile-object thing person student police-officer make-class is the procedure for constructing objects in the class class student inherits from person which inherits from mobile-object which inherits from physical-object which inherits from sim-object.

21 Lecture 23: Programming with Objects PS6 Objects object physical-object place mobile-object thing person student police-officer Cabal Hall Recursa Alyssa P. Hacker (make-place name) evaluates to an object that is an instance of the class place.

22 Lecture 23: Programming with Objects Are there class hierarchies like this in the “real world” or just in fictional worlds like Charlottansville?

23 Lecture 23: Programming with Objects Charge Monday: –Quiz on GEB reading (depending on poll) –History of Object-Oriented Programming PS6 due Friday Start thinking about PS9 project ideas –Use the forum to find teammates and propose ideas