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

Darryl D. Prince CoWeb Assignment 3

Programming (this should have been worth more than 1 point like 2? but maybe there was an easier way and I just did too much)

shortenTo: newOddLength
"This method is an Instance method in the String class."

"This method returns a MyString of the length given with as close to an even number of characters from the original MyString at the beginning and end with an elipse (...) in the middle. If the string does not need to be shortened it returns the original string."

| blockSize newSelf inc |

"check if this string needs to be shortened. and can be"
((self size >= newOddLength) & (newOddLength >= 5))
ifTrue: [
newSelf := MyString new: newOddLength.

(newOddLength even)
ifFalse: [
"do this if the desired length is an even number of characters"

"determines thenumber of characters on either side of the elipse."
blockSize := ((newOddLength - 3)/2).

"copy the characters before the elipse"
1 to: blockSize do: [: i |
newSelf at: i put: (self at: i)
].

"copy the elipse"
(blockSize + 1) to: (blockSize + 3) do: [:i |
newSelf at: i put: $.
].

"copy the last blockSize characters"
inc := 0.
(blockSize + 4) to: newOddLength do: [:i |
newSelf at: i put: (self at: (self size - (blockSize - inc) + 1)).
inc := inc + 1.
]
] ifTrue: [
"do this if the desired length is an even number of characters"

"determines thenumber of characters on either side of the elipse."
blockSize := ((newOddLength - 4)/2).

"copy the characters before the elipse"
1 to: blockSize do: [: i |
newSelf at: i put: (self at: i)
].

"copy the elipse"
(blockSize + 1) to: (blockSize + 3) do: [:i |
newSelf at: i put: $.
].

"copy the last blockSize characters"
inc := 0.
(blockSize + 4) to: newOddLength do: [:i |
newSelf at: i put: (self at: (self size - (blockSize - inc))).
inc := inc + 1.
]
].
^newSelf.
].

"If the string is already short enough or the desired length is more than 5 characters (the maximum number you can have with an elipse in the middle and characters on both sides) then return the original string."
^self.


Object-Oriented Programming (2 point)
1. Objects do the work. The work is done in instance methods in the appropriate class.
This means that Objects in well designed and executed Object Oriented Programs perform the operations that need to be performed on them themselves. There's no factorializer because Small Integer does factorial itself. Anything that can be done to an object is done by the object or another object with that object sent to the method. (as in 1 + 4 the work is done in 1 with 4 sent as a parameter)

2. Good objects have clear responsibilities and clear interfaces
By "Good objects" we mean well designed Object Oriented Objects.
Clear responsibilities means that it is obvious what they are responcible for. in the case of numbers they are responcible for adding subtracting and all other mathematical operations. They do not have secret responcibilities like capitolizing characters passed to them nor do they have dependant responcibilities in other classes that they are not subordinant to. ie Morph doesn't calculate the absolute value of numbers for them.

Clear interfaces means that methods don't expose the way things are implemented in the code (the objects are treated as black boxes). for example there's no lowerLeftCornerMover for a rectangle. There is a size: instead. maybe an x and y, width and height but nothing that would confuse the user as to what exactly it would do if called. this means that methods have simple names and also responsibilities that stand alone. ie you don't change width and then have to tell the rectangle to redraw itself seperately.


Link to this Page