CS246 Software Abstraction and Specification ination Spring 2007 Date: 04-Aug-2007 Time: 4.00 6.30pm Permitted Aids: None 14 pages Student Name: UW Student ID: Instructions: (Read carefully before the exam begins): 1. Before you begin, make certain that you have one Exam Booklet with pages numbered 1 14 printed double-sided. 2. The marks assigned to each question are shown at the beginning of the question; use this information to organize your time effectively. Question out of 1 10 2 15 mark marker s initials 3. Place all your answers in the spaces provided on these pages. 4. You do not need to write comments in your code unless it is specifically required by the question. 3 4 10 10 5. Questions will not be interpreted. Proctors will only confirm or deny errors in the questions. If you consider the wording of a question to be ambiguous, state your assumptions clearly and proceed to answer the question to the best of your ability. You may not trivialize the problem in your assumptions. 6. Cheating is an academic offense. Your signature on this exam indicates that you understand and agree to the University s policies regarding cheating on exams. 5 20 6 10 7 20 Total 95
Question 1. (10 marks) General Read each of the following statements carefully. For each statement, circle T if the statement is true and circle F if the statement is false. T / F In UML, a subclass inherits all of its parent class's OCL constraints. T / F In C++, if a class has pointer data members, then the class's destructor method should always delete those members' referenced objects. T / F In C++, all calls to virtual methods are resolved, to the specific function called, at runtime. T / F In C++, a const function is a function that does not modify any of its parameter variables. T / F In C++. nonmember functions are defined outside of any class definition. T / F In object-oriented programming, inheritance and object composition both promote code reuse. T / F In object-oriented programming, language constructs such as classes and objects realize designs that have good cohesion and loose coupling. T / F The primary purpose of Design Patterns is to ease the task of implementing a program. T / F In interface specifications, adding a precondition weakens a specification. T / F In general, the ideal program is one in which all packages are completely stable (i.e., have a stability measure of 1). Aug 4, 2007 Page 2 of 14 CS 246 S07
Question 2. (15 marks) C++ 2a. (10 marks) Pointers Provide a C++ implementation that satisfies the following function interface and specification. Be sure to avoid potential memory leaks and segmentation faults. The Supplemental Materials sheets includes a list of operations on STL vector containers. #include <vector> void DeepCopyVector (const std::vector<int*> *v1, std::vector<int*> *v1copy) // modifies: contents of vector *v1copy // ensures: vector *v1copy is a vector of pointers to new integer variables. Each element in // *v1copy points to a new integer variable whose value is the same as the variable // pointed to by the corresponding element in v1. The size of *v1copy equals the // size of *v1. Aug 4, 2007 Page 3 of 14 CS 246 S07
Question 2. [continued] 2b. (5 marks) Polymorphism The following set of program fragments define a base class, a derived class, and a main program that uses both classes. The main class includes 10 method calls. For each method call, indicate in the comment following the call how that call is resolved: 1. Base::aMethod() 2. Base::bMethod() 3. Base::cMethod() 4. Derived::aMethod() 5. Derived::bMethod() 6. Derived::cMethod() 7. Derived::dMethod() 8. none of the above - compiler error 9. none of the above - runtime error Each method call is followed by a comment and label, where you should write your answer. In your answers, you may simply use the above number labels rather than writing out the entire answer. For each method call, assume that all previous statements were successful (i.e., did not result in compiler or runtime errors). class Base { public: void amethod(); virtual void bmethod(); virtual void cmethod(); } class Derived : public Base { public: void amethod(); virtual void bmethod(); virtual void dmethod(); private: virtual void cmethod(); } int main (void) { Base *b = new Derived(); b->amethod(); b->bmethod(); b->cmethod(); b->dmethod(); Derived d; d.amethod(); d.cmethod(); d.dmethod(); Base b1 = d; b1.amethod(); b1.bmethod(); b1.cmethod(); // (a) // (b) // (c) // (d) // (e) // (f) // (g) // (h) // (i) // (j) Aug 4, 2007 Page 4 of 14 CS 246 S07
Question 3. (10 marks) UML This question asks you to evaluate a UML class diagram and to enhance the model with appropriate UML structural constructs. The class diagram models key parts of an inventory and transactions system for a small coffee and doughnut shop called the C&D. The C&D sells a number of different types of Products. Each Product is identified by a unique product number (prodid). Product numbers are automatically generated when new Products are added to the inventory (via method newprodid()). The inventory system keeps track of how many items of each Product the C&D currently has in stock; it also is responsible for notifying the C&D manager when the instock quantity of a Product falls below a specified threshold, so that the manager can order more. The C&D buys its products from Suppliers. For each Product, the inventory system keeps track of all the Suppliers from which the shop can buy that Product. Some Products can be purchased from more than one Supplier. The inventory maintains a record of each PurchaseOrder that is made, including the Supplier, the quantities of Products ordered, their costs, taxes on the order (PST and GST), and the total cost of the order. The C&D sells food to customers who walk in the door. There is no attempt to keep records of customers. But the system does maintain a record of each Sales transaction, including the Products purchased, the taxes on the Sale, and the total bill of the Sale. The Sale of a Product causes the system to immediately update its record of how many items of that Product the C&D has in stock. Supplier 1..* 1 prodid 1 C&D + order (prodid, int) - newprodid : prodid 1 * Sale PST : double GST : double total cost : double 1 * * prodid 1 1 * 1..* PurchaseOrder PST : double GST : double total cost : double * Product instock : integer saleprice : double threshold : integer + sale (int) + delivery (int) * quantity : integer purchaseprice : double 3a. (5 marks) UML Structures, Navigability Based on the above description of the program and what information needs to be recorded and maintained, detail each of the association ends in the above model with structure (composition, aggregation) and with navigability. (There is only one C&D shop, so all multiplicities on associations to the C&D class are 1). Aug 4, 2007 Page 5 of 14 CS 246 S07
Question 3. [continued] The UML model given on the previous page is not necessarily the best model of the C&D inventory and sales transaction system. Below are questions about two different modelling decisions that need to be made. For each question, say which modelling decision is better and explain why. For your convenience, the UML model is duplicated in the Supplemental Materials sheets. 3b. (3 marks) Association Classes In the model on the previous page, a PurchaseOrder is modelled as an ordinary class. Alternatively, we could have modelled PurchaseOrder as an association class of the relationship between the C&D and a Supplier. Supplier 1..* 1 prodid C&D + reorder (prodid, int) PurchaseOrder PST : double GST : double total cost : double Which modelling decision is better for this program and why? (Hint: think about the definition of an ordinary association.) 3c. (2 marks) Qualifiers In the model on the previous page, product numbers are modelled as association qualifiers. Alternatively, we could have modelled product numbers as attributes of the Product and Supplier classes. Which modelling decision is better for this program and why? Aug 4, 2007 Page 6 of 14 CS 246 S07
4. (10 marks) Implementing UML Associations This question is based on the annotated UML model that you created in Question 3a, including navigation and structure. 4a. (5 marks) Provide a partial C++ class declaration for the C&D class that realizes the class's associations with Product and Supplier. The class declaration must be complete in declaring all data members, function members, and access specifiers that are needed to implement the class and the two associations to Product and Supplier (do not include data or function members to realize the class's other associations). 4b. (5 marks) Provide a C++ definition for the C&D method to add a new Product. The definition of the method addproduct(...) must realize the two associations' multiplicity and navigation constraints. Use representative names in your declarations. The Supplemental Materials sheets includes partial lists of operations for several STL containers. If you need to, you can assume that the type prodid supports comparison with operator<. Aug 4, 2007 Page 7 of 14 CS 246 S07
Question 5. (20 marks) Exceptions and Specifications The following question refers to the IntVector class defined (partially) on the last page of the Supplemental Materials sheets for this exam. In this question, you are to rewrite the class declaration for IntVector to include declarations of the exceptions that might be thrown by the IntVector's methods and to include a proper interface specification. For each condition that could cause a run-time error in one of IntVector's methods, identify the likely standard library exception that would be raised when that condition occurs. (Don't worry about identifying which of two similar exceptions might be thrown for a particular condition. Simply state your assumptions). All exceptions that are raised in the IntVector class will be passed back to the calling code; none will be handled in the IntVector class. Rewrite below the C++ class declaration for the IntVector, so that (10 marks) Each method declaration specifies the exceptions that can be raised in that method, and (10 marks) The class declaration includes as comments an interface specification (contract) Aug 4, 2007 Page 8 of 14 CS 246 S07
Question 6. (10 marks) Design Patterns (multiple choice) 6a. (4 marks) (match each Design Pattern on the left with its description on the right) (a) Composite Pattern (b) Decorator Pattern (c) Strategy Pattern (d) Template Pattern 1. Creates a single interface for a collection of objects 2. Creates a single interface for a collection of operations that can operate on specific types of objects. 3. Creates a single interface for a collection of algorithms that access the members of a collection of objects. 4. Creates a single interface for a collection of algorithms that can be used interchangeably to "implement" an object's method. 5. Create a single interface for a collection of features (extra functionality) that can be added to an object 6. Modifies the interface of an object (without adding functionality) 7. Creates interfaces for steps of an algorithm (ensures that subclasses execute the same algorithm, but can specialize the steps) 6b. (2 marks) (circle the most relevant answer) The Factory Method is useful for (a) encapsulating the construction of a concrete object. (b) constructing decorated objects with the right decorators. (c) constructing the appropriate iterator for a composite object. (d) all of the above 6d. (2 marks) (circle the two most relevant answers) The Observer Pattern promotes which of the following design principles: (a) program to an interface, not to an implementation (b) Liskov substitutability principle (c) dependency inversion (d) principle of least knowledge (e) don't repeat yourself (f) open-closed principle 6e. (2 marks) (circle the two most relevant answers) The Visitor Pattern promotes which of the following design principles: (a) program to an interface, not to an implementation (b) Liskov substitutability principle (c) dependency inversion (d) principle of least knowledge (e) don't repeat yourself (f) open-closed principle Aug 4, 2007 Page 9 of 14 CS 246 S07
Question 7. (20 marks) Design Patterns (application) This question asks you to use two design patterns to extend the following UML design. For your convenience, the model below is duplicated in the Supplemental Materials sheets. InternetFlix is an internet-based DVD rental company. Customers can ask about the availability of movies and can request to borrow movies (up to 2 movies at a time). The company mails the DVDs out to the customer, and the customer mails the DVDs back when he is done with them. The rental period is four weeks. There is a fee for renting a particular movie (movies have different fees based on whether they are new releases). There is a late fee (assessed daily) for not returning a movie before the end of the rental period. InternetFlix checkavail(movie) : boolean checkout(movie) checkin(move) 1 0..1 * Movie title : string onloan : boolean rentalfee: Money latefee: Money loanperiod: duration 1 * Account balance : Money borrow (Movie) return (Movie) 0..1 0..2 0..1 Loan duedate: Date Aug 4, 2007 Page 10 of 14 CS 246 S07
Question 7. [continued] 7a. (10 marks) Decorator Pattern Use the Decorator Pattern to revise the UML model to offer a Subscription service. If a customer subscribes to this service, then instead of asking to borrow movies one at a time, the customer, as part of this service, maintains a queue of movies that he is interested in renting. Whenever the customer has fewer than two movies out on loan (e.g., whenever a movie is returned), the company finds an available movie that is on the customer's request queue and sends the movie to the customer. With this service, there is no rental fee per loan, no loan period, no late fee. Instead, the customer pays a fixed monthly fee for the privilege of always having two movies out on loan. Your revisions to the UML model should include any new classes, data members, and methods to (1) realize the Decorator Pattern and (2) realize the new Subscription decorator. Aug 4, 2007 Page 11 of 14 CS 246 S07
Question 7. [continued] 7b. (10 marks) Visitor Pattern Use the Visitor Pattern to revise the original UML model to allow for the easy addition of operations that operate over all the loans. Include in your revised model a new visitor operation OutstandingFines that computes the total amount of outstanding fines for all overdue movies. Your revisions to the UML model should include any new classes, data members, and methods to (1) realize the Visitor Pattern and (2) realize the new OutstandingFines operation. Aug 4, 2007 Page 12 of 14 CS 246 S07
Extra Space for Any Question Aug 4, 2007 Page 13 of 14 CS 246 S07
Extra Space for Any Question Aug 4, 2007 Page 14 of 14 CS 246 S07