View this PageEdit this PageUploads to this PageVersions of this PageHomeRecent ChangesSearchHelp Guide

MP3


Blocks and control structures

Homework 3.

It is very important to learn to find things in the class library. These exercises will help teach you that. They require looking around with the browser. So, you need to learn to find the implementation of a method with a particular name, the variables defined by a class, and so on. Squeak has excellent tools for this, but you can't use them until you learn them.

Part 1. Blocks and control structures revisited

In this part, you will be writing small methods in Squeak. As in the previous homework, we are providing a set of test cases, linked at the bottom of the page. Studying them should help clarify the exercises below. You will be graded on the number of passing test cases. We reserve the right to add more tests to our suite.
For each exercise below, write a method specified in the description. Make sure to name your methods as specified. Create a new class and place all methods inside that class. How to name this class? See the provided TestCase subclass to find out.

Exercise 1.1. Write a method blockExpression. Write a block expression which increments a temporary variable index by one. Make this block a return value of method blockExpression.

Exercise 1.2. What is returned from an empty block? (Try [] value)

Exercise 1.3. A very simple 'repetition' control structure using a block may be achieved using the timesRepeat: message sent to an Integer. Write a method repetition: anInteger to calculate the sum of the integers 1 to anInteger.

Exercise 1.4. Rewrite the following example (Exercise 1.8.1 in HW1) adding to sum until it reaches the minimum number higher than 100, using (a) whileTrue:, and (b) whileFalse: methods. Put your code in methods usingWhileTrue and usingWhileFalse respectively.
 

| incrementBlock sumBlock sum index |
incrementBlock := [index := index + 1].
sumBlock := [sum + (index * index)].
sum := 0.
index := 1.
sum := sumBlock value.
incrementBlock value.
sum := sumBlock value.
sum

Exercise 1.5. Write a method maxArray: anArray which returns the maximum value in an array of numbers, anArray.

Exercise 1.6. Write a method reverseArray: anArray which reverses the order of elements in an array, anArray, and returns it.

Exercise 1.7. Write a method calculateOddSumFrom: anInteger1 to: anInteger2, which sums the odd integers in the range provided. Assume valid input. Use a timesRepeat: loop. (Hint: look in the testing messages of class Integer ).

Exercise 1.8. Write a method to calculate the squares of the integers from 1 to 10, using a whileTrue: loop. Call this method listOfSquaresFrom1To10. Return the results as an array. The array needs to be initialized with the correct number of elements, so set all of them to 0. Note: this is not the right way to do this is Smalltalk, but we haven't discussed collections and streams yet.

Exercise 1.9. Write a method arrayOfIntegers which, for all odd integers < 20 calculates the number which is (the factorial of twice the integer) divided by the square of the integer. Place the results in an array and return it following the approach from the previous exercise.

Part 2. Abstraction and superclasses

This is writing portion. Answer each question in 1 or 2 sentences.

Exercise 2.1.The numeric system of Squeak is quite extensive and flexible. It contains several useful number classes and a general conversion mechanism to perform operations on different subclasses of Number.
Open an Inspector on an instance of Float. Send the message + 3.0 to self. What is the result? Try again, but use super rather than self.

Exercise 2.2. Why is raisedTo: implemented in the mathematical functions protocol of Number, even though it performs differently depending on the class of the argument? How does raisedToInteger work?

Exercise 2.3. Bit manipulation methods are defined for Integer (e.g. bitAnd:), and redefined in SmallInteger. Why?

Exercise 2.4. What is returned from the expression rect1intersect: rect2 if the areas of rect1and rect2 do not overlap?

Exercise 2.5. Where is Rectangle's implementation of the "new" method? (This might be harder than you think!) Smalltalk is ideal for experimentation. If you want to know whether something will work, just try it!

Comments:

Squeak offers a "pretty print" feature, which formats the code according to ST standards. Right-click on the code window, select more... and "pretty print with color."



CS598rej-Grading HW3.st

Advice


Take a look at inject: into: and see where you can use it.
Also, be careful when you are modifying any array in place. Sometimes it is best to operate on a copy of the array.

For Exercise 2.2 you just need to give me a short desription of how it works. You do not need to write the entire algorithm in full detail.

For Exercise 2.5 you might want to read this chapter to get an understanding of how the object model is implemented.

Deliverables


For this exercise, you can just fileOut the class and send me the text contents directly as e-mail i.e. do not send as an attachment but just paste the text in.

For Part 1, I will assume that you have run the tests and that they pass. I am going to look at the code and offer some comments on different ways to write it, style, etc.

For Part 2, I will be reading your explanations.

To do a fileOut, open a browser, find the BlocksControl class, select it with the middle mouse button and select fileOut.

I would appreciate it if you created a new category to store all the methods that you implemented. That way I can quickly search for that category.


Examples


Squeak's raisedTo: methods are a dog! IMHO, it has many bad code smells. It has unnecessary type checks and is generally ugly. Worst of all, it creates an unnecessary type separation of raisedTo: and raisedToInteger: in the interests of "efficiency". All it does however is pollute the protocol.

For fun, I have included a refactoring of the raisedTo: methods. Here are the three change sets to file in:

raisedTo-UnitTests.cs
raisedTo-refactoring.cs
raisedTo-updated-calls.cs

Please let me know what you think!

Maurice Rabb



Link to this Page

  • Machine Problems last edited on 14 May 2008 at 4:25:53 pm by c-98-212-224-168.hsd1.il.comcast.net