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

M5 - M8

M5


Problem Description:

Write a class method severalChunkyNumbers in class Integer which returns a collection containing the first 100 chunky numbers which are greater than 0. You may use any kind of collection you wish.

Solution:

severalChunkyNumbers

| ourCollection |

ourCollection _ OrderedCollection new.
17 to: SmallInteger maxVal do: [ :aNum | aNum isChunky ifTrue: [ ourCollection add: aNum ]. ourCollection size > 99 ifTrue: [ ^ourCollection ]]


M6


Problem Description:

Examine the following methods in class StringHolder: #contents and #acceptContents:. Write some code that creates a StringHolder and opens three PluggableTextMorph's viewing the contents of the StringHolder. If the user modifies any of the PluggableTextMorph's and chooses "accept" (alt-s), then all of the other PluggableTextMorph's should be updated with the new value.

Solution:

1.Modify StringHolder->#acceptContents:
acceptContents: aString

self contents: aString.
self changed: #contents.
^true



2.Creates some PluggableTextMorphs with a StringHolder as their model, then add them as dependents.

|ourString pluggies|

ourString _ StringHolder new.
ourString contents: 'something good'.
pluggies := (1 to: 3) collect: [ :ignored | PluggableTextMorph on: ourString text: #contents accept: #acceptContents: ].
pluggies withIndexDo: [ :pluggie :index | pluggie position: (index*100)@(index*100) ].
pluggies do: [ :pluggie | pluggie openInWorld ].



or, to save those extra few keystrokes:

|ourString|

ourString _ StringHolder new.
ourString contents: 'something good'.
(((1 to: 3) collect: [ :ignored | PluggableTextMorph on: ourString text: #contents accept: #acceptContents: ]) 
withIndexDo: [ :pluggie :index | pluggie position: (index*100)@(index*100) ]) do: [ :pluggie | pluggie openInWorld ]



M7


Problem Description:

Create a class named Counter with one instance variable name count. Add the following methods to it:

count – returns the current value of count.
countString – returns the current count, but printed to a string.
increaseCount – increase the count by one.
openUI – open a PluggableTextMorph that reflects the value of the receiver's count, and a PluggableButtonMorph that, when pressed, will cause increaseCount to be
called on the receiver.

For full credit, you must use a Model-View architecture. This means that you will need to add a call to changed: somewhere, so that the UI updates at the right time.

Solution:

Instance Methods:


count

	^count

countString


	^self count asString

increaseCount

	count _ count + 1.
	self changed: #countString

openUI

	
	((PluggableTextMorph on: self text: #countString accept: nil) position: 100@100) openInWorld.
	((PluggableButtonMorph on: self getState: nil action: #increaseCount) position: 50@50) openInWorld.

initialize

	count _ 0


Class Methods:

new

	^super new initialize



M8


Problem Description:

Write a method secondSmallest in class Collection which returns the second smallest element of the collection. Use
#<
to decide whether one element is smaller than another.

Solution:

secondSmallest

^self asSet asSortedCollection at: 2


Note: This assumes that the number of uniqute elements in the set representing the collection in question is strictly larger than 1. To correct for cases where this is not true:

secondSmallest

self asSet size < 2 ifTrue: [ self error: 'Undefined behavior' ].
^self asSet asSortedCollection at: 2


This solution doesn't use the less-than-sign "to decide whether one element is smaller than another". What's up?

It's not explicit, but it's there. The default sort block for a new sortedCollection is: [:x:y | x <= y], which isn't a strict less than. However, converting our collection to a set makes strict and nonstrict less than comparisons behave in the same manner. Shaggz

That is so lame. In CS2130, CS2200, CS1312, even CS1311 when the assignment says "use 'xxx'" they never mean "use something that looks a little like 'xxx'". This is Georgia Tech. We never assume the requirements allow for broad interpretation of intent. The assignment said "use #less-than to determine...." and that's what we should do. If we were free to use some built in method that kind of used the less-than operator, then the assignment shouldn't have spelled out what we should or shouldn't have used. Your post on the newsgroup said "there's a VERY easy way, using #less-than, to do this in one line". Don't post something like that unless you have the code to back it up.

^(self asSet asSortedCollection: [:x :y | x < y ]) at: 2


Hmm? Now, the only difference here is that less than is explicit rather than being implied to the equivalent less than or equal. This really is a meaningless addition. The major project in this class leaves room for all kinds of interpretation, and if you don't assume freedom to build your movie editor in almost any fashion that you choose, but rather in a single specified manner, they would all look and feel about the same. I believe that would take a great deal of the enjoyment of creating something of your own right out. And please, use whatever builtin functionality that you please. It serves to save you time and effort. In 2330, even though rewriting the string functions is interesting and pretty fun, it may not be to your benefit to do so during one of the timed labs (ok, that was 2331). Shaggz

Fair enough. Switching gears from Greenlee to Spoon is like getting out of a bulldozer and driving a BMW. –Jay
True =) Shaggz
The statement was there simply because "smallest object" can have multiple interpretations; perhaps the most obvious is "takes the least amount of memory", which is very different from what was intended! -Lex Spoon
Though wouldn't that be a handy way of sorting? Shaggz

^((self collect: [ :index | index class ]) asSet asSortedCollection: [ :first :second | first spaceUsed < second spaceUsed]) at: 2


Something like this maybe? Although this doesn't really compare the size of the instances in question, only a "rough estimate" of the size of the classes, but still, it's kind of neat. Shaggz And it's all I feel like doing at this hour.



Link to this Page