COM 1204 Object-Oriented Design - Spring 2001 - Assignment #2 -- Textbook work

Professor Futrelle -- College of Computer Science, Northeastern U., Boston, MA

(Version of 4/15/2001)

Problems based on your textbook


These problems are based primarily on Chapters 3, 5 and 6 in your texbook.

For the problems below, you're welcome to write and execute code to explore them. Of course on tests you'll have to be ready to answer questions such as these without the aid of a computer (or your textbook). Also, you may want to refer to any extra Java book(s) you have or the online textbooks or other Java info (the API) in answering the questions.

1. If a string is immutable, explain how it is possible to execute the following statements? What "happens to" the string "abc"?

String s = "abc";
s = s + "def";

2. For a Java variable v of type Vector:

  1. What is the purpose of the "(String)" construct in the expression,
    String t = (String) v.get(1);

  2. Why is it legal to write v.add("xyz") but not v.add(36)? How can the value 36 be accommodated in the vector?

  3. On the other hand, why is the expression v.remove(36) legal and what does it accomplish?

3. Redraw the diagram shown after the following statements are executed. In particular, what is the status of the string "summer" at the end of this sequence of statements?

m = x;
x = a;
y = b + "s";
// k is passed to a function that doubles all odd element values
b = "abc";

4. After reading the following paragraphs, write out four groups of statements for the mythical class Climb (as in a mountain climb or hike) and for each function, corresponding to the specifications for OVERVIEW, REQUIRES, EFFECTS and MODIFIES, statements that correctly capture the content of the description in the paragraphs. Also, comment as to whether the descriptions correspond to any underdetermined behavior, to use your textbook's terminology. You may need to make explicit certain implicit constraints, such as the inappropriateness of having a negative number of items.

"The class Climb is handy for planning various climbs for mountains in its database. You can load just the parts of the DB you need into your laptop and haul it with you wherever you go. Just don't take it in your whitewater raft! And in addition (since you're a CS type) you'll need to build some other code for this class to use it with any convenience. Luckily there's functionality in this class to produce HTML descriptions with refs to the stored images in the right places."

"One of the procedures is alternate(), which when given a Route (produced by getRoute()) will return a vector of alternate Routes. The first argument is a Route, the second the maximum of alternatives wanted and the third, an integer from 1 to 10 for the desired difficulty of the alternate routes. The procedure will limit the alternatives to 5. If alternate routes are few, the procedure may return Routes with alternate starting points or even, occasionally, alternate endpoints, e.g., an easy route not reaching the summit."

"Another procedure is addDetails() which, when given a Route will alter it by adding further details, more images, etc., in the Route argument. It will make no changes to a Route that already has details added."

5. Assuming you have a Vector of Strings, create an Iterator for it and write a simple for loop that uses the Iterator to loop through the elements of the vector, printing them out one per line. Hint: Vector is a Collection type so you don't have to define your own next() and hasNext() methods.

6. Sketch part of an implementation of a ConnectionSet by doing the following:

  1. Define a Connection record with two integer fields, caller and callee.

  2. Define a class ConnectionSet, very similar to IntSet but with Connection records as its elements instead of ints and Integers, and write out the implementation of the insert() and getIndex() methods for this class.

Go to COM1204 home page

Return to Prof. Futrelle's home page