
Name __________________________________________
Answer 9 of the following questions. Indicate which of the questions you wish to be graded. Answer
essay questions as briefly as possible.
1. Assume we have a method called trimBlanks() on the String class that removes all leading and
trailing spaces. That is it repeatedly removes the first character of the string as long as it is a
space and repeatedly removes the last character of the string as long as it is a space. Write XUnit
(where X = J, R, S, Cpp) test(s) for the method. C++ people can assume that all C++ strings are
an instance of the String class.
2. The following methods are from classes B & C that have a common superclass A. Rewrite the
methods using the Template method. Show all the methods you would create and which classes
they would be in.
Class B
Key keyFrom(HttpRequest aRequest) {
if (aRequest.identifier().size() < depth() ) {
if (aRequest.hasPostDataAt( "Command"))
return aRequest.postDataAt("Command");
else
return defaultKey();
}
if (containsAction(matchingElement(aRequest)) {
aRequest.decodeFormData();
}
return matchingElement(aRequest);
}
Class C
Key keyFrom(HttpRequest aRequest) {
if (aRequest.identifier().size() < depth() ) {
return defaultKey();
}
return matchingElement(aRequest);
}
3. Explain
A. Information Hiding
B. Encapsulation
C. Abstraction
4. We now have three different ways to produce a list of all odd valued integers in a linked list. Select
two of the methods and discuss the pros and cons of each way.
5. In object-oriented programming programers are told to avoid if (case) statements and replace
them with polymorphism. What is gained by using polymorphism and what is lost by using poly-
morphism.
5/14/09!CS 635 Midterm!Page 1 of 2