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

Discussion 4 - Jimmy M. Espana


For Discussion 4, I chose to answer the following Midterm Review Question, entitled Write Smalltalk Code, from the Midterm Review - Summer 2002 page:

Write workspace code (something I can select and say DoIt in the workspace) to draw the below figure. Don't worry about exact sizes–just draw two rectangles with the text inside. (To remind you, Pens know how to up, down, go:, turn:, and north:. Strings know how to displayAt: a given point.)
Uploaded Image: cs2340.gif

To start off, we are going to need to run Squeak and open up a Workspace; most all of us know how to do this, therefore I won't waste time explaining how to do this. As is mentioned in the question above, we are probable going to need a Pen and a String objects. Let us draw the figure close to the upper left corner of the screen. First we must create the Pen object for drawing and have it up so that it won't draw during the set up. In the set up, we tell the Pen where we want it to start off, give it a direction (the turn), and a color. Now, we put the Pen down and start to direct it in the directions we want so as to draw the rectangle; very much like you would do when drawing on a piece of paper. The way we do the directing of the Pen, is by sending messages to the Pen object we created; these different messages maybe found in the System Browser which most of us know how to use. After drawing the first rectangle on the screen, we then need to do the same for the second rectangle. Now comes the String that will be place inside the two rectangles. We create the String object by assigning it a string and using the displayAt: method on the String object to place it inside the two rectangles. After this is done, we have accomplished the task given to us in this Midterm Review Question. The answer, that is the actual workspace code, to this Midterm Review Question is below. Note: there are many ways to code this problem.

thePen := Pen new. "creates a new Pen for drawing"
thePen up. "pick up Pen so it doesn't draw durning the set up"
"setting up the Pen to draw the 1st rectangle"
thePen goto: 10 @ 10.
thePen turn: 0.
thePen color: (Color black).
"put the Pen down and start drawing the 1st rectangle"
thePen down.
thePen go: -100.
thePen turn: -90.
thePen go: -200.
thePen turn: -90.
thePen go: -100.
thePen turn: -90.
thePen go: -200.
thePen turn: -90.
thePen up."pick up Pen so it doesn't draw durning the set up"
"setting up the Pen to draw the 2nd rectangle"
thePen goto: 20 @ 20.
thePen turn: 0.
thePen color: (Color black).
"put the Pen down and start drawing the 2nd rectangle"
thePen down.
thePen go: -75.
thePen turn: -90.
thePen go: -175.
thePen turn: -90.
thePen go: -75.
thePen turn: -90.
thePen go: -175.
thePen turn: -90.
"create the String to be displayed in the two rectangles and display it in the center"
theString := 'CS 2340'.
theString displayAt: 75 @ 50.


Return to the Jimmy M. Espana's CS 2340 Homepage Fall 2005 Who Who's Page.


Links to this Page