DOC PREVIEW
Berkeley COMPSCI 61A - Object-oriented programming

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61A Notes – Week 4a: Object-oriented programmingParadigm Shift (or: The Rabbit Dug Another Hole)And here we are, already ready to jump into yet another programming paradigm – object-oriented programming. To those used to Java, this will be a pretty straightforward section; to those not, OOP does take a bit of getting used to. However, I’ve always regarded OOP as one of the simpler parts of CS61A – and this is because OOP is often the most natural way to approach certain problems.We will now model the world as many objects, each with its own properties and a set of things it can do. Recall that we started out with smart procedures; given some piece of argument, they know what to do and what to return. Then, we talked about data-directed programming, where we took the smarts away from the procedures and stored them, Big-Brother-like, into this gigantic table; in that case, both the procedures and the data are dumb. Now, we’re going to place the smarts entirely within the data; the data themselves know how to do things, and we merely tell them what to do.Being in a new programming paradigm requires some new syntax, and there’s lots of that in the OOP section in your reader. Read over that carefully and familiarize yourself with your new friends. Those notes are all reasonably clear, so I’m not going to repeat them here.One thing that often confuses people is the difference between classes and instantiations. A class is a set of methods and properties defined using define-class. But defining a class doesn’t give you an instance of that class, however; after you define a person class, you still don’t have a person until you instantiate one – through a call to instantiate. The important difference is that, whereas a class is just an abstract set of rules, an instance is something real that follows those defined rules.Three Ways of Keeping State (or: Memento)As you saw in the reading, there are three kinds of variables:1.instantiation variables – these are variables that you pass into the instantiate call; they’re remain available to you throughout the life of your object.2.instance variables – these are variables associated with each instance of a class; instantiation variables are really just instance variables. You declare these with (instance-vars (var val) (var val)).3.class variables – these are variables associated with a whole class, not just a single instance of the class. Sometimes it makes more sense to keep certain information in the class rather than in each instance (for example, it doesn’t make much sense to have each person object keep track of how many persons have been created; the class should do that). You declare these with (class-vars (var val) ...).Taste the Rainbow (or: Dinner is not Ready)Using objects to represent Skittles is kind of a classic (I’m really not sure why). But it’s easy enough, so let’s try one!Consider this class skittle (the singular of “skittles”, of course):(define-class (skittle color))This is a class with no methods (how many interesting things can a skittle do?) and a single property – its color. Now, we’d like to hold skittles in a bag, so let’s define a bag class:(define-class (bag) (instance-vars (skittles ‘())) (method (tag-line) ‘taste-the-rainbow) (method (add s) ...) ;; adds a skittle s to the bag (method (take) ...)) ;; takes a skittle from the bagA bag object will be able to hold multiple skittles, and you can add or take skittles to or from the bag.CS61A Summer 2010George Wang, Jonathan Kotker, Seshadri Mahalingam, Steven Tang, Eric TzengNotes courtesy of Chung Wu1QUESTIONS1.Implement the add and take methods.2.Implement a (take-color color) method that takes a skittle of the specified color from the bag. You can use find or remove.Directories and Files (Again)Consider a directory class that can hold multiple directories and files:(define-class (directory name) (instance-vars (content ‘())) (method (type) ‘directory) ...)And a file class that holds a list of symbols as content:(define-class (file name content) (method (type) ‘file) ...)We’d like to be able to do these:(define root (instantiate directory ‘root))(define hw1 (instantiate file ‘hw1.scm ‘(I have no idea how to do this)))(define hw2 (instantiate file ‘hw2.scm ‘(please have mercy on me)))(define proj1-soln (instantiate file ‘proj1.scm ‘(my dad is going to kill me)))(ask root ‘add hw1) ;; add the hw1 file to root(ask root ‘add hw2) ;; add the hw2 file to root(ask root ‘mkdir ‘proj1) ;; makes an empty directory in root named ‘proj1’(define proj1 (ask root ‘cd ‘proj1)) ;; returns directory named ‘proj1’(ask proj1 ‘add proj1-soln) ;; add file proj1-soln to directory proj1(ask root ‘mv ‘hw1.scm ‘proj1) ;; moves file hw1.scm from root to proj1(ask root ‘ls) ;; returns list ‘(proj1 hw2.scm)(ask proj1 ‘ls) ;; returns list ‘(hw1.scm proj1.scm)CS61A Summer 2010George Wang, Jonathan Kotker, Seshadri Mahalingam, Steven Tang, Eric TzengNotes courtesy of Chung Wu2QUESTIONS1.Implement the add, mkdir, cd, mv and ls methods for the directory class.2.Implement a size method for the file class; it returns the length of its content.3.The size of a directory is defined as the sum of the sizes of all files in that directory and in all its subdirectories. Implement the size method for the directory class.Pairs Are Objects TooHere’s yet another way to model a cons pair:(define-class (cons-pair-list the-car the-cdr) ...)(define-class (the-null-list) ...)We can make up lists using the cons-pair-list object. So to make the list ‘(2 3), we can do:(define l (instantiate cons-pair-list 2 (instantiate cons-pair-list 3 (instantiate the-null-list))))CS61A Summer 2010George Wang, Jonathan Kotker, Seshadri Mahalingam, Steven Tang, Eric TzengNotes courtesy of Chung Wu3QUESTIONS: For questions 2-5, you cannot use if or cond. Assume a cons-pair-list object represents a valid, flat list for the following.1.Implement a (make-oop-list ls) procedure that takes in a normal list and returns a cons-pair-list constructed with instances of the above cons-pair-list and the-null-list classes. Assume ls is a flat list.2.Implement the listify method for a cons-pair-list so that (ask l 'listify) returns the list (2 3).3.Implement the length method for a cons-pair-list so that (ask l 'length) returns 2.4.Implement the (accumulate combiner init) method for a cons-pair-list; for example, (ask


View Full Document

Berkeley COMPSCI 61A - Object-oriented programming

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Object-oriented programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Object-oriented programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-oriented programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?