(Mondays 2 PM)
(Fridays 2 PM)
| || 22 ||Jan
|| 25 ||Jan
|| 29 ||Jan
Programs that manipulate or process data are more user-friendly when
the data can be visualized. In this lab, you design and implement
a segment display---a graphics tool that can show a
sequence of segments.
When shown together, the segments comprise a picture---for example,
the growth of an NASDAQ stock, the components
of an airplane engine, or a freehand drawing.
Such a display could be driven directly
by keypresses or mouse actions, but then the display's use would be restricted
to those input devices.
Instead, we abstract the display's input so that it can be driven by
a variety of sources, including a mouse-click pad that you implement in
this lab. In later labs, the display might be driven by other objects
that you create.
As will be the case in most of our labs, you will build upon code that
is provided to you. Ideally, the API (Application Programming Interface)
tells you all you need to know to use this code. In this lab,
the segment display and the mouse-click pad are each based on
(separate instances of)
object, whose relevant features are described in this handout.
The system you design and build will contain at least the following
kinds of objects (classes):
- Objects of this type are constructed to hold a pair of
The object represents relative horizontal and
vertical displacement. In Physics, direction vectors
appear in free-body diagrams
to show components of a two-dimensional force.
Our convention for positive displacement is
to the right (for horizontal) and down (for vertical).
This object should be immutable, as discussed in class---once
constructed, its properties do not change.
This necessitates the
good software practice of accessor methods---methods that return
properties so that the object's values can be protected from accidental
- An object of this type displays a canvas for the mouse to click in.
For our purposes,
a mouse gesture consists of two clicks. The
should have a method that return the relative displacement between the
two clicks. This displacement is ideally encoded as
- An object of this type can display segments. The object should
contain a method that accepts a
specified vector is then drawn, starting at the location of the previous
Like its toy analog,
the display for Craf-ta-Graf
must contain code to keep the drawing from going off the
So far, three objects have surfaced in our design. Even in this simple
system, there are design questions whose answers significantly affect
the implementation at-hand as well as the possibility of reuse of the
classes you develop. For example, how do the
SegmentDisplay objects communicate? With respect
to object containment, there are three possibilities:
MousePad contains the
MousePad is in control. When a click is registered,
MousePad object synthesizes a
DirectionVector, passing it to the appropriate method in the
SegmentDisplay to update the display.
SegmentDisplay contains the
In this design, the display is in control, calling the
for the next click. A
DirectionVector is synthesized, and
the display is updated.
- Neither contains the other. Instead,
interaction between the
MousePad and the
SegmentDisplay is managed by a third party.
Read over this entire document before you start.
- Also, read the CS101
Style Guide. Beginning with Lab 2, all work you turn in should conform
to the recommendations in the CS101 Style Guide.
- Take a look at the API for the
and make sure you understand the parts that are described in this handout.
If you need help, please ask.
This API may give you an idea how to organize your APIs, in terms
of presenting each class's constructors, methods, and instance variables.
Your design need not look as nifty as what you see on the web pages---these
were generated automatically using
What to turn in:
- Explain how the
SegmentDisplay will interact. Think about the three possibilities described
above, and pick the one that promotes the cleanest design and greatest
possibility of code reuse.
- Provide a description of the extra classes present in your design. As you
design the API, you may discover the need for new types.
- Choose names and type signatures (that is, the API)
for the methods in the classes of your design. Pay attention to
- The names you use--they should be informative but not overly verbose.
- The style you use in spelling
- The extent to which your methods will have unclear side-effects.
- The width of the methods' interface.
For example, instead of passing a pair
of values around as separate parameters, you might invent an object to
represent such pairs. This reduces the chance of error (for
example, does the width
come first or the height?)
- Be sure to submit your design with the
design cover sheet properly filled out, including your primary TA's name.
- Be sure to submit the design by 2 PM on the Design Due Date at
the top of this handout.
You can construct graphics objects as in the following examples:
The constructor takes no parameters.
The add method takes a GraphicsObject as a parameter and puts
it on the canvas (draws it).
new Rect(20,15,30,60) creates a new Rect object with its upper
left corner at (x=20, y=15). The values 30 and 60 are its width and height,
respectively. When added to a canvas, this object would cause a 30 by 60
rectangle to be drawn on the canvas at position (20,15).
new Oval(20,15,30,60) creates an oval inscribed in the
rectangle with the given x, y, width, and height.
new Line(20,15,50,75) creates a new Line object with one endpoint
at (x=20, y=15) and the other endpoint at (x=50, y=75). When added to a
canvas, this object would cause a line to be be drawn on the canvas between
the two pixel locations.
Keeping in mind that coordinate (0,0) is at the upper left of the canvas,
and predict what the picture will
look like. Then
execute the program to confirm your ideas. If things look different than
you expected, try to find out why. Remember to ask for help as needed.
Note that all coordinates are in pixels, (a word
whose origin is "picture elements"). Think of a pixel as the smallest possible
dot on the screen. A typical television picture is 640 pixels wide and
480 pixels in height.
As an example of an API, and to assist you in developing your
solution in Lab, the API for the
DirectionVector class is as follows.
DirectionVector(int x, int y) constructs an immutable
direction vector with the supplied x and y components.
- Public Accessors
getX() returns the x component.
getY() returns the y component.
- Other Public Methods
returns a new direction vector that
is the vector sum of
this direction vector and
the supplied one.
double magnitude() returns the magnitude of
toString() represents the direction vector as a
- Public Fields
- Private Methods
- Private Fields
x is the horizontal component
y is the vertical component
- Exceptional behavior
Last modified 15:51:39 CST 21 January 1999
by Ron K. Cytron