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

Discussion 4 - Daniel Engel

Java and squeak are similar in that they are object oriented and projects for each language can be designed in similar ways. The big differences are in syntax and working with the UI.

In Squeak you are going to create your classes in methods within the browser. This is opened in linux by pressing alt-b. You can also use the tools tab to drag it out. The tools tab should be on the right side of your screen. In the browser you can create a catagory on the left then create a class in the middle pane. Within your class you can then create methods on the right pane. This will be where you write your code. Lets go through a simple example. Make a class called Example and enter this code.

sayHello
	Transcript show: “Hello”. “System.out.print(“Hello”).


sayHello is your function name. Transcript is a class with a class(In java called static) method called show. This method takes in a string parameter and shows it on the transcript(Can be opened from tools tab). This can be used as a replacement for System.out.print(). Now lets run this code. Open a workspace from the tools tab. In it type the following.

myExample := Example new.    “myExample = new Example()”
myExample sayHello. “myExample.sayHello()”


Highlight this code and press alt-d in linux. This will 'do-it'. You should see Hello on your transcript. Lets try a more complicated example. Make another function in example.

factorial: aNum “int factorial(int aNum) You don't declare types in squeak, you can use a variable for whatever you want.”
	(aNum = 0) “if (aNum==0)”
	onTrue: [ ^1] “{ return 1}”	
	^ ((self factorial: (aNum-1) ) * aNum) “return this.factorial(aNum-1) * aNum”


Now in your workspace add

myExample factorial: 5 “myExample.factorial(5)”


Highlight this and press alt-p this will 'print-it' you should see 120.

Now lets try an array. I prefer to use OrderedCollections for my arrays as they are close to Java arrays in functionality, but there are many types to choose from.

sum: anArray “int concat(int [] anArray)” 
	|ret| “creates a temporary local variable sum”
	ret  =''.  “ ret=”” ”	
	anArray do: [:each | ret = ret  , each] “This is different and neat. This will go through each element of the array and assign it to each. Then ret , each concats the 2 strings”
	^ret “return ret”


Now lets run our new code. In the workspace type

Transcript show: (myExample concat: #(  “hey “ “how “ “are “ you” )). 
“This makes an array with the 4 strings”


Those examples should cover the basics of what is needed to go from java to squeak. Theres a lot more to learn but just go through the browser and read more code. It helps.




Link to this Page