View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Brandon Pitman

Hi.

Writing Code:
fib
	(self < 0) ifTrue: [self error: 'fib only works on nonnegative numbers.'].
	(self < 2) ifTrue: [^1].
	^(self - 1) fib + (self - 2) fib.


This should be added as a method in the Integer class.

Tracing Code:
| data onlyPositiveNumbers |
data := OrderedCollection withAll: #(1 2 3 -4 -5 'error' 6 -7 999 2). "data = {1, 2, 3, -4, -5, 'error', 6, -7, 999, 2}"
onlyPositiveNumbers := [:i | (i isKindOf: Number) and: [i positive]]. "data unchanged"
data := data select: onlyPositiveNumbers. "data = {1, 2, 3, 6, 999, 2}"
data := data copyUpTo: 999. "not including" "data = {1, 2, 3, 6}" 
Transcript show: data average "data unchanged, transcript prints 3"


Using the Debugger:
When a bug pops up, the debugger will open. Click open the debugger window and look at the stack trace. Click around on each of the stack traces to see where exactly the call which caused the error occurred. You can even edit your code in there directly, though it's probably a better idea to use the system browser or something.

==

CoWeb Assignment 3


Blocks in Virtual Machines (2 Points)
(Part A) The first two expressions use the message
parenthesisGreaterThan:
, which does not use a block to separate the two logical parts of the expression. Therefore, before the & message is parsed, both the receiver
(self > greaterThan)
and the argument
(self < lessThan)
are evaluated. So even if the first check would fail, the second one is still evaluated, so the same amount of time would be taken in either case.

In the case of the second two expressions,
blockGreaterThan:
is used, which uses a block to store the second part of the logical and. In this case, only if the first check fails will the block be evaluated to see the second check. So in the case of the third expression, -500 < 0, so the block is never evaluated; in the fourth expression, 500 > 0, so the block is evaluated. The evaluation of the block is why the two expressions differ in runtime.

(Part B) The block bytecode uses at most two messages to the numbers (one < message and one > message), and it also quits as soon as it has figured out that the logical and of the two expressions must necessarily be false; meanwhile, the parenthesis bytecode uses three messages (one <, one >, and one &) to complete its work and it always evaluates both sub-expressions of the logical and, even if the first comes out false.

(Part C) The smalltalk compiler inlines the
and: [block]
expression so that the part of the code that handles the message as a whole and the block are intertwined, leading to greater performance.

History of Object-Oriented Programming (1 Point)
Ivan Sutherland: Sutherland, as part of his doctoral thesis, invented a computer system known as "Sketchpad." It was the first computer system to use only a GUI as its user interface, as well as being the ancestor of modern CAD programs; however, more important to object-oriented purposes, it was also the first computer system to have some concept of objects, allowing the user to create master drawings (classes) from which many copies (instances) could be made.

Kent Beck: Beck has done a bunch of stuff. He created Extreme Programming, he popularized CRC cards, and he created JUnit. He also writes a lot of stuff, even stuff about Smalltalk!

Collection Classes (1 Point)
1. The OrderedCollection and Set are both classes which store a collection of other objects inside of them; their main difference is that the OrderedCollection imposes some order on its contents (this is the first object, this is the second, etc.), while the Set contains the object but stores them in whichever order it chooses. For practical programming purposes, you can change the position of objects inside an OrderedCollection and the OrderedCollection will always yield its contents in the same order each time it is iterated over; this is not true of the Set.

2. OrderedCollection and Array both store an ordered collection of objects, and both allow random access to these objects; however, the OrderedCollection can be resized to fit additional contents, while the size of the Array is fixed at creation. You would use an OrderedCollection for most purposes; however, the Array is better when you have an upper bound or fixed number on the number of elements you need to hold at once.

3. You use a Dictionary to store a collection of objects, much like the other collection classes discussed so far. However, instead of keeping its elements in an unordered manner (as per the Set), or in a linear manner (as per the OrderedCollection and Array), the Dictionary holds its contents with respect to a set of keys. Each element in the set is associated to one or more keys, such that by giving the Dictionary a given key you can get the single element that is associated to that key. Accessing a Dictionary is very fast because it uses a fast hash method to turn any key into a number, then uses that number to decide which "bucket" to put each element into. If a bucket contains more than one element, a quick linear search finds the one that is wanted.

Links to this Page