![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
MP3Homework 3.
Part 1. Blocks and control structures revisitedIn 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.
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.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. 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 superclassesThis 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.
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 AdviceTake 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. DeliverablesFor 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. ExamplesSqueak'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
|