Class 26: Objectifying Objects


Published on

Making a better counter
Object-oriented programming
Real databases

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Class 26: Objectifying Objects

    1. 1. Class 26:Objectifying Objects cs1120 Fall 2011 David Evans 24 October 2011
    2. 2. Thanks for the comments!Subject: Re: The book is now available for studentsDate: Thu, 20 Oct 2011 22:08:17 -0400From: Stephen Solomon <>To: evans@virginia.eduCC: Andrew Bender <>, Dev Purkayastha <>Dave,… Most of this is pure gold, and will be incredibly helpful to us aswe continue development of the Reader. Its greatly motivatingto see so many of our ideas validated by your students.Stephen 2
    3. 3. PlanPS6, PS7, Rest of the Class / cs2110ObjectsPS5 vs. “Real” Databases Quiz 3 is Wednesday: Course book through end of Chapter 10, The Information through end of Chapter 8 Note: we haven’t given up on mlist-reverse! …but will delay it (and do it in Scheme and Python) next class
    4. 4. Remaining Problem Sets PS6: Programming with Objects Python PS7: Implementing Interpreters Plan J: (Java) Plan W: Plan X: PS8: Static Types Something Else(Interpreter in Java) Build a Web Application (mostly using (mostly using Python) English or Video) PS9
    5. 5. Which Plan to PickIf you indicated Computer Science major or potential major on your registration survey: You must pick Plan J or provide a convincing reason why you aren’t picking Plan J If you do Plan J, you will satisfy the prerequisite to take cs2110 (Software Development Methods) in the Spring, and are encouraged to take cs2110 this Spring. (You will actually be much better prepared for cs2110 than the students who took cs1110.) cs2110: MWF 11-11:50, Rice 130Otherwise, choose any plan (more information later). 5
    6. 6. from Class 22: nextx(define x 0)(define (nextx) (set! x (+ x 1)) x)> (nextx)1> (set! x 23)> (next x)24
    7. 7. Can we make a better counter?The place that keeps track of the count should be part of the counter, not part of the global environment Can have more than one counter Counter state is encapsulated: can only be modified by counter procedure
    8. 8. Stateful Application Rule:To apply a constructed procedure:1. Construct a new environment, whose parent is the environment of the applied procedure.2. For each procedure parameter, create a place in the frame of the new environment with the name of the parameter. Evaluate each operand expression in the environment or the application and initialize the value in each place to the value of the corresponding operand expression.3. Evaluate the body of the procedure in the newly created environment. The resulting value is the value of the application.
    9. 9. Making a Better Counter 9
    10. 10. A Better Counter(define (make-counter) ((lambda (count) (lambda () (set! count (+ 1 count)) count)) 0))
    11. 11. Sweeter Version (define (make-counter) (let ((count 0)) (lambda () (set! count (+ 1 count)) count)))This is easier to read (syntactic sugar), but means the same thing. The place forcount is created because of the application that is part of the let expression.
    12. 12. (let ((Name1 Expression1) (Name2 Expression2) ... (Namek Expressionk)) Expressionbody)is equivalent to Draw the environment after evaluating:((lambda (Name1 Name2 . . . Namek) > (define mycount (make-counter)) Expressionbody) > (mycount) Expression1 Expression2 . . . Expressionk) 1 > (mycount) 2 (define (make-counter) (let ((count 0)) (lambda () (set! count (+ 1 count)) count)))
    13. 13. (define (make-counter) ((lambda (count) (lambda () (set! count (+ 1 count)) count)) 0))> (define mycount (make-counter))> (mycount)1> (mycount)2> (mycount)3 13
    14. 14. global(define (make-counter) environment ((lambda (count) (lambda () (set! count (+ 1 count)) + : #<primitive:+> count)) 0)) make-counter: mycount:> (define mycount (make-counter))> (mycount) environment:1 parameters: ()> (mycount) 3 1 0 body: ((lambda …2 count : 2> (mycount)3 environment: parameters: () body: (lambda () (set! count …)
    15. 15. Versatile Counter (define (make-counter) ((lambda (count) (lambda () (set! count (+ 1 count)) count)) 0))How can we make a counter that can dothings other than just add 1?
    16. 16. An Even Sweeter Counter(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"))))))
    17. 17. Using Counter> (define bcounter (make-counter))> (bcounter next)> (bcounter next)> (bcounter next)> (bcounter how-many)3> (bcounter reset)> (bcounter how-many)0
    18. 18. ObjectsAn object packages:state (“instance variables”)procedures for manipulating and observing that state (“methods”) Why is this such a big deal?
    19. 19. Problem-Solving StrategiesPS1-PS4: Functional Programming Focused on procedures Break a problem into procedures that can be composedPS5: Imperative Programming Focused on data Design data for representing a problem and procedures for updating that dataPS6: “Object-Oriented Programming” Focused on objects that package state and procedures Solve problem by designing objects that model the problem Lots of problems in real (and imaginary) worlds can be thought of this way
    20. 20. Python! We can do imperative and object-oriented style programming using Scheme, but it was designed only for functional programming. Python is a programming language designed to provide support for functional, imperative, andWe will use Python for PS6 and PS7 object-oriented style programming. 20
    21. 21. Python Version class defines a new classclass Counter: def __init__(self): The __init__ method is special: it constructs a new object of the class. self.count = 0 self is the object we are creating (for __init__) def reset(self): or manipulating (for the other methods). self.count = 0 self.count = 0 (like (let ((count 0)) …) def current(self): In __init__: creates a new place named return self.count count as part of this object’s frame, and initializes its value to 0. def advance(self): self.count = self.count + 1 Python’s built-in support for objects should (soon) make this easier to read and understand than the Scheme object system.
    22. 22. PS5How are commercial databases different fromwhat you implemented for PS5? UVa’s Integrated Systems Project to convert all University information systems to use an Oracle database was originally budgeted for $58.2 Million (starting in 1999). Actual cost ended up over $100 Million.
    23. 23. Real DatabasesAtomic Transactions a transaction may involve many modifications to database tables, but the changes should only happen if the whole transaction happens (e.g., don’t charge the credit card unless the order is sent to the shipping dept)Security limit read/write access to tables, entries and fieldsStorage efficiently store data on disk, backup mechanismsScale support really big data tables efficiently
    24. 24. Shannon’s Sketch (The Information, p. 232) Library of Congress: < 1014 = 100 TB Library of Congress (2009): 74 TB (publicly-available digital) Scanning: 5 PB/year 1 Petabyte = 1000 Terabytes = 1015 bytes < 105: Genetic constitution on human Note: this was 1949, DNA discovered in 1953! Human genome is 3B base pairs: 6 109 bits but much less information (1.5% coding ~ 107) 24
    25. 25. Big Databases TodayInternal Revenue Service 150 TerabytesChoicePoint 250 TBWal-Mart 1 Petabyte = 1000 Terabytes > 500 Terabytes (2004) = 1015 bytesYahoo! (2008) 2 Petabytes Analyze behavior of 500 M web Lots more information to be collected: visitors per month telephone calls in one year ~ 20National Security Agency Exabytes trillions of call recordsWorld Data Center for Climate 1 Exabyte = 1000 Petabytes = 1018 bytes 6 Petabytes
    26. 26. How much work? table-select is in (n) where n is the number of entries in the tableWould your table-select work for Wal-Mart? If 1M entry table takes 1s, how long would it take Wal-Mart to select from >500TB ~ 2 Trillion Entries? 2 000 000s ~ 23 days How do expensive databases perform table-select so much faster? Indexing is the key! See Section 8.2.3
    27. 27. ChargeQuiz 3 WednesdayPS6 will be posted by tomorrowWednesday: Python, Object-Oriented ProgrammingRegister for cs2110 for the Spring cs1120 Plan J satisfies the prerequisite