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

Jeffrey Vatalaro

gth294a


For anyone unfortunate enough to have to use my terrible answers to study for the midterm, I sincerely apologize.

CoWeb Assignment 1 - Midterm Review


Class-Based Inheritance


A class is an object that defines the behavior and data of other objects that can be created. An instance is one of the objects whose behavior and data is defined by the class. A class functions like both a blueprint for its instances (in that it specifically defines what variables and methods the instances will have and what messages it can receive) and as a factory for its instances (in that it is the class that receives the "new" message for creating a new instance). An instance functions as one of a certain kind of object defined by the class that, while having the same variables and methods as other objects defined by the same class, might contain different values for that data.

Class methods are methods for behavior that a class, but not its instances, has. For example, in a Car class, the class method "new" would be used to create a new instance of Car. Instance methods are methods for behavior that an instance of a class, but not the class itself, has. For example, an instance of the Car class above might have a method for turning left or right by a specified amount. A class responds to messages with class methods, while an instance responds to messages with instance methods.

The distinction between class and instance variables is similar. Class variables are variables that hold data for the class, but not its instances. For example, class variables might be variables that hold constants or keep track of how many instances of the class have been created. Instance variables are variables that hold data for an instance of a class but not the class itself. The values of these variables can vary among instances of the same class.


Tracing Code


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




| data onlyPositiveNumbers |

This line declares the temporary variables "data" and "onlyPositiveNumbers." At this point, both variables point to nil.



data := OrderedCollection withAll: #(1 2 3 -4 -5 'error' 6 -7 999 2).

This line first creates an OrderedCollection containing all the elements in the array
(1 2 3 -4 -5 'error' 6 -7 999 2). It does this by sending the "withAll" message to the OrderedCollection class with the array as the argument. Then, it assigns the OrderedCollection to the variable "data," so that "data" now points to the OrderedCollection. At this point, "data" contains: 1, 2, 3, -4, -5, 'error', 6, -7, 999, and 2.



onlyPositiveNumbers := [:i | (i isKindOf: Number) and: [i positive]].

This line iterates over each item in the "data" OrderedCollection. For each item, it sends the "isKindOf" message to the item with the argument of "Number" to determine if the item is an instance of class Number (that is, if the object is a number). This evaluates to a boolean object. The item also receives the message "positive" to check if it is positive, and this evaluates to boolean object as well. The first boolean object is sent the "and:" message with the second boolean object as the argument, and a new boolean object is returned. If this new boolean object is true, then the item is copied to the "onlyPositiveIntegers" collection. In other words, this line checks each item in the "data" OrderedCollection to see the item is a positive number and then copies those items that are positive numbers to the "onlyPositiveNumbers" collection. "data" is unaffected by this line. At this point, "onlyPositiveNumbers" contains: 1, 2, 3, 6, 999, and 2. "data" still contains: 1, 2, 3, -4, -5, 'error', 6, -7, 999, and 2.



data := data select: onlyPositiveNumbers.

This line sends the "select" message to "data" with "onlyPositiveNumbers" as the argument to return an OrderedCollection that contains only those items that were copied to "onlyPositiveNumbers" in the previous line. "data" is then reassigned to point to this new OrderedCollection. In other words, this line reassigns "data" to an OrderedCollection that contains the elements that are in both "data" and "OnlyPositiveNumbers." At this point, "data" contains: 1, 2, 3, 6, 999, and 2.



data := data copyUpTo: 999. "not including"

This line sends the "copyUpTo" message to "data" with the argument of "999" to copy the items in "data" from the first item up to, but not including, "999" to a new OrderedCollection. Then, "data" is reassigned to point to this OrderedCollection. "not including" is a comment in this line. At this point, "data" contains: 1, 2, 3, and 6.



Transcript show: data average

This line sends the "average" message to "data" to calculate the average of data's elements, which at this point are all positive integers. Then, the "show" message is sent to the Transcript with the average as the argument. This displays the average on the Transcript. Since "data" contains 1, 2, 3, and 6, the average shown on the Transcript is (1+2+3+6)/4 = 3.



CoWeb Assignment 2 - Tool Description


Finding Tools


Using senders of...

You can use senders of... to see a list of the methods that send a specified message. To do this, select the method/message name from a list of method names in a System Browser window (or from the list of method names in the right pane in a Message Sender window) and then bring up the yellow button menu on the name. Select "senders of..." from this menu to open a window with a list in the top pane of the window of the methods that send the selected message. You can click on the names of any of the methods in the list to display that method's code in the bottom part of the window.

You can open a senders of... window in the same manner from the list of message names in the left pane in a Message Sender window, or from the left pane of message names in a Method Finder window, as well. Note that if you bring up the yellow button menu on a message name in either of these lists, you select "senders" instead "senders of...".

It is also possible to open the senders of... window by highlighting the name of a method in a window where you can edit text, such as a Workspace, bringing up the yellow button menu, selecting "more..." from the menu, and then selecting "senders of it." Whether selecting a method name from a text editing window or from a method list, you can alternatively open the senders of... window by just typing Alt-N.


Uploaded Image: Senders Of Example.JPG
Opening an implementors of... window on the "isTagged" method in the SudokuCell class.

Please note that I used Chapter 3 of the textbook as a source for this description of using senders of... .

Using implementors of...

You can use implementors of... to see a list of specific implementations of methods with a given name. To do this, select a method/message name from a list of method names in a System Browser window (or from the list of method names in the right pane in a Message Sender window) and then bring up the yellow button menu on the name. Select "implementors of..." from the menu to open a window with a list in the top pane of the window of names of methods that implement the selected method. Similar to the "implementors of..." window, you can click on the names of the methods in the list to display the code of their implementation in the bottom pane of the window. Note that when you open an implementors of... window by bringing up the yellow button menu on a method name from the list in the left pane in a Message Sender window, or from the list in the left pane in a Method Finder window, you select "implementors" instead of "implementors of... ."

As with senders of..., you can also open an implementors of... window by highlighting the name of a method in a window where you can edit text, bringing up the yellow button menu, selecting "more..." from the menu, and then selecting "implementors of it." Alternatively, any time you have selected a method name, you can open the implementors of... window by just typing Alt-M.


Uploaded Image: Implementors Of Example.JPG
Opening a senders of... window on the "isTagged" method in the SudokuCell class.

Please note that I used Chapter 3 of the textbook as a source for this description of using implementors of... .

Using the Method Finder

You can use the Method Finder to search for a method whether you know the exact name of the method or just a fragment of it. Open the Method Finder by selecting "open..." in the World menu and then selecting "Method Finder." In the top pane of the Method Finder window, you can enter a method name or just a fragment of a message selector. To search for a method, highlight what you entered in the top pane and "print it" or press Enter. The results will be displayed in the bottom pane.

When entering text in the top pane of the Method Finder, you can alternatively enter an example use of the method in the format "receiver. args. answer" and then "print it" or press Enter to search for the method. For example, "1. 2. 3" will yield 3 results.

Another way to use the Method Finder is to enter a line of code in the bottom pane of the window, select the line, and then "print it."

After searching for a method, results (method names) will be displayed in the middle pane on the left side of the Method Finder window. Click on a method name result in this pane to display, in the middle pane on the right side of this window, a list of names of implementations, in various classes, of a method with this name. Clicking on the name of an implementation in this pane will open the code for that implementation in a System Browser.


Uploaded Image: Method Finder Example.JPG
Opening a Method Finder window and using it to find the "isTagged" method in the SudokuCell class.

The comments in the bottom pane of the Method Finder window, which I used as a source for this description of how to use the Method Finder, go into more detail.

Links to this Page