Vous êtes sur la page 1sur 11

Please write your name only in the last page.

Fall 2005 Final Exam Sample Solutions


CS 319-02,03 Object-Oriented Software Engineering
Instructor: Uğur Doğrusöz
• Time: 120 minutes.
• Write your name and sign only in the last page as
indicated.
• Show your work and reasoning clearly and write
legibly, only within the space provided for each
question.
• You may use scratch sheets that are not to be
submitted.
• Please stay seated if you finish during the last 10
minutes of the exam period.
Q1 15
Q2 15
Q3 25
Q4 35
Q5 10
Total 100 pts

CS 319, CS Dept., Bilkent University Page 1 of 11


Please write your name only in the last page.

Question 1: Fill in the blanks [15 pts]


Fill in the blanks below, with a word or two per blank, to form valid statements.

(a) As part of the Unified Process, Inception is a feasibility study which envisions the product scope,
approximate vision and business case. Elaboration generates a refined vision, iterative
implementation of core architecture, resolution of high risks, identification of most requirements and
scope.

(b) Polymorphism denotes the ability to manipulate objects of distinct classes using only knowledge of
their common properties without regard for their exact class.

(c) Cloning, copying code from one place to another is not a form of reuse.

(d) Design patterns are recurring solutions to software design problems you find again and again in real-
world application development.

(e) The currently accepted view is that software engineering is, and should be, a highly iterative process.

(f) According to GRASP, when object-orientation is used in software design, we assign class B the
responsibility to create an instance of class A if one of these is true: B contains A; B aggregates A.

(g) One essential idea of using layers in architecting software is that collaboration and coupling
(dependency) is from higher to lower layers; coupling in the other direction is avoided.

(h) A subsystem or module has high cohesion if it keeps together things that are related to each other,
and keeps out other things.

(i) An association in a UML class diagram is a relationship between classes (more precisely, instances
of those classes) that indicates some meaningful and interesting connection.

(j) System sequence diagrams are more likely to be used in the analysis phase during OO A/D.

(k) UML is a language and notation for specification, construction, visualization and documentation of
models of software systems.

CS 319, CS Dept., Bilkent University Page 2 of 11


Please write your name only in the last page.

Question 2: OO Programming [15 pts]

Keeping in mind the principles of OO,

(a) Write (in your favorite OO language) a class SavingsAccount for implementing a savings account
in a banking system. Each savings account has an account number (an integer) along with double
values for the current balance (in dollars) and interest rate (percentage) of the account. A savings
account should be able to be instantiated by either specifying its account number, initial balance, and
rate or by specifying only its account number in which case balance and rate respectively default to
0.0 and 5.0. In addition, implement methods for depositing and withdrawing to and from this
account with specified amounts. Another method is to calculate and add interest
(balance=balance*(100+rate)/100) to the current balance when executed.
public class SavingsAccount {
protected int no;
protected double balance;
protected double rate;

public int getNo() { return no; }


public double getBalance() { return balance; }
public double getRate() { return rate; }

public SavingsAccount(int ino, double ibalance, double irate) {


init(ino, ibalance, irate);
}
public SavingsAccount(int ino) {
init(ino, 0.0, 5.0);
}
private void init(int ino, double ibalance, double irate) {
no = ino; balance = ibalance; rate = irate;
}

public void deposit(double amount) {


if (amount > 0)
balance += amount;
}
public void withdraw(double amount) {
if (amount > balance)
balance -= amount;
}
public void addInterest() { balance += balance*(100+rate)/100; }
}

(b) Write a class BonusSaverAccount which implements a bonus savings account that earns extra
interest, but there is a penalty for withdrawals. Extra interest is a fixed value of 0.5 percent but
penalty, which is the amount deduced on a withdrawal and defaults to 20 dolars, could be changed.
Similarly, a bonus savings account should be able to be instantiated by either specifying an account
number, initial balance, interest rate and penalty amount or by specifying only the account number in
which case all other data members are to be set to default values.

public class BonusSaverAccount extends SavingsAccount {


private final static double extraRate = 0.5;
private double penalty;

public double getPenalty() { return penalty; }


public void setPenalty(double ipen) { penalty = ipen; }

public BonusSaverAccount(int ino, double ibal, double irate,


CS 319, CS Dept., Bilkent University Page 3 of 11
Please write your name only in the last page.
double ipen) {
super(ino, ibal, irate + extraRate);
penalty = ipen;
}
public BonusSaverAccount(int ino) {
super(ino, 0.0, 5.0 + extraRate);
penalty = 20.0;
}

public void withdraw(int amount) {


super.withdraw(amount + penalty);
}
}

CS 319, CS Dept., Bilkent University Page 4 of 11


Please write your name only in the last page.

Question 3: OO A/D & UML [25 pts]


Consider the following description for a simple file system.

Entries in the file system are either files or folders or links to other entries. Files contain text. Folders
contain zero or more entries; all entries within a folder are uniquely named. The file system has a single
root folder; all other folders have a unique parent folder. Each link refers to at most one other entry; links
may be "broken" meaning that they refer to no entry. An entry is accessible to another if it is contained
within it, or if it is accessible via a contained link.

Operations to be supported include addition, renaming, copying and removal of entries from the file
system, recursive size queries (the recursive size of a folder is its own size together with the size of its text
files and the size of its subfolders), and recursive text search (which searches for particular text strings in
all text files accessible to a given entry).

A preliminary design for this system is given below.

(a) Assuming a complete implementation of the above design in your favorite programming language,
provide code needed to construct the following file system rooted at folder home:

/home/
teaching/
cs570/ Æ cs571/ // cs570 is a link to cs571
cs571/
cs571-desc.txt
exams/
cs571-final.txt

Folder home = new Folder(“home”);


Folder teaching = new Folder(“teaching”);
Folder cs571 = new Folder(“cs571”);
Link cs570 = new Link(“cs570”, cs571);
File cs571-desc = new File(“cs571-desc.txt”);
Folder exams = new Folder(“exams”);
File cs571-final = new File(“cs571-final.txt”);
Entry.setRoot(home);
home.add(teaching);
teaching.add(cs570);
teaching.add(cs571);

CS 319, CS Dept., Bilkent University Page 5 of 11


Please write your name only in the last page.
cs571.add(cs571-desc);
cs571.add(exams);
exams.add(cs571-final);

(b) Write the code necessary to find out the size of the directory rooted at folder teaching, followed by
a search for the string “Ugur” in directory teaching.

int sizeofteaching = teaching.size();


File filewithUgur = teaching.search(“Ugur”);

(c) An important scenario for the commonly used file management operations is searching for a
particular string in a given directory. Provide a detailed sequence diagram (i.e. clearly show all
actors, objects, classes and parameters involved), in line with the above design and assumed
implementation, for searching for the string “Ugur” in directory teaching. You may assume that
this string occurs only in file cs571-final.txt and the operation is triggered by the user via a File
Browser, which is a software component external to the File System under design.

CS 319, CS Dept., Bilkent University Page 6 of 11


Please write your name only in the last page.

Question 4: Design Patterns & UML [35 pts]


Consider the following C# code for implementing items in a library (books and videos) with ability to
dynamically add “borrowable” functionality:
// taken from http://www.dofactory.com
namespace DoFactory.GangOfFour.Decorator.RealWorld {

// MainApp test application


class MainApp {
static void Main() {
// Create book
Book book = new Book ("Worley", "Inside ASP.NET", 10);
book.Display();

// Create video
Video video = new Video ("Spielberg", "Jaws", 23, 92);
video.Display();

// Make video borrowable, then borrow and display


Console.WriteLine("\nMaking video borrowable:");

Borrowable borrowvideo = new Borrowable(video);


borrowvideo.BorrowItem("Customer #1");
borrowvideo.BorrowItem("Customer #2");

borrowvideo.Display();
}
}

abstract class LibraryItem { // "Component"


private int numCopies;

public int NumCopies {


get{ return numCopies; }
set{ numCopies = value; }
}

public abstract void Display();


}

class Book : LibraryItem { // "ConcreteComponent"


private string author;
private string title;

public Book(string author,string title,int numCopies) {


this.author = author;
this.title = title;
this.NumCopies = numCopies;
}

public override void Display() {


Console.WriteLine("\nBook ------ ");
Console.WriteLine(" Author: {0}", author);
Console.WriteLine(" Title: {0}", title);
Console.WriteLine(" # Copies: {0}", NumCopies);
}
}

CS 319, CS Dept., Bilkent University Page 7 of 11


Please write your name only in the last page.
class Video : LibraryItem { // "ConcreteComponent"
private string director;
private string title;
private int playTime;

public Video(string director, string title, int numCopies, int playTime) {


this.director = director;
this.title = title;
this.NumCopies = numCopies;
this.playTime = playTime;
}

public override void Display() {


Console.WriteLine("\nVideo ----- ");
Console.WriteLine(" Director: {0}", director);
Console.WriteLine(" Title: {0}", title);
Console.WriteLine(" # Copies: {0}", NumCopies);
Console.WriteLine(" Playtime: {0}\n", playTime);
}
}

abstract class Decorator : LibraryItem { // "Decorator"


protected LibraryItem libraryItem;

public Decorator(LibraryItem libraryItem) {


this.libraryItem = libraryItem;
}

public override void Display() {


libraryItem.Display();
}
}

class Borrowable : Decorator { // "ConcreteDecorator"


protected ArrayList borrowers = new ArrayList();

public Borrowable(LibraryItem libraryItem) : base(libraryItem) {}

public void BorrowItem(string name) {


borrowers.Add(name);
libraryItem.NumCopies--;
}

public void ReturnItem(string name) {


borrowers.Remove(name);
libraryItem.NumCopies++;
}

public override void Display() {


base.Display();

foreach (string borrower in borrowers) {


Console.WriteLine(" borrower: " + borrower);
}
}
}
}

CS 319, CS Dept., Bilkent University Page 8 of 11


Please write your name only in the last page.

Following is the output of the above code when the main function is executed to completion:
Book ------
Author: Worley
Title: Inside ASP.NET
# Copies: 10

Video -----
Director: Spielberg
Title: Jaws
# Copies: 23
Playtime: 92

Making video borrowable:

Video -----
Director: Spielberg
Title: Jaws
# Copies: 21
Playtime: 92

borrower: Customer #1
borrower: Customer #2

(a) Draw a UML diagram to explain the logical and static aspects of the architecture of the above code.

(b) The above code is an example application of a design pattern called Decorator Pattern, which is
described in detail below:

Decorator Pattern is used for adding additional functionality to a particular object as opposed to a class of
objects. It is easy to add functionality to an entire class of objects by sub-classing an object, but it is
impossible to extend a single object this way. With the Decorator Pattern, you can add functionality to a
single object and leave others like it unmodified.
A Decorator, also known as a Wrapper, is an object that has an interface identical to an object that it
contains. Any calls that the decorator gets, it relays to the object that it contains, and adds its own
functionality along the way, either before or after the call. This gives you a lot of flexibility, since you can
change what the decorator does at runtime, as opposed to having the change be static and determined at
compile time by sub-classing. Since a Decorator complies with the interface that the object that it
contains, the Decorator is indistinguishable from the object that it contains. That is, a Decorator is a
concrete instance of the abstract class, and thus is indistinguishable from any other concrete instance,
including other decorators. This can be used to great advantage, as you can recursively nest decorators

CS 319, CS Dept., Bilkent University Page 9 of 11


Please write your name only in the last page.
without any other objects being able to tell the difference, allowing a near infinite amount of
customization.

Describe the Decorator Pattern detailed out above using a UML class diagram.

(c) Give an anti-pattern for the Decorator Pattern.

Most common anti-pattern for the Decorator Pattern is sub-classing concrete components for each
extra functionality and/or data needed.

(d) Often in a domain model you find a set of related objects called occurrences. The members of such a
set share common information but also differ from each other in important ways. The Abstraction-
Occurrence Pattern offers a solution for representing such sets of occurrences avoiding the
duplication of common information.

Now suppose the library system above is to be modified to add unique “bar codes” to each copy of
each library item in the library. List and explain the necessary modifications to the above design. If
applicable, use the Abstraction-Occurrence pattern described.

We simply need to add a new class to represent the data & functionality unique for each copy and
associate with LibraryItem class using the Abstraction-Occurrence pattern described.

CS 319, CS Dept., Bilkent University Page 10 of 11


Please write your name only in the last page.

Question 5: Name [10 pts]


I hereby affirm that the work submitted in this examination is my own exclusively.

Name & Signature: Uğur Doğrusöz

CS 319, CS Dept., Bilkent University Page 11 of 11

Vous aimerez peut-être aussi