3.2 Explore OOP from the Browser

In Figure 2.1 of the Browser, below the classes pane, there are three buttons:

Below these three buttons, observe the wide text pane, it provides contextual information on the selected item.

Again, Class methods apply to the Class itself. Instance Methods apply for all instances modeled by the class. We saw above that the class Fraction has a method #numerator:denominator: which is used to get a new instance of a Fraction. There is only one Fraction class object. Messages like #squared and #abs are sent to any Fraction instance, of which there are many.

Up to now we have attempted to be very careful with definitions, but you know that when we say “a fraction” we mean “an instance of the class Fraction”. From here our language will be more casual.

 CuisLogo When the Float class is selected, what is the information provided by the text pane?

Exercise 3.1: Float class information

We have spent much time here because it is important to avoid confusion between instance methods and class methods. Let’s consider the Float class as an example.

Class Methods. In Figure 3.1 the methods listed are class side, in the browser the class button is pressed to see this list.

ch03-floatClassSide

Figure 3.1: Class methods in Float

From a Workspace, these methods are called with the message name sent directly to the class:

Float e
⇒ 2.718281828459045
Float epsilon
⇒ 2.220446049250313e-16
Float fmax
⇒ 1.7976931348623157e308

 note You have noticed that text typed into the Workspace is colored and highlighted based on what you type. We will discuss this below when we talk about the Smalltalk language, but the idea is to be helpful. If you start to type a word the Cuis Workspace knows about, you can press the tab key and get a set of choices for completion of the word. Try typing Float epsi and pressing tab. You can then press enter and should see Float epsilon. Click elsewhere on the Workspace to make this menu go away.

Nevertheless, you can not send a class message to an instance of Float, it throws an error and opens the red debugger window. Just close the debug window for now to ignore the result.

3.14 pi
⇒ MessageNotUnderstood: SmallFloat64>>pi
Float pi e
⇒ MessageNotUnderstood: SmallFloat64>>e

Often these class methods are used to access constant values as seen in the previous example or to create a new instance:

OrderedCollection new
⇒ Create a new empty ordered collection
Fraction numerator: 1 denominator: 3
⇒ 1/3 "a fraction instance"
Float new
⇒ 0.0
Float readFrom: '001.200'
⇒ 1.2 
Integer primesUpTo: 20
⇒ #(2 3 5 7 11 13 17 19) 

Instance methods. In Figure 3.2, the methods listed are instance side, in the browser the instance button is pressed to see this list.

ch03-floatInstanceSide

Figure 3.2: Instance methods in Float

In a Workspace, these methods are called with the message name sent directly to an instance:

-10.12 abs ⇒ 10.12
3.14 cos ⇒ -0.9999987317275395
-10.12 * 2 ⇒ -20.24

Instance method messages cannot be sent directly to a class, you need to instantiate an object first:

Float cos
⇒ MessageNotUnderstood: Float class>>cos
Fraction squared
⇒ MessageNotUnderstood: Fraction class>>squared
OrderedCollection add: 10
⇒ MessageNotUnderstood: OrderedCollection class>>add:

Of course you can mix both class and instance methods, as long as you send the message to the appropriate class or instance:

Float pi cos
⇒ -1.0
Float e ln
⇒ 1.0 
(Fraction numerator: 4 denominator: 5) squared
⇒ 16/25
OrderedCollection new add: Float pi; add: Float e; yourself
⇒ an OrderedCollection(3.141592653589793 2.718281828459045) 

Here is another example from Spacewar! mixing class and instance methods. This portion of code updates the orientation of a torpedo according to its velocity vector:

self rotation: (velocity y arcTan: velocity x) + Float halfPi

Example 3.2: Aligning a torpedo with its velocity direction

With this brief introduction to the system browser, you are now equipped to explore the system classes.