






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Fall 2006 (Thursday, December 7) Name:
CS 3331 — Advanced Object-Oriented Programming
This test has 6 questions and pages numbered 1 through 12.
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.
(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.
(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; }
/**
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 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.)
(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
/** A background thread that manages this timer’s tick and fires
/**
delay is assumed to be a positivelistener is not null, it’s registered// 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); }
/**
/**
(b) (5 points) Which design pattern does the above code use? Justify your answer.