Halfway through the semester, our team suffered a crisis unlike any suffered before! Poop was everywhere! After taking two weeks off of class time without working on Smalltalk (compounded by the fact that individual members had difficulty dealing with their independent Smalltalk assignments), we ended up in an unproductively frustrating situation – meetings during which nobody remembered how to do anything.
To solve that dilemma, we constructed a list of fundamental procedures we needed to understand by the end of the first two independent coding assignments. In particular, this means Smalltalk/VisualWorks basics, testing your code in a workspace, and integrating the GUI. In this guide, I’m going to present the list and walk you guys through it (in comments, if you need the extra assistance).
So, without further ado...
Let us start off Smalltalk again from the basics and hopefully get to our next meeting a lot more comfortable with our abilities. Here’s a quick checklist of things we should be able to do with Smalltalk. Try to follow it, to make sure you are comfortable with programming/doing all of it please (if you want to make sure you really understand the code, try renaming things).
[Note: There are screen captures of the VisualWorks windows at the bottom of the case.]
A1. Making a new package. Inside, make a namespace: Rabbits.
“From the basics: open up VisualWorks, press F5 (Browse>System), then right-click on the leftmost panel to open a new package. For the namespace, right-click the second panel and then go to new.”
A2. Make two classes: Bunny and Farm. Include both in the namespace.
“The second panel handles the new classes.”
A3. Bunny has instance variables: name and bunnyID.
“Click on your class. On the tabs above the third and fourth panels, select Instance Variable. Add the variables in here. Remember to right click on each of them to create accessors. In Bunny’s default initialize method, default the variables to something.”
A4. Farm has a SortedCollection variable bunnyList that holds instances of Bunny and sorts them according to their bunnyID.
“Add the instance variable, and then go to Farm’s default initialize method. Then add the following line of code after you call super initialize’
bunnyList:= SortedCollection sortBlock: [:a :b| a bunnyID b bunnyID].”
A5. Bunny has a printString method that returns a string: ‘(name) tagged (bunnyID).’
“Create the new method that has
^(self name printString, ‘ tagged ’, bunnyID printString).”
A6. Farm has a printString method that returns a string in the following format (mind the linebreaks):
‘Farm has these bunnies:
(name) tagged (bunnyID)
(name) tagged (bunnyID)
(name) tagged (bunnyID)
“Hint: to return a string for the above, I would use this code:
str := ‘Farm has these bunnies:\(name) tagged (bunnyID)\(name) tagged (bunnyID)\(name) tagged (bunnyID)\etc.’
B1. Testing your code. Open a new workspace in VisualWorks (Tools > Workspace).
“This is back in the main window.”
B2. Create an instance of Farm and 3 instances of Bunny. Name the Bunny variables and change one’s cuteness to 3 and another’s to 2. Add the Bunny variables to Farm’s ‘bunnyList’ collection. (If you are getting errors for your classes, remember to add the namespace).
“Use the following code:
B3. At the end of your code, type ‘(Farm variable) printString.’ Highlight all of your code, click the ‘Print it’ button. Does it print correctly?
“If not, review the previous steps.”
C1. Create a new GUI. It has an “Input Field” at the very top, a ‘List’ in the middle, and five ‘Buttons’ at the bottom. This GUI represents a Farm. For the input field, go to its Details tab and remove the border – this is a trick to create dynamically updating labels. The list will be holding the elements of your ‘bunnyList’ and will update when you add/remove/update items. The buttons are labeled: Create, Read, Update, Remove, and Exit. Install the GUI and place it in your package.
“Smalltalk has a drag-and-drop GUI maker. To access it, click Painter > New Canvas. The best way to understand the GUI maker is to explore it yourself. Once you’ve designed it, you need to install it onto a class. To do this, in the GUI Painter Tool window, and go to Edit > Install. Install into a new class, and then pull that new class into your Rabbits package.”
C2. Input field has an aspect ‘numOfBunnies.’ Define the aspect in the GUI window, and its aspect methods should appear in the GUI class.
“Each object in the Painter has attributes that can be defined/edited in that GUI Painter Tool.”
C3. Create an instance variable ‘farmville’ and create accessors for it. Do the same for three instances of Bunny. Write a new method:
numOfBunnies := (bunnyList size printString, ‘ bunnies at the farm’) asValue.
farmville := Rabbits.Farm new.
‘here, create three instances of Bunny; give them names and IDs’
“Note that this refers to the GUI class you just created.”
C4. List has an aspect ‘theBunnies.’ Define the aspect in the GUI window, and its aspect methods should appear in the GUI class. Go to your Initialize method and include the following code.
“The define button is in the GUI Painter Tool window, under Edit. Every time you update the aspects for objects you attached to your GUI, you need to click this button again to redefine their attributes to your Smalltalk class.”
C5. Create a new method: addBunny. The GUI button ‘Create’ links its action to addBunny. The method opens up a new dialogue request for a name and a bunnyID. Add the new Bunny to bunnyList. Remember to add the following code at the end of the method:
farmville changed: #bunnyList.
“this dynamically updates the list”
“If you’re having difficulty remembering the code, it’s the following:
C6. removeBunny removes the selected Bunny. readBunny opens up a warn dialogue that displays a Bunny’s printString. updateBunny updates a Bunny’s name and ID. Remember to dynamically update the list.
“Warn dialogues are written via – Dialog warn: 'string displayed here’.
To determine which bunny is selected for your update, use the following code:
todoList selection notNil ifTrue: [updatingbunny:= theBunnies selection. “more code goes here; you want to call dialogue requests to define your variables, and calling your update method in the Bunny class.”].”
C7. The exit button closes the application with a warning dialogue that says: ‘fin.’
“See C6’s comment for tips on creating a warning dialogue. Congratulations, you’re finished!”
Link to this Page
Cases last edited on 30 July 2011 at 2:33 am by r59h132.res.gatech.edu