Docsity

UML, Java, Unit Testing and Inheritance - Exam 1 | CS 3331, Exams of Computer Science

Toggle sidebar
bg1
1
Fall 2006 (Thursday, October 12) Name:
CS 3331 — Advanced Object-Oriented Programming
Exam 1 on UML, Java, Unit Testing, and Inheritance
This test has 5 questions and pages numbered 1 through 11.
Reminders
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).
1. (10 points) Briefly answer the following questions:
(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?
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download UML, Java, Unit Testing and Inheritance - Exam 1 | CS 3331 and more Exams Computer Science in PDF only on Docsity!

Fall 2006 (Thursday, October 12) Name:

CS 3331 — Advanced Object-Oriented Programming

Exam 1 on UML, Java, Unit Testing, and Inheritance

This test has 5 questions and pages numbered 1 through 11.

Reminders

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).

  1. (10 points) Briefly answer the following questions:

(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?

  1. (20 points) Draw a UML class diagram consisting of three classes: Curriculum, Course, and Lecture. Your class diagram should reflect all the following design decisions: - A course is composed of zero or more lectures. - A curriculum contains (or consists of) one or more courses. All the courses of a curriculum can be accessed from the curriculum, but not the other way around. - A course may have other courses as prerequisites. - The Course class has the following field and methods: protected String name public String getName() public void setName(String name)

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.)

  1. (total 50 points) This problem consists of three subproblems and is about writing an animation applet and enhancing it by introducing a subclass. You are to write an applet class and several helper classes whose design and partial implementation are given (see Figure 1). The applet paints a balloon, a filled circle, at the center of the viewing area. Initially the balloon is completely deflated (i.e., not visible), but it grows delta pixels in radius at every delay milliseconds. In subproblems (a)–(c) below, you are to fill in the implementation of the classes, some of which skeleton code is provided.

<> Balloon

<> Balloon BalloonAppletBalloonApplet^ balloon

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;

/**

  • The growing rate of this balloon’s radius. The radius grows
  • delta pixels every time the draw
  • method is called.
  • @see #draw(Graphics, int, int) */ protected int delta = 2;

/** The color of this balloon. */ protected Color color;

/**

  • Creates a new balloon at the given position and with the given
  • color. The initial size of the balloon is 0. */ public GrowingBalloon(int x, int y, Color color) { this.x = x; this.y = y; this.color = color; }

/**

  • Increases this balloon’s radius by delta pixels
  • and draws it on given the graphics g. The
  • balloon’s position and color remain the same. */ public void draw( // WRITE YOUR CODE HERE ...

} // End of GrowingBall

  1. (extra credit: 20 points) Write an applet class, named AlarmClock, that behaves like the DigitalClock class (see the code below) except that an alarm rings at the specified time by playing an audio file alarm.au. The alarm time is specified by three applet parameters: hour, minute, and second. The audio file is assumed to be located in the same directory where the applet is located. Your applet should download the audio file only once throughout its lifetime. Hints: Use the AudioClip getAudioClip(URL url, String name) method of the class Applet to retrieve the audio file, and the void play() method of the class AudioClip to play the audio.

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); } }

DEFINE YOUR CLASS HERE ...