Royal Military College of Canada
Department of Electrical and Computer
Engineering
EE573 - Object Oriented Analysis and Design
Dr G.S. Knight
Lab 5
Association Relationships and Dependency
Objective
The main objective of this lab is to implement a short program from a
design that is given to you. The program you will be implementing is a
stopwatch that responds to basic commands, like run/stop and reset.
Given high-level design diagrams and the code that implements some
of the
classes, you will have to recognize the relationships between the
classes in the design and complete the implementation of the
program.
Background
Figure 1 presents a high-level class diagram for the classes that will
have
to be implemented in your stopwatch program. As shown, a stopwatch is
composed
of a timer, a display, and a controls object.
Figure 1: High-Level Class Diagram - Stopwatch Program.
Your stopwatch will have to display the time and respond to keyboard
input
simultaneously (to stop the timer, reset it, etc). The
StopwatchClient class in the figure is representative of any
class
which is to make use of a Stopwatch
. The client initially have the main tread of control. It will create a
new
instance of a stopwatch and then activate the stopwatch by sending a
run
message to the stopwatch timer. The timer will obtain the state of the
controls
from the TimerKeys object and interpret this state; if the timer is
running,
it will also send a message to the display to update the time, if
necessary.
The TimerKeys object will
verify
if certain keys on the keyboard have been pressed; if so, the state of
the
keys will be saved accordingly.
Figure 2: Object Collaboration Diagram, Stopwatch Program.
Your stopwatch will behave as follows and in accordance with figure
2:
- if the timer is stopped, pressing on the SPACE bar will engage
the
timer and the running time will be updated every second;
- if the timer is engaged, pressing on the SPACE bar will stop it;
- pressing again on the SPACE bar (when the display is not reset)
re-engages the timer from the exact point in time where it was stopped
(you may have stopped the watch part way through counting a second);
- pressing on the ENTER key when the timer is stopped will reset
the
timer and the display; and
- pressing on the ENTER key when the timer is already reset will
terminate the program.
Procedure
The source code for the implementation of classes
TimerDisplay
and TimeKeys
is given to you. You may not alter the files given to
you.
Using the source code provided, you are asked to complete the stopwatch
program. Note that class TimerKeys
and
TimeDisplay use the Java AWT GUI library. It is not
necessary
for you to understand how these classes work. You only need to
understand
how the public interface works (i.e. the methods
TimerKeys.getSpace(), TimerKeys.getEnter()
, TimeDisplay.setTime(), and
the
construcors). Try to be efficient in implementing your timer. You
should
be accurate to at least 100 ms.
You must pay particular attention to the class relationships and
object relationships that are imposed upon your implementation. Study
Figures 1
and 2 carefully. The association and dependency relationships are
to
implemented as indicated.
You will want to look at the class java.lang.System.currentTimeMillis()
. You can get the current time by creating a new date object with
no
parameters.
Laboratory Report
As your lab report, you must submit a complete source listing for your
stopwatch
program (no need to include the classes that are provided to you). Your
code
must be documented with intelligent comments to ensure it is
understandable
and facilitate its marking.
Question 1. In the current design the user starts the program
by
sending a run() message to the
timer
object. This method could also be available in the class
Stopwatch. Is this useful?
Question 2. You main()
method
is in the class StopwatchClient.
There is also a main() method in each of the
TimerKey and TimeDisplay classes.
Why is this? does this make sense?
Question 3. Create
a collaboration diagram (similar to that in figure 2) that explains the
object-interactions involved with the set-up (creation of the objects
and the web of visibilities). That is, a scenario that describes
everything that happens before the run() method is called.
Deliverables
- Source code for each file you
create.
- Your lab report
– as per the course outline must be submitted with an introduction,
responses to the questions, and a discussion and conclusion.
- A zipped electronic copy of
the above files submitted by e-mail.