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

JED Coweb Assignment 3

Method Dispatch (1 point)
Explain the difference between method overloading (in statically-typed languages) and multiple dispatch? In your explanation, include example code to demonstrate the differences.

Method overloading involves having more than one method for a particular class with the same name but differing arguments. It is used to customize the operation of a method based on the types of the arguments. For example, consider the following overloaded functions:
Public Function MyFunction (str as String)
...'do code based on having a string as an argument
End Function

Public Function MyFunction (int as Integer)
...'do code based on having an integer as an argument
End Function

Overloading functions based on the type of objects is limited when compared to how robust multiple dispatch can be. Typically, the functions are instance methods operating on objects of a certain class. Therefore, the operation of the function is determined by the argument types AND the receiving object's type. Multiple dispatch lets you have a master function of sorts that operates on a list of arguments of certain types, and its operation is dependent on which types are used. It is hard to see an example of this without having a language that supports multiple inheritance to demonstrate on, as much of the benefits and flexibility created by multiple dispatch are not fully realized unless this type of inheritance is used. In most languages, overloading functions and using static typing is flexible enough for most things.

History of OO (1 point)

What was the first object-oriented language? What two elements, kept separate in structured programming, did it combine?

Programming (1 point)
Write a Squeak method that contracts a string to a certain size by inserting an ellipsis (…) in the middle of the string. So, contracting ‘I am the Walrus’ to a size of 11 equals ‘I am…lrus’ (4 beginning characters + 3 periods + 4 ending characters = 11 total characters). Your method should deal with special cases, such as when the string is short enough that it doesn’t need to be contracted. This code should not just function properly, but use good object-oriented style. To that end, be sure to identify the class in which the method is implemented and whether it is an instance or a class method.

The following method is an instance method, implemented in the string class
"this method returns a string of length 11 or less.  If the instance is 11 or fewer characters, the instance is returned, else the instance has three elipses inserted into the middle of the string, replacing all but the first 4 and last 4 characters."
self size < 11 ifTrue: [ ^self] ifFalse: [
	tempString := (self copyFrom: 1 to: 4 ), ('...'),( self copyFrom: (self size-3) to: (self size)).

Link to this Page