Software Design (CS223 2004F)

Take-home Exam 1

Distributed: Wednesday, 27 October 2004
Due: 9:00 a.m., Monday, 8 November 2004
No extensions.

This page may be found online at



There are four problems on the exam. Some problems have subproblems. Each full problem is worth twenty-five points. The point value associated with a problem does not necessarily correspond to the complexity of the problem or the time required to solve the problem.

This examination is open book, open notes, open mind, open computer, open Web. However, it is closed person. That means you should not talk to other people about the exam. Other than that limitation, you should feel free to use all reasonable resources available to you. As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately.

Although you may use the Web for this exam, you may not post your answers to this examination on the Web (at least not until after I return exams to you). And, in case it's not clear, you may not ask others (in person, via email, via IM, by posting a please help message, or in any other way) to put answers on the Web.

This is a take-home examination. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date.

This exam is likely to take you about eight to ten hours, depending on how well you've learned topics and how fast you work. You should not work more than eleven hours on this exam. Stop at eleven hours and write There's more to life than CS and you will earn at least 80 points on this exam. I would appreciate it if you would write down the amount of time each problem takes. Including that information will earn you three points. I expect that someone who has mastered the material and works at a moderate rate should have little trouble completing the exam in a reasonable amount of time. Since I worry about the amount of time my exams take, I will give two points of extra credit to the first two people who honestly report that they've spent at least six hours on the exam or completed the exam. (At that point, I may then change the exam.)

You must include both of the following statements on the cover sheet of the examination. Please sign and date each statement. Note that the statements must be true; if you are unable to sign either statement, please talk to me at your earliest convenience. You need not reveal the particulars of the dishonesty, simply that it happened. Note also that inappropriate assistance is assistance from (or to) anyone other than Professor Rebelsky (that's me).

1. I have neither received nor given inappropriate assistance on this examination.
2. I am not aware of any other students who have given or received inappropriate assistance on this examination.

Because different students may be taking the exam at different times, you are not permitted to discuss the exam with anyone until after I have returned it. If you must say something about the exam, you are allowed to say This is among the hardest exams I have ever taken. If you don't start it early, you will have no chance of finishing the exam. You may also summarize these policies. You may not tell other students which problems you've finished. You may not tell other students how long you've spent on the exam.

You must both answer all of your questions electronically and turn in a printed version of your exam. That is, you must write all of your answers on the computer, print them out, number the pages, put your name on every page, and hand me the printed copy. You must also email me a copy of your exam by copying your exam and pasting it into an email message. Put your answers in the same order as the problems. Please write your name at the top of each sheet of the printed copy. Doing so will earn you two points of extra credit.

In many problems, I ask you to write code. Unless I specify otherwise in a problem, you should write working code and include examples that show that you've tested the code. You should also document your code.

Just as you should be careful and precise when you write code and documentation, so should you be careful and precise when you write prose. Please check your spelling and grammar. Since I should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify on this exam. I will limit that form of extra credit.

I will give partial credit for partially correct answers. You ensure the best possible grade for yourself by emphasizing your answer and including a clear set of work that you used to derive the answer.

I may not be available at the time you take the exam. If you feel that a question is badly worded or impossible to answer, note the problem you have observed and attempt to reword the question in such a way that it is answerable. If it's a reasonable hour (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (269-4410) or at home (236-7445).

I will also reserve time at the start of classes next week to discuss any general questions you have on the exam.


Problem 1: The Java Collections Framework

Topics: UML, reading documentation, collections, interfaces.

Using UML, diagram the inheritance and implementation hierarchy of the classes, abstract classes, and interfaces in the Java collections framework. Documentation on that framework is available at

You need not submit an electronic version of this problem.

Problem 2: Introspective Testing

Topics: testing, introspection

As you may have noted, JUnit assumes that you'll spend some time designing appropriate tests for your classes. Is it possible to automate some tests? For example, can we take a class that provides a variety of unary methods with integer parameters and test what range of integers they work on? Certainly. By using introspection, we can determine the names of those methods and ways to call them. We can then call each method with an appropriate variety of parameters (large negative, small negative, zero, positive, large positive, random) and see whether or not it succeeds.

Write a class, UnaryIntegerMethodTester, that aids in that kind of testing. The constructor to UnaryIntegerMethodTester should accept a Class as a parameter. (It should throw an exception if Class provides no nullary constructors.) UnaryIntegerMethodTester should provide two methods randomTest(int n) and structuredTest(). Each should do a variety of tests and print out the exception stack for each exception it finds.

The randomTest method should call each unary integer method in Class using a randomly generated number as the parameter. The structuredTest should try a consistent set of values (e.g., -2^16, -2^15, ... 2^16).

Problem 3: Refactoring

Topics: XP, refactoring, class design

Beck repeatedly tells us that regularly refactoring is a key aspect of extreme programming, but gives few, if any, concrete examples of refactoring. Give two useful examples of refactoring, one of methods and one of classes. These examples should be useful in the senses that (1) they help show what refactoring is and (2) they demonstrate benefits (in clarity, efficiency, correctness, or some combination thereof) from refactoring.

Problem 4: Building Factories

Topics: factories, introspection, anonymous inner classes

As you know, most Java classes provide a toString method, which converts an object to a string representation. Many classes therefore also provide a constructor that does the reverse. Such constructors take a string as the only parameter, parse the string, and build the object.

Classes that provide string-based constructors are an excellent context for factory methods. In particular, we might create a ParsingFactory interface that could then be used for a factory for any of those classes.

public interface ParsingFactory
   public Object parse(String s)
      throws Exception;
} // interface ParsingFactory

We can now fairly easily create our own factories. For example, here is a BigIntegerFactory.

public class BigIntegerFactory
   implements ParsingFactory
   public Object parse(String s)
      return new BigInteger(s);
   } // parse(String)
} // class BigIntegerFactory

However, it is so straightforward to create such factories that we should be able to write a method that builds them for us. Write a class, FactoryBuilder, that contains one static method, makeFactory(Class c), that, given a class with a parsing constructor, returns a ParsingFactory for that class.

We might use your methods as follows:

	ParsingFactory f = FactoryBuilder.makeFactory(Class.forName("java.math.BigInteger"));
	Object[] objects = new Object[args.length];
	for (int i = 0; i < args.length; i++)
		objects[i] = f.parse(args[i]);

Some Questions and Answers

These are some of the questions students have asked about the exam and my answers to those questions.

For problem 3, with refactoring, when you say give examples, do you want extremely specific examples such as "This is some code, this is what I would do to refactor it, this is how it would look after refactoring, this is why it is better", or would a more general answer be sufficient?
I want to see real code.
Although your now-repaired sample code assumes you can create a Class object by refering to the full name of the class, that strategy doesn't seem to work. What should I do?
Use Class.forName(nameofclass).
Can parse throw exceptions?
It can now.
How do I found out the class for int, given that int is a primitive type?
Try Class.forName("int").
What is the relationship of problem 2 to JUnit?
You should not use JUnit for problem 2. Problem 2 is to help you think about some of the things going on behind the scences in JUnit.


Here you will find errors of spelling, grammar, and design that students have noted. Remember, each error found corresponds to a point of extra credit for everyone. I limit such extra credit to five points.

False errors



Tuesday, 26 October 2004 [Samuel A. Rebelsky]

Sunday, 31 October 2004 [Samuel A. Rebelsky]


Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Dec 6 08:33:49 2004.
The source to the document was last modified on Wed Nov 3 08:23:36 2004.
This document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,