6 Pages

oop

Course: CSC 151, Fall 2009
School: Grinnell College
Rating:
 
 
 
 
 

Word Count: 1520

Document Preview

of Fundamentals CS I (CS151 2001S) Object-Oriented Programming Records, Revisited As you may recall, one of the key issues in the design of records is that the record designer have some control over the use of records. In particular, the designer might want to require that some fields be fixed and allow others to be mutable. The designer may also want to limit the legal values of some fields. As we saw in the...

Register Now

Unformatted Document Excerpt

Coursehero

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
of Fundamentals CS I (CS151 2001S) Object-Oriented Programming Records, Revisited As you may recall, one of the key issues in the design of records is that the record designer have some control over the use of records. In particular, the designer might want to require that some fields be fixed and allow others to be mutable. The designer may also want to limit the legal values of some fields. As we saw in the corresponding lab, it is relatively easy for someone other than the designer/implementer of the record to modify the record in inappropriate ways. For example, suppose that someone has written a student record type and someone else has written a related set of utilities. We might hope that the following would only behave correctly: (load "student.ss") (load "sams-student-stuff.ss") ; includes compute-gpa (define report-gpa (lambda (student) (if (not (student? student)) (error "Bozo, thats not a student") (display (compute-gpa student))))) If we havent looked at the code for compute-gpa, we have no guarantee as to whether or not the student record is still correct afterwards. (Other than crossing our fingers.) The compute-gpa procedure may have gone behind the scenes and changed a field. It will also fairly clear to anyone who uses our records that weve chosen to implement records with vectors. They can determine this fact by printing out a record that theyve created. Hence, at a more detailed level, theres nothing to stop someone from changing a fixed field of a record by using vector-set!. Wed like to encapsulate our implementation so that we can hide how our records are implemented and restrict how theyre used. Objects: Records that Protect Their Fields One of the basic ideas of the programming paradigm called object-oriented programming is to encapsulate the data so as to intercept low-level interventions and treat them as errors. An object is a data structure that permits access to and modification of its elements only through a fixed set of procedures -- the objects methods. One cannot peek inside an object; one is limited to the procedures provided. To request the execution of one of these methods, one sends the object a message that names the desired method, providing any additional parameters that the object will need as part of the message. Attempting to send an object a message that does not name one of its methods simply causes an error. The custom is to precede the message names with colons. 1 Objects in Scheme The Scheme standard does not include objects. However, you can implement an object as a procedure that takes messages as parameters and inspects them before acting on them. Since Scheme typically does not allow one to look inside procedures, procedures provide an appropriate form of encapsulation. How do we store data for use within the procedure? We can use vectors to build the storage locations that are protected by the procedure. Heres a simple example -- an object named sample-box that contains only one field,contents, and responds to only one message, :show-contents. ;;; Value ;;; sample-box ;;; Type: ;;; object ;;; Purpose: ;;; To provide a sample "box"; something whose value you ;;; can look at but not change. ;;; Valid Messages: ;;; :show-contents ;;; Get the contents of the box. (define sample-box (let ((contents (vector 42))) (lambda (message) (if (eq? message :show-contents) (vector-ref contents 0) (error "sample-box: unrecognized message"))))) That is, Build a new symbol table with the let that contains one name-to-value mapping (that is, it maps contents to a one-element vector that contains 42). Build and return a procedure that takes a message as a parameter. Since the lambda falls within the let, it has access to that new symbol table and nothing else has direct access. We can test our sample object by trying to set the contents to 0. > (sample-box :show-contents) 42 > (sample-box :set-contents-to-zero!) sample-box: unrecognized message > (sample-box :set-contents 0) sample-box: unrecognized message > (set! (sample-box :show-contents) 0) set!: not an identifier at: (sample-box (quote :show-contents)) ... > (set! contents 0) set!: cannot set undefined identifier: contents > (sample-box :show-contents) 42 2 All the attempts to modify the contents field of sample-box fail. Sending it the message :set-contents-to-zero! doesnt work, because the procedure is not set up to receive such a message. And you cant reach the actual contents variable from outside the sample-box procedure because that identifier is bound to the storage location that contains 42 only inside the body of the let-expression. Changing Object Values One could revise the procedure so that it would accept the message :set-contents-to-zero!: ;;; Value ;;; zeroable-box ;;; Type: ;;; object ;;; Purpose: ;;; To provide a sample "box"; something whose value you ;;; can look at and change to 0 ;;; Valid Messages: ;;; :show-contents ;;; Get the contents of the box. ;;; :set-to-zero! ;;; Set the contents of the box to 0. (define zeroable-box (let ((contents (vector 57))) (lambda (message) (cond ((eq? message :show-contents) (vector-ref contents ((eq? 0)) message :set-contents-to-zero!) (vector-set! contents 0 0)) (else (error "zeroable-box: unrecognized message")))))) > (zeroable-box :show-contents) 57 > (zeroable-box :set-contents-to-zero!) > (zeroable-box :show-contents) 0 Of course, there is no way for anyone to set the contents of this particular object to anything except zero. Now that the box has been zeroed its contents will remain zero forever. Making Several Objects of the Same Type In the preceding examples, we have created only one object of each type, but it is not difficult to write a higher-order constructor procedure that can be called repeatedly, to build and return any number of objects of a given type. Suppose, for example, that we want to build several switches, each of which is an object with one field (a Boolean value) and responding to only two messages: :show-position, which returns on if the field contains #t and off if it contains #f, and :toggle!, which changes the field from #t to #f or from #f to #t. Heres a constructor for switches: 3 ;;; Procedure: ;;; make-switch ;;; Parameters: ;;; None ;;; Purpose: ;;; Creates a new switch in the off position. ;;; Produces: ;;; newswitch, a switch ;;; Preconditions: ;;; None ;;; Postconditions: ;;; newswitch is an object which responds to two messages: ;;; :show-position ;;; Shows the current position (on or off) ;;; :toggle! ;;; Switches the current position (define make-switch (lambda () (let ((state (vector #f))) ; All switches are off when manufactured. (lambda (message) (cond ((eq? message :show-position) (if (vector-ref state 0) on off)) ((eq? message :toggle!) (vector-set! state 0 (not (vector-ref state 0)))) (else (error "switch: unrecognized message"))))))) Now lets manufacture a couple of switches and show that they can be toggled independently: Because the make-switch procedure enters the let-expression to create a new binding each time it is invoked, each switch that is returned by make-switch gets a separate static variable to put its state in. This static variable retains its contents unchanged even between calls to the object and independently of calls to any other object of the same type. Methods with Additional Parameters In all of the preceding examples, the messages received by the object have not included any additional parameters. Suppose that we want to define an object similar to sample-box except that one can replace the value in the contents field with any integer that is larger than the one that it currently contains, by giving it the message :replace-with and including the new, larger value. We can accommodate such messages by making the object a procedure of variable arity, requiring at least one argument (the name of the method to be applied) but allowing for more: ;;; ;;; ;;; ;;; ;;; ;;; ;;; ;;; ;;; ;;; ;;; Procedure: make-growing-box Parameters: None Purpose: Creates a new box whose values you can change to larger values. Produces: newbox, a box whose contents can change to larger values. Preconditions: None Postconditions: 4 ;;; newbox is an object which responds to two me...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Bethel VA - CHE - 400
ProjectChE 400 Applied Chemical Engineering Calculations Fall 2007 Project Due 11/09/07 General Comments 1. As explained in the syllabus you will need to write a report in the format of a technical memorandum about your project. Examples of how to write
University of Toronto - CSC - 228
000000000000000000000000000000000101010101010101010101010101010100110011001100110011001100110011011001100110011001100110011001100000111100001111000011110000111101011010010110100101101001011010001111000011110000111100001111000110100101101001011010010110100
University of Toronto - CSC - 228
0001
University of Toronto - CSC - 228
# First, decode Test_Input1.txt[strider@layer A1]$ decode Test_Input1.txt Test_Output1.txt Code_Matrix5.txtDecoding file Test_Input1.txtto file Test_Output1.txtCode matrix is in: Code_Matrix5.txtCorrected 0 bits.Found 0 unrecoverable errors.# Check
University of Toronto - CSC - 228
000000100000000000000100000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010
University of Toronto - CSC - 228
University of Toronto - CSC - 228
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
University of Toronto - CSC - 228
000000000000000000000001000000000101000101010101010101010101010100100011001100110011101100110011001001100110011001100110011001100001111100001101000011110000111101011011110110100101101001011010001111011011110000111100001001000110100101101001011010010111110
University of Toronto - CSC - 228
000000000000000000000000000000000101010101010101010101010101010100110011001100110011001100110011011001100110011001100110011001100000111100001111000011110000111101011010010110100101101001011010001111000011110000111100001111000110100101101001011010010110100
University of Toronto - CSC - 228
University of Toronto - CSC - 228
a0Actinum canyon a0Al2O3 canyon a0Beryllium canyon a0Boron canyon a0CH3 canyon a0CO2 canyon a0Cadmium canyon a0Calcium canyon a0Carbon canyon a0Cesium canyon a0Gallium canyon a0Germanium canyon a0Gold canyon a0H2 c
University of Toronto - CSC - 228
c7magnetite soft sand g2MgO dune h9Chloride plain e7H2 CO2 ice b1SO3 CO2 ice b4FeO soft sand g5MnO rocky h2magnetite moving sand b2Lead rocky g9magnetite rocky e6MnO dune d3Potasium cliff c8Calcium crevasse d4CO2 d
University of Toronto - CSC - 228
f4K2O cliff a8Sodium plain g7MnO CO2 ice f7FeO cliff f5Chromium cliff h8TiO2 plain i6SiO2 plain b6Potasium soft sand c6H2 CO2 ice f3MgO CO2 ice a1NaCl dune a2Chloride rocky h3K2O plain f1FeO CO2 ice e8Calcium so
University of Toronto - CSC - 228
Implicit Type Conversion With Operator OverloadingC provides implicit type conversion: int j; double a,b; a = b + j; Additional are: examples of implicit type conversionschar -> short short -> int int -> long long -> float float -> double C+ extends the
University of Toronto - CSC - 228
Pointers and Dynamic Memory Management There are two main areas in memory that our program can use to store information: - The stack, which is handled automatically and stores local variables - The heap, which is handled by your program, and can store inf
University of Toronto - CSC - 228
Operator OverloadingAllows the programmer to define the way that operators will work with user defined types (classes).- Only apply to user defined types- If not used carefully can cause obfuscation!General Considerations- At least one operand must b
University of Toronto - CSC - 228
Streams in C+ C+uses streams to handle data I/O. Data flow is treated as a stream of bytes, the iostream.h library contains the classes that perform screen and file I/O. - iostream classes perform screen I/O. - fstream classes perform file I/O. Additional
University of Toronto - CSC - 228
CSC228H - File Structures and Data ManagementThe Relational model Proposed in 1970 by E.F. Codd in A Relational Model of Data for Large Shared Data Banks, Communications of the ACM, Vol. 13, No. 6, June 1970, pp. 377-387 It was intended to form the basis
University of Toronto - CSC - 228
CSC228H - File Structures and Data ManagementOne last work about hashing. Hashing has found applications on many domains (which share the property that fast access to information is critical) The power of hashing as an indexing technique has been applie
University of Toronto - CSC - 228
CSC228H - File Structures and Data ManagementExtendible Hashing The performance of standard hashing techniques degrades with the occurrence of additions and deletions. In the worst case, we are back to O(n) search complexity. Once the load factor becomes
University of Toronto - CSC - 228
CSC228H - File Structures and Data ManagementB-Trees Provide an efficient structure for maintaining a LARGE index (one that does not fit in main memory and has to be managed on disk). Eliminates the need to sort data on disk, data is divided into pages a
University of Toronto - CSC - 228
Sample data, for CD's only!UPC 4000 7000 2000 1005 9000 5000 3000 6000 8000 1000Title I'm alive! Chicken dance Opus #31415 Best of. Opus #22108 I'm alive 2 The red disk The grey disk Anchovy Rules! Opus #24289Artist Anchovy Iron wasabi Mu-seek Anchovy
University of Toronto - CSC - 228
CSC228H - File Structures and Data ManagementThis week The File Structures Workshop! In this couple of hours, you will use the techniques we have covered during the previous lectures to: - Design the file structures required to store a massive amount of
University of Toronto - CSC - 228
CSC228H - File Structures and Data ManagementIndexing. Allow direct fast access to files Eliminates the need to re-organize or sort the file (files can be entry sequenced) Provide direct access for files with variable length records Provide multiple acce
University of Toronto - CSC - 228
CSC228H - File Structures and Data ManagementData compression. Results in a more efficient use of storage Allows faster transmission times Enables faster sequential processing Has an associated cost due to the de-compression process1Francisco J. Estrad
University of Toronto - CSC - 228
CSC228H - File Structures and Data ManagementWeek 1 - Welcome to the course!Important course information: Office hours: Wednesday 6-7 pm, SF 2110 Course web page: http:/www.cs.utoronto.ca/~strider/csc228 My email address: strider@cdf.toronto.edu1Franc
University of Toronto - CSC - 228
CSC228H - File Structures and Data ManagementFile structure design principles. Ideal case: get all the information we want with just one access. Is this possible in practice? Whenever we can't get the information we need in just one access, we would l
University of Toronto - CSC - 228
CSC 228H - File Structures and Data Management, Summer 2002 Extendible hashing insertion exercises.Given the following directory, and the set of buckets (each of which can hold 2 pseudokey/reference pairs):2 000101 000 001 010 011 100 101 110 111 2 1100
University of Toronto - CSC - 228
CSC 228H - File Structures and Data Management, Summer 2002 Extendible hashing insertion exercises.Given the following directory, and the set of buckets (each of which can hold 2 pseudokey/reference pairs):2 000101 000 001 010 011 100 101 110 111 2 1100
University of Toronto - CSC - 228
CSC228H - File Structures and Data Management, Summer 2002 B-Tree Insertion exercise, given the following B-tree:iszdilnsvzacd f ijkl m nqrs u vxyzShow the structure and contents of the tree after inserting each of the following key
University of Toronto - CSC - 228
CSC 228H - File Structures and Data Management, Summer 2002 Final Quiz Relational Algebra Name:_ Student #: _1.- Given the following set of tablesD1dish_id teri01 teri02 burg01 faji01 teri03 burg02 chow01 souvl01 sala01 spag01 dish_name Chicken Teriyak
University of Toronto - CSC - 228
CSC 228H - File Structures and Data Management, Summer 2002 Final Quiz Relational Algebra Name:_ Student #: _1.- Given the following set of tablesD1dish_id teri01 teri02 burg01 faji01 teri03 burg02 chow01 souvl01 sala01 spag01 dish_name Chicken Teriyak
University of Toronto - CSC - 228
Student#Team Name990236954U2990236605991903310Hunter991586155990824872228'ers990793806981704430The Orion Project990965951981869570Octal Dump981449160991097442Fellowship of the Ring990908951991043483Division 029904646089804871
University of Toronto - CSC - 228
%!PS-Adobe-2.0 %Creator: dvipsk 5.58f Copyright 1986, 1994 Radical Eye Software %Title: guide.dvi %Pages: 13 %PageOrder: Ascend %BoundingBox: 0 0 612 792 %EndComments %DVIPSCommandLine: dvips -o guide.ps guide.dvi %DVIPSParameters: dpi=300, comments remov
University of Toronto - CSC - 228
IMPORTANT NOTES: - The marks for the final exam were adjusted DOWNWARD due to a very highaverage of 77%, please take into account that this is in effect balancesthe upward adjustment performed upon the Midterm grade (which; as you mayrecall, was origi
University of Toronto - CSC - 228
#%-12345X@PJL SET RESOLUTION = 600 @PJL ENTER LANGUAGE=POSTSCRIPT %!PS-Adobe-3.0 %Title: Untitled Document %Creator: Windows NT 3.5 %CreationDate: 14:11 7/2/2002 %Pages: (atend) %BoundingBox: 12 12 599 780 %EndComments %BeginProcSet: NTPSOct94 % Copyright
University of Toronto - CSC - 228
%!PS-Adobe-2.0 %Creator: dvipsk 5.58f Copyright 1986, 1994 Radical Eye Software %Title: testing.dvi %Pages: 16 %PageOrder: Ascend %BoundingBox: 0 0 612 792 %EndComments %DVIPSCommandLine: dvips testing %DVIPSParameters: dpi=300, comments removed %DVIPSSou
Lake County - USERS - 427
Large Scale Software Architecture A Practical Guide Using UMLBy Jeff Garland and Dick Anthony April 28, 2002Large Scale Software Architecture Copyright 2002 Richard Anthony and Jeff Garland All Rights ReservedPage 1Preface . 9 Chapter 1 Introduction .
UNC Asheville - HUM - 324
The Western Enlightenment Scientific Revolution Enlightenment Copernicus, On the Locke, An Essay Revolution of the Concerning Human Heavenly Spheres Understanding (1689) (1543) Paine, The Rights of Newton Mathematical Newton, Man (1790) Principles of Nat
U. Houston - TCED - 4738
Instructional ModificationsInstructions to Intern: What follows is a checklist of modifications. Any variety of these modifications may be needed in order for a student to be successful in the school or classroom environment and/or to achieve curricular
U. Houston - TCED - 4738
Pay Attentionhttp:/www.teachertube.com/view_video.php?viewkey=40c570a322f1b0b65909 (7 mins. 41 secs.) Have you been paying attention? How do your students learn? Are they interpersonal, spatial, logical, intrapersonal, musical, linguistic, naturalist, Bo
U. Houston - TCED - 4738
Name_ Date_Internship I (TCED 4738) Reflective Learning Journal Entry One: Planning and Teaching Experience Reflection Substitute Day for Mentor After your substitute day, reflect on your professionalism and your impact on student learning. Write a refle
U. Houston - TCED - 4738
(c) 2006 The McGraw-Hill Companies, Inc. All rights reserved.Three Schools of Thought about Learning and TeachingChapter 4 (c) 2006 The McGraw-Hill Companies, Inc. All rights reserved.Three Schools of Thought The Cognitive School of Thought The Huma
U. Houston - TCED - 4738
(c) 2006 The McGraw-Hill Companies, Inc. All rights reserved.GettingtoKnow YourStudentsand MotivatingThem toLearnChapter5(c) 2006 The McGraw-Hill Companies, Inc. All rights reserved.Knowingand MotivatingLearners Gettingtoknowstudents Unearthingnewinf
U. Houston - TCED - 4738
(c) 2006 The McGraw-Hill Companies, Inc. All rights reserved.Teaching Diverse StudentsChapter 3 (c) 2006 The McGraw-Hill Companies, Inc. All rights reserved. Student Diversity Socioeconomic differences Cultural differences Gender differences Sexu
U. Houston - TCED - 4738
(c) 2006 The McGraw-Hill Companies, Inc. All rights reserved.Reflective Skills of Effective TeachersChapter 14 (c) 2006 The McGraw-Hill Companies, Inc. All rights reserved.Reflection Characteristics of Reflective Practitioners Benefits of Reflecting
U. Houston - TCED - 4738
(c) 2006 The McGraw-Hill Companies, Inc. All rights reserved.The Challenge of Teaching in a Changing SocietyChapter 2 (c) 2006 The McGraw-Hill Companies, Inc. All rights reserved.America's Challenges A changing population The changing family The cha
U. Houston - TCED - 4738
(c) 2006 The McGraw-Hill Companies, Inc. All rights reserved.FactorsThatInfluence HowWeTeachChapter1(c) 2006 The McGraw-Hill Companies, Inc. All rights reserved.FactorsThatInfluence HowWeTeachHow We TeachPersonal CharacteristicsExperience and Prepa
Washington - WEEK - 142
Week 7ListsSpecial thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed under: http:/creativecommons.org/licenses/by-nc-sa/3.0Lists list: Python's equivalent to Java'
Wheaton College - MATH - 236
Math 236All this trouble for one little Geococcyx californianus1Wile E. Coyote Bleached Bones, AZ 59055 February 6, 2009 Math 236 Students Wheaton College Norton, MA 02766 Dear Calculus Students: The pain, the agony, the frustration have nally gotten t
University of Toronto - CSC - 340
CSC340H5 SInformation Systems Analysis and Designpage 1/12University of TorontoFaculty of Arts and Science at Erindale Department of Computer ScienceCSC340S - Information Systems Analysis and DesignSpring 2002 Arnold RosenbloomApril-May Examination
University of Toronto - CSC - 340
DATA PEAR SYSTEMSDanielle Lottridge Simon Hatch Joshua CollingsDetailed Design: The HMV Information System InterfaceCSC 340S Monday, April 16th, 2001 TA: Afsaneh FazliDesign Details Table of Contents Introduction Part A: Global Architecture Specificat
University of Toronto - CSC - 340
CSC 340 Assignment 1Feasibility Study on Behalf of: TNT Canada Inc. Toronto BranchTeam #: Team 9 By: Chon-Woo Kim, Simon Tang, Welly Hong Date: February 5, 2001 Prof.: John Mylopoulos T.A.: Afsaneh Fazli RW 2290Table of ContentsThe Organization The C
CSU Long Beach - IS - 233
A B C D 1 Introduction to the PMT Function: Financing the Dream Machine 2 You think you've found the perfect car - if only it fits within your budget. 3 How much will be due How much higher will the 4 payment be if loan term is 5 each month on a four3 yea
Texas San Antonio - CS - 505
SimultaneousMultithreading:MaximizingOn-ChipParallelismDean M. Tullsen, Susan J. Eggers, and Henry M. Levy Department of Computer University Science and Engineering of WashingtonSeattle, WA 98195AbstractThis paper examines simultaneous mitting sev
Texas San Antonio - CS - 505
Appears in the Proceedings of the 34A Design Space Evaluation of Grid Processor ArchitecturesRamadass Nagarajan Karthikeyan Sankaralingam Doug Burger Stephen W. KecklerComputer Architecture and Technology Laboratory Department of Computer Sciences The
Texas San Antonio - CS - 505
Improving Direct-Mapped Cache Performance by the Addition of a Small Fully-Associative Cache and Prefetch BuffersNorman P. Jouppi Digital Equipment Corporation Western Research Lab 100 Hamilton Ave. Palo Alto, CA 94301AbstractProjections of computer te
Texas San Antonio - CS - 505
IMPLEMENTATIONOF PRECISEINTERRUPTSIN PIPELINEDPROCESSORSDtpanmtnr01 Elt~r~l awi Compurrr Enginttring Unibwsiiy o/ Wisconsin-Madison Mdison. WI J37W AndrewR. PlazkunCaqwuttr Scitncrs Dtpamntnr Univtrsip of Wisconsin-Madison Modiron. WI 5.1706Abalm
Lake County - ECE - 541
ECE/CS 541: COMPUTER SYSTEM ANALYSISHomework #1 Due Thursday September 7, Beginning of Class August 31, 2006 The intent of this homework is to review material you should have learned in your probability and statistics course (e.g., ECE 413) and explore n
CSU Sacramento - CSC - 20071203
ATTACHMENT HName & last four digits of SSN:STUDENT APPLICATIONI. THIS CHECKLIST IS REQUIRED TO BE THE FIRST PAGE OF THE APPLICANTPACKAGE, AND MUST BE SIGNED BY THE STUDENT APPLICANT AND THE PI. 1. Provide address at school and permanent address as wel
CSU Sacramento - CSC - 20071203
Department of Defense Information Assurance Scholarship ProgramSponsored by the Assistant Secretary of Defense (ASD) for Networks and Information Integration (NII) DoD Chief Information OfficerSOLICITATION FOR PROPOSALSFrom Universities Designated by t