Docsity

Advanced Object Oriented Programming - Final Exam 2006 | CS 3331, Exams of Computer Science

Toggle sidebar
bg1
1
Fall 2006 (Thursday, December 7) Name:
CS 3331 — Advanced Object-Oriented Programming
Final Exam
This test has 6 questions and pages numbered 1 through 12.
Reminders
This test is open book. You may also bring one page (8.5 ×11) of notes (both sides). Your notes must be
your own, and they must be hand-written and turned in with your test. This test is to be done individually,
and you are not to exchange or share your notes with others during the test.
If you need more space, use the back of a page. Note when you do that on the front.
This test is timed. Your test will not be graded if you try to take more than the time allowed. Therefore,
before you begin, please take a moment to look over the entire test so that you can budget your time.
Clarity is important; if your writing or code is sloppy or hard to read, you will lose points. Correct syntax
also makes some difference.
There are 110 points all, including 10 points of extra credits.
1. (5 points) Answer briefly the following questions.
(a) One of the most distinguishing characteristics of object-oriented application frameworks is “in-
version of control”. Explain what the inversion of control means.
(b) Explain the benefits of design patterns.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Advanced Object Oriented Programming - Final Exam 2006 | CS 3331 and more Exams Computer Science in PDF only on Docsity!

Fall 2006 (Thursday, December 7) Name:

CS 3331 — Advanced Object-Oriented Programming

Final Exam

This test has 6 questions and pages numbered 1 through 12.

Reminders

This test is open book. You may also bring one page (8.5 × 11) of notes (both sides). Your notes must be your own, and they must be hand-written and turned in with your test. This test is to be done individually, and you are not to exchange or share your notes with others during the test. If you need more space, use the back of a page. Note when you do that on the front. This test is timed. Your test will not be graded if you try to take more than the time allowed. Therefore, before you begin, please take a moment to look over the entire test so that you can budget your time. Clarity is important; if your writing or code is sloppy or hard to read, you will lose points. Correct syntax also makes some difference. There are 110 points all, including 10 points of extra credits.

  1. (5 points) Answer briefly the following questions.

(a) One of the most distinguishing characteristics of object-oriented application frameworks is “in- version of control”. Explain what the inversion of control means.

(b) Explain the benefits of design patterns.

  1. (total 20 points) In this problem you are to define a class Product that describes the products sold by an Internet shopping mall. The products may have many attributes, but in this problem you will consider only two attributes: the manufacturer (of type String) and the price (of type int). In the subproblems below you will define two different orderings for the products so that the costumer can browse or list the products in different orders, e.g., sorted by manufacturers or by prices.

(a) (10 points) Define the class Product. The Product class should implement the Comparable interface to define a natural ordering on the products. The natural order of the product objects is based on the ascending order of the manufacturer and price attributes. That is, a product, p 1 , precedes a product, p 2 , if the manufacturer’s name of p 1 precedes the manufacturer’s name of p 2 in the lexicographic order. If both p 1 and p 2 are from the same manufacturer, the product with a lower price precedes the other.

public class Product implements Comparable { private String manufacturer; private int price;

public Product(String manufacturer, int price) { this.manufacturer = manufacturer; this.price = price; }

public String getManufacturer() { return manufacturer; }

public int getPrice() { return price; }

// >>>WRITE YOUR CODE HERE<<<

} // end of class Product

(b) (15 points) Define the SieveSet class by completing the skeleton code given below. You also need to write, in JML, the pre and postconditions for the add method. (Caution: The return type of the add method is boolean.)

public class SieveSet extends HashSet {

/** Returns true iff the given object o falls through. / protected /@ pure @*/ boolean fallsThrough(Object o) { return Math.random() < 0.5; }

/**

  • Adds the given object o to this set if it is not already present
  • and o does not fall through; otherwise, do not add the given object.
  • Returns true if the object o was successfully added to this sieve set. / /@ >>>WRITE YOUR PRE AND POSTCONDITION HERE<<<

public boolean add(Object o) { // >>>WRITE YOUR CODE HERE<<<

(c) (10 points) Write a new class called ControllableSieveSet that behaves like a sieve set except that one can now specify the percentage that added items fall through the sieve. For example, new ControllableSieveSet(30) creates a new sieve set in which 30 percents of added items fall through. You can assume that a value between 0 and 100, inclusive, is passed as an argument to the constructor. You also need to provide a default constructor that creates a new sieve set in which half of the added items fall through. If you have duplicate code, you will be penalized for that. (Hint: You only need to define two simple constructors and one simple method. The static method Math.random() returns a random double number between 0 and 1.)

public class ControllableSieveSet extends SieveSet {

/** The percentage that the added items fall through. */ private int percentage;

// >>>WRITE YOUR CODE HERE<<<

/** Creates a new instance by composing its GUI. */ public PhotoViewer() { JButton prevButton = new JButton("<Prev"); JButton nextButton = new JButton("Next>");

// >>>WRITE YOUR CODE HERE<<< // (Hint: refer to part (b) of this question that define // the PhotoAlbum class.)

/** Downloads pictures and adds them to the album. */ public void init() { // >>>WRITE YOUR CODE HERE<<< // (Hint: refer to part (b) of this question.)

(b) (15 points) Define the PhotoAlbum class used by the PhotoViewer applet class.

import java.awt.; import javax.swing.; import java.util.List; import java.util.ArrayList;

public class PhotoAlbum extends JPanel {

/**

  • The list of pictures to display. Each element is supposed to be
  • an object of type Image. */ private List photos = new ArrayList();

/** The index of the current picture to be displayed. */ private int index = 0;

public PhotoAlbum() { super(true); // enable double-buffering }

/** Paints the current picture by scaling it. */ public void paint(Graphics g) { if (photos.size() > 0) { Dimension d = getSize(); Image p = (Image) photos.get(index); g.drawImage(p, 0, 0, d.width, d.height, Color.WHITE, this); } }

/** Adds the given image to this album. */ public void addPhoto(Image image) { // >>>WRITE YOUR CODE HERE<<< // (Hint: one line of code should be enough.)

// MORE CODE ON THE NEXT PAGE.

  1. (total 15 points) In this problem you will write a simple timer class, called MyTimer, that fires one or more action events after a specified delay. For example, an animation object can use this class as the trigger for drawing its frames. Each timer has one or more action listeners and a delay (the time between action events). When delay milliseconds have passed, the timer fires an action event to its listeners; this cycle repeats forever.

(a) (10 points) Complete the MyTimer class below by adding the missing part.

public class MyTimer {

/** A list of listeners that have registered interest for

  • notification on this timer. */ private List listeners = new ArrayList();

/** A background thread that manages this timer’s tick and fires

  • action events after a specified delay. */ private Thread timerThread = null;

/**

  • Creates a timer that will notify its listeners every delay
  • milliseconds. The delay is assumed to be a positive
  • number. If listener is not null, it’s registered
  • as an action listener on the timer. */ public MyTimer(final int delay, ActionListener listener) { addActionListener(listener);

// event to be fired after every delay milliseconds. final ActionEvent e = new ActionEvent(this, 0, "time out");

// >>>WRITE YOUR CODE HERE<<<

} // end of the constructor

/** Adds an action listener to this timer. */ public void addActionListener(ActionListener listener) { if (listener != null && !listeners.contains(listener)) { listeners.add(listener); } }

/** Removes the given action listener from this timer. */ public void removeActionListener(ActionListener listener) { listeners.remove(listener); }

/**

  • Starts this timer, causing it to start sending action events
  • to its listeners. */ public void start() { timerThread.start(); }

/**

  • Notifies all listeners that have registered interest for
  • notification on this timer. */ protected void fireActionPerformed(ActionEvent e) { for (Iterator i = listeners.iterator(); i.hasNext(); ) { ((ActionListener) i.next()).actionPerformed(e); } } }

(b) (5 points) Which design pattern does the above code use? Justify your answer.