Java Reference

In-Depth Information

Subclass Parallelogram

We design the class for the first shape, class
Parallelogram
. This class is

a subclass of class
Shape
, which provides the position of the parallelogram.

We determine what is needed to define a parallelogram whose bounding rec-

tangle has upper-left corner
(x, y)
. A parallelogram has two horizontal sides of

length
l1
(say) and two vertical sides of length
l2
(say). But this does not define

how much the parallelogram leans. For this purpose, we use a value
d
, which we

call the
leaning factor
of the parallelogram. If
d
is at least
0
, the parallelogram is

as defined by the diagram on the left in Fig. 4.10, and if
d
is negative, it is as

defined by the diagram on the right. Coordinates
(x, y
), lengths
l1
and
l2
, and

leaning factor
d
completely determine the position, size, and shape of the paral-

lelogram.

We develop specs of the methods in class
Parallelogram
. See Fig. 4.11.

The constructor has as its parameters the five properties that define a parallelo-

gram. The class has procedure
drawShape
, which draws the parallelogram using

a
Graphics
object
g
. This procedure overrides
drawShape
in superclass
Shape
.

This ends the design of class
Parallelogram
.

Activity

4-4.2

(x,y) (x, y)

d
l1

l1

l2

l2

-d

Figure 4.10:

Defining a parallelogram in terms of
x
,
y
,
l1
,
l2
, and leaning factor
d

import
java.awt.*;

/**
A parallelogram that can be drawn.
*/

public class
Parallelogram
extends
Shape {

/**
Constructor: a parallelogram with horizontal length
l1
, other length
l2
, bounding

box with top-left corner
(x, y)
, and leaning factor
d

*/

public
Parallelogram(
int
x,
int
y,
int
l1,
int
l2,
int
d) { }

/**
Draw this parallelogram using
g */

public void
drawShape(Graphics g) { }

/** =
a description of this parallelogram */

public
String toString()

{
return
""; }

}

Figure 4.11:

Specification of class
Parallelogram

Search WWH ::

Custom Search