






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, October 12) Name:
CS 3331 — Advanced Object-Oriented Programming
This test has 5 questions and pages numbered 1 through 11.
This test is open book and notes; but no laptop computers, PDAs, calculators, or similar devices are allowed. However, it is to be done individually, and you are not to exchange or share materials with other students during the test. If you need more space, use the back of a page, noting this 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. For programs and diagrams, clarity is important; if your programs or diagrams are sloppy or hard to read, you will lose points. Correct syntax also makes some difference. There are 120 points all (including 20 extra credit points).
(a) Explain the life-cycle of applets by describing when applet methods such as init, start, stop, and destroy are invoked by a web browser or an applet viewer.
(b) In Java, when is a type (a class, an interface, and an array) a subtype of another type?
You should use the standard UML notation, especially for field and method declarations, and justify your decisions choosing particular kinds of relationships (association, aggregation, and composition).
// WRITE YOUR TEST CLASS HERE ... (It’s okay to omit boiler-plate methods // such as constructor, suite, and main.)
<
<
GrowingBalloonGrowingBalloon
GrowingShrinkingBalloonGrowingShrinkingBalloon
Figure 1: Design of the balloon applet.
// File: BalloonApplet.java import java.awt.*; public class BalloonApplet extends AnimationApplet {
private Balloon balloon;
public void init() { super.init(); balloon = createBalloon(); }
private Balloon createBalloon() { //return new GrowingBalloon(dim.width/2, dim.height/2, Color.GREEN); return new GrowingShrinkingBalloon(dim.width / 2, dim.height / 2, Color.GREEN); }
protected void paintFrame(Graphics g) { balloon.draw(g, dim.width, dim.height); } }
// File: AnimationApplet.java import java.awt.; import java.awt.event.; import javax.swing.*;
public abstract class AnimationApplet extends java.applet.Applet {
protected int delay = 10; protected Timer timer; protected Dimension dim; protected Image image; protected Graphics offscreen;
public void init() {
(a) (10 points) Write the interface Balloon shown in Figure 1. (Hint: How is the interface used by the class BalloonApplet?)
(b) (20 points) Write the class GrowingBalloon that implements the interface Balloon by filling in the following skeleton code. (Hint: To draw a balloon, use the method void fillOval(int x, int y, int width, int height) of the class Graphics.)
import java.awt.; public class GrowingBalloon / YOUR CODE HERE -> */ _________________________ {
/** The x and y coordinates of this balloon. */ protected int x, y;
/** The current radius of this balloon. */ protected int radius = 0;
/**
delta pixels every time the draw/** The color of this balloon. */ protected Color color;
/**
/**
delta pixelsg. The} // End of GrowingBall
import java.applet.; import java.awt.; import java.awt.event.; import javax.swing.; import java.util.Calendar;
public class DigitalClock extends Applet {
protected Timer timer; protected Font font = new Font("Monospaced", Font.BOLD, 48); protected Color color = Color.GREEN;
public DigitalClock() { timer = new Timer(1000, createTimerTickHandler()); }
protected ActionListener createTimerTickHandler() { return new ActionListener() { public void actionPerformed(ActionEvent event) { repaint(); } }; }
public void start() { timer.start(); }
public void stop() { timer.stop(); }
public void paint(/@ non_null @/ Graphics g) { Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); g.setFont(font); g.setColor(color); g.drawString(hour + ":" + minute / 10 + minute % 10 + ":" + second / 10 + second % 10, 10, 60); } }