CS2340 Homework #1
Setup:
Download and install Cincom Visualworks Smalltalk http://smalltalk.cincom.com/index.ssp
Part 1:
- Go to
the class swiki and create a page for yourself
in the Who’s Who portion of the website.
- While
Smalltalk is a “pure” OO language (everything is an object), Java is
not. Java has primitives like int and float.
This makes the design of the API non-OO in many areas. Take a look at the Java API for the Math
class with all its static methods.
How would this API be implemented in Smalltalk? Just pick 1 method (like
abs) and give an example of how it would be implemented in Smalltalk?
(What class would it be in, and give an example of a call, you do not need
to write the implementation code)
Hint: think about factorial example we did in class.
Part 2:
- Let’s
play a little with Smalltalk.
Double-click your icon for Visualworks
(or use Programs->Visualworks->VisualNC. For
Linux and Mac users, you should see similar startup options after install.
- You
should have 2 windows: One titled Workspace and one titled Visualworks NonCommercial. Let’s backup our image first. Go to the File menu and select Save
Image As…
Give your image the name working.
Anytime your image becomes corrupted, simply reload your orginal Visualworks image to
“start over”. Modify your startup
icon to use working.im instead of visualnc.im.
This leaves visualnc.im as your original
clean image.
- Now
let’s play with some operations. Go
to the Workspace window. If you
accidentally closed this window, you can launch another by going to the Visualworks main window and select the Tools menu and
then Workspace.
- You
may type the entries in Part 3 by hand into the workspace, or you may load
a saved file that has all the entries pre-typed. To do this, in the workspace window
select File->Open. Then load the
Homework1.ws file. You should see a new tab appear in the
workspace with the entries below filled out.
Part 3:
For this section, type in (or select the appropriate lines)
and select PrintIt to see the actual answer. For each line, FIRST write out what you think
the answer will be, then PrintIt (or DoIt is specified) and record the actual answer.
|
Smalltalk Code
|
Expected
|
Actual
|
|
5 - 3 * 2
|
|
|
|
‘Bob’ class
|
|
|
|
2 * 2 - 1 factorial
|
|
|
|
10 between: 2 factorial + 3 and: 20
|
|
|
|
'cs','2340'
|
|
|
|
#(a b c) inspect (Do It)
|
|
|
|
|a| a := 5. a negated.
|
|
|
|
a := a + 6.
|
|
|
|
Highlight both the above lines and PrintIt
(ignore dialog)
|
|
|
|
‘’ notNil
|
|
|
|
‘burdell’ at: 1
|
|
|
|
‘buzz’ size
|
|
|
|
|v| v:= #($p $o $c $k) as Array.
(v at:1 put: $s). v.
|
|
|
|
Transcript show: ‘I like’; cr;
show: ‘Smalltalk’. (DoIt) (Look for result in Transcript Window)
|
|
|
|
|
|
|
Part 4:
Defining our first class.
- In the
Visualworks window, select Browse->System.
- The
System browser will be the main window that you use to create and edit
classes and methods.
- There
are four listviews in the middle of the
browser. From left to right, these
are the Package, Class, Protocol and Method views. Scroll down the Package view until you
see the (none)* package. This
should be the last entry in the view.
- Click
on the (none)* package and the bottom portion of the window should change
to 5 tabs: Source, Properties, Comment, Rewrite and Critic.
- For
now, we will care only about the first 3.
Select the source tab. In
the Source window, you can see a stubbed out dummy class. You can edit this entry to create your
own class if desired. Otherwise,
you can use the wizard.
- Let’s
use the wizard. Position the mouse
pointer over the second list view from the left (our class list) and
right-click select New Class.
- In the
third box down (labeled name) type in MyFirstClass
as the name of your class. In the
fifth box down (labeled instance variables) type in v1.
- Now
hit OK. Look in the source window,
and you can see the definition of your class.
- Click
on the Comment tab, and you can enter comments that describe your
class. You can see some are stubbed
out already. Replace the Default Comment
(first two sentences) with your own comment.
- Click
the Hierarchy tab. You can see a
graphical representation of the inheritance structure of your class. Since we just accepted the default of Core.Object, we see that we inherit from the standard
root object. This is similar to
java where everything inherits from java.lang.Object.
- Now
let’s look at some methods. Click
on the Protocol initialize-release.
Recall that protocols are simply ways to organize our methods into
logical groups. You can see all the
methods in this protocol. In this
case, there is one method initialize.
If you select initialize, the Source window will show you the
code. This code was auto-generated
by our class creation wizard. It
basically initializes our instance variable to nil. Change the nil to 1235 and press ctrl-S
to accept. Now click the accessing
protocol. Here we find all the
“getters/setters” grouped together.
Again these are auto-generated by the wizard for all our instance
variables.
- To
create a new Protocol, position the mouse cursor in the protocol window
and right-click. Then select
New… In this window, you type any
name you wish. There are some
pre-existing names that are somewhat “standard” in the smalltalk
world. These will pop up based upon
the letters typed. You may choose
one of these, or continue typing your new name. Let’s make a protocol named
converting. Since there is already
one of these, just type ‘c’ in the listbox and
then select converting and click OK.
- To create a method, we first select a
Protocol to put the method in.
Select (converting). It is
in italics and surrounded by parens to show that
there are no methods currently in the protocol. When you select the protocol, the
Source window will contain a stubbed out version of a method. Delete the placeholder “message selector
and argument names” and type in asInteger. The next line is comment (since it is
between double quotes). You can
replace that comment with anything you like. We do not need any local variables so
delete the temporary variable lines.
Finally, replace the statements line with ^v1. Press Ctrl-S to accept the changes. This will commit your code to the
image.
- Now
return to the Workspace. In the
workspace area type: |c| c:= MyFirstClass new. c asInteger. And select PrintIt.
- Record
the answer here:_______________
SUBMIT THIS PAGE FILLED OUT ON WEBWORKS BY 2400 FRIDAY 1
SEPTEMBER.