CSC151 2007S, Class 49: Object Basics Admin: * EC for the Alumna Scholar talk tonight. * Are there questions on the exam? * We'll start class by talking about homework 16. * For tomorrow, you may want to review the reading for today. (Particularly if you didn't do it.) Overview: * The N paradigms * Representing Compound Values. * Introduction to Objects. * Procedures as Objects. * Adding State. /Exam Questions/ //Sorting// (define grades (list (list "Sam" 60 60 90 100 60 80) (list "Sammy" 95 95 95 95 90 80) (list "Samwise" 100 0 0 0 0 0) (list "Sammer" 60 70 80 90 100 110))) (define points (list (cons 1 20) (cons 2 10) (cons 2 5) (cons 1 3) (cons 5 10))) Can we really write make-filter without a lambda? * I'll try after class * If SamR can't do it in fifteen minutes or less, everyone gets credit (I think) How concise is concise enough * I'll post the number of words in my solutions /Homework 16 - Selection Sort/ index-of-smallest * Two strategies: * Direct recursion * Assume we get the index of smallest element in "the rest of the vector" * If the thing in pos is smaller, return ops * Otherwise return the index of smallest element in rest * Helper - keeps track of something swap * Straightforward selection-sort! * Use a kernel that recurses over the positions --- /Object-Oriented Programming/ * Computer science is the study of algorithms and data * Two key questions: * How do I think about and represent algorithms? * How do I think about and represent data? * History: Multiple "paradigms" * First: Imperative (aka Procedural) * Algorithm is a sequence of steps, each of which does computation or reads/writes values * Traditional recipe * Alternate: Functional * Algorithms as function definitions and applications * Order of operations is more implicit (* (+ 3 4) (- 1 1) (sqrt 23)) * Smart implementations can do less work * Tends to mimic mathematical approach to algorithm design * Alternate (popular): Object-oriented * Model everything in the program as a "thing"/"object" * Each object has some properties * Each object has some capabilities * Grinnell believes you need to learn all three paradigms early in your career * Full blown object-oriented discussed in 152 Motivating example: Representing a library book * How do you represent a book? * Issue one: What information do you store? * Library of Congress number - String * Title - String * Author - (list of strings) * Number of pages - Integer * Subject matters - (list of strings) * Issue two: How do you group that information together? * List '("Q76.2.23" "Scheme Scarified" ("Emily Jacobson" "Jacob Emilyson") 2386 (...)) * How do you find the list of authors? (list-ref book 2) (caddr book) * Vector * File * Association list (('loc "QA76.2.23") ('title "Scheme Scarified for Scheming Students") ('author "Emily Jacobson") ('number-of-pages 2386) ('subject-matters "Sentience of computers" "Maliciousness of computers" "Lack of sentience of CS professors" "misspelling")) Goal of OOP: If you change the representation, the program doesn't break