CSC152 2006S, Class 2: About Object-Oriented Programming Admin: * HW1 due * Assignments: * HW2: Course Basics * Read "An Introduction to Unix" * Read "Basics of Object-Oriented Problem Solving" (available by 5 pm) * Sam is sarcastic. Complain when necessary. * Missed question from day 1: Why are you here? * Might want to be an engineer and most engineers program (3-2) * I want to write videogames * Other career choice involves programming as part * * Wants to teach math, math teachers expected to teach programming (or are more hireable) * Programming can be fun, intellectually stimulating, etc. * Exercise a different part of your brain Overview: * Review: Subject of the class * Object-Oriented Problem Solving, Revisited * Our rational class * ADTs and Data Structures, Revisited /Review this is a class in .../ Close to what Sam said yesterday * Algorithms * Abstract Data Types and Data Structures * Object-Oriented Problem Solving * Java Other interesting answers * Oy, and other Jewish phrases * Thinking /Object-Oriented Programming/ * Programming in which the focus is on building objects * Object (trad.)- A physical thing that exists in the real world "thing" = "entity" * Object (programming) - Something that collects data and functionality - A group of variables and functions that use those variables * We call the variables "fields" or "attributes" * We call the capabilities (functions, procedures) "methods" ; "messages" * Why do programmers (and managers and designers and ...) like object-orientation so much? * Lots of programming is modeling, virtual objects simplify the modeling of real objects * Many modern programs involve GUIs, objects model the parts of GUIs (windows, buttons, menus, etc.) * Experience suggests that good oop leads to more reusable code * Encapsulation * Inheritance * Polymorphism Encapsulation * Idea of grouping methods and fields * Encapsulation protects the fields from outsiders; other objects (and other programmers) access fields only through methods * Problem: Given a name, find the telephone number * Representation: List of pairs, name/phone number, string (define csc152 (list (cons "Sam Rebelsky" "269-4410") (cons "Tony Leguia" "269-1111"))) * Sample use (phone "Sam Rebelsky" csc152) * (define phone (lambda (name phonebook) (cond ((null? phonebook) "411") ((string-equal? name (caar phonebook)) (cdar phonebook)) (else (phone name (cdr phonebook)))))) * Realization: Makes more sense to represent name as two values, first and last (define csc152 (list (list "Sam" "Rebelsky" "269-4410") (list "Tony" "Leguia" "269-1111"))) * Suppose we'd written (get-name phone-entry) and (get-number) and written phone in terms of that. The change to the data structure would require changes to those, but not to phone. * The fixed code is harder to accidentally break * Side note: The correct answer to "write phone" is (define phone (lambda (name phonebook) (assoc name phonebook))) How do we define objects? * Traditional solution: Classes - Classes provide templates for objects * List of field names and types title (string) authors (list of names) ... * Methods extract-the-text-on-page(page number) look-at-index(text-to-find) Inheritance: Defining new classes of objects based on old classes of objects * Consider library books * Every library book is a book - Automatically make every library book have the fields and methods of a regular book * Library books also have a call-number field and a "checked" status checkIn, checkOut, lose methods Polymorphism: If we write a method/function that uses one kind of object, it can use any related object * Saves programmer from recoding similar things Classes in the concrete: Rational numbers * Assume language lacks rational numbers * 3/4 is interpreted as "3 divided by 4" and not "the rational number three-fourths" * What methods (procedures, functions) would you want in your language to work with rational numbers? * Arithmetic: add, subtract, multiply divide * Scheme notation (+ r1 r2) (- r1 r2) (* r1 r2) (/ r1 r2) * Java notation r1.add(r2), r1.subract(r2), ... * Invert * Mutator: Tell x/y to become y/x * Observer: What is your inverse? * Predicates: negative? positive?, isNegative, isPositive * Negate * Construct from numerator and denominator (ints->rational n d) -- Scheme new Rational(n,d) -- Java * Construct by approximating decimal (decimal->rational dec) new Rational(dec)