CS 3371: Test 2
April 19, 2000
Suppose that class Derived is a subclass of class Base, and that method farkle is defined in both Base and in Derived. Given this definition of the preconditions and postconditions of farkle in Base:
int farkle(n: int)
// pre: n > 0
// post: result > log n
explain why each of the following preconditions or postconditions of farkle in Derived would be valid or invalid. (Recall that this refers to the current object of class Derived, and assume that isFull is a Boolean method of Base.)
- // pre: n > 0 OR n < -1
(3 points)
Valid: demands less because it allows more inputs to be accepted.
- // pre: n > 0 AND !this.isFull()
(3 points)
Invalid: demands more because it allows fewer inputs to be accepted.
- // post: result >= log n
(3 points)
Invalid: promises less because it allows a wider range of results to be returned.
Suppose that we have a Point class to use in developing a Rectangle class, and that we are trying to decide whether to use inheritance, aggregation, or composition to implement the relationship between Point and Rectangle. For each of the three types of association, explain why we would use it, or why not. (9 points: 3 + 3 + 3)
Inheritance: A bad choice because a Point is not a Rectangle. Fails the principle of substitutivity.
Aggregation: Possible because a Rectangle has a Point. A good choice if we assume that a Point may exist independently of a Rectangle, say by belonging to more than one shape.
Aggregation: Again, possible because a Rectangle has a Point. A good choice if we assume that a Point may not exist independently of a Rectangle, so that a Point is created with and dies with its Rectangle.
Redraw this class diagram, adding another class to eliminate the xor constraint. (9 points)

Two possible solutions:
Add a JournalCopy class that is associated with Journal while Copy is associated with Book. (We might then also change Copy's name to BookCopy and add a new abstract base class called Copy that BookCopy and JournalCopy extend.)
Add an abstract LibraryItem class that is the parent of Book and Journal and that is associated with Copy.
Suppose that a product may be manufactured at several factories, and that a factory may manufacture several different products, so that there is a many-to-many relationship between the Product and Factory classes. Draw a class diagram to represent the association in which a product is manufactured at a factory on a particular day. (9 points)

or
Suppose that a C object sends a getMyB message to an A object, and uses the returned reference to a B object to call foo(). How does this violate the Law of Demeter? How could we change the design to fix the problem? (9 points: 5 + 4)

This violates the Law of Demeter because the C object uses the return value of the getMyB method to access the B object directly. Instead, we could replace the getMyB method in A with method
foo(c : C) : int
that simply delegates responsibility for "foo-ing" to the proper B object, shielding C from all knowledge of B.
In UML interaction diagrams, how do we represent ...
- A message to be sent n times? *[i := 1..n] msg( ) (2 points)
- The condition x > 0 on a message? [x > 0] msg( ) (2 points)
Is sending e-mail from a program synchronous or asynchronous message passing? Why? Give an example to illustrate your answer. (9 points)
Sending e-mail from a program is asynchronous. The sender does not wait for acknowledgement from the receiver. It sends the mail, then continues processing.
For example, consider a CGI script that sends mail to recognize form input and sends a web page back to the browser. The script sends the mail and then immediately transmits the web page, without waiting to see if the mail message got to the recipient or to get a response to it.
Consider the grocery store system we've used as a running example. Suppose that during checkout processing, a BarCodeScanner user interface object sends a "create a sale line item" message to a Sale object. What design pattern does this violate? Why is this bad design? How could we improve it? (10 points: 2 + 4 + 4)
This violates the Controller pattern by forcing a user interface object to take responsibility for a problem domain operation. In doing so, we are mixing user interface and domain logic. If we later replace the scanner with another input device, we will have to rewrite and retest the code that sends the message to the Sale object. A better solution would be to have a use case controller for the Checkout use case get input from the scanner and send the "create a sale line item" message to the Sale object.
Design an interaction among objects in which NOT using nested message numbering would create ambiguity. Demonstrate the ambiguity by drawing two collaboration diagrams to represent the interaction, one with and one without nested message numbers (11 points)
Many are possible. For example, have an A object send message 1 to a B object, and the B object message 2 to a C object. Then redraw it with the messages numbered 1 and 1.1.
Draw a state diagram for a telephone. A telephone is either idle or active. Initially it is idle. When the user takes the receiver off the hook, the phone becomes active. While active, the phone initially plays a dial tone. When the user enters a digit, the phone stops playing the dial tone to begin dialing, and continues dialing until the user has entered a complete phone number. When the number is complete, the phone starts connecting to the called phone. When the connection is made, the phone is connected. When the connection is terminated, either because the user hangs up or because the line is broken, the phone stops being active and returns to idle. (12 points)

Suppose that a Person can be associated with many Banks, but that given a Bank and an account number, there is at most one Person with that account number at that Bank. Draw a UML class diagram to show that relationship. (9 points)