Java Reference
In-Depth Information
Rectangle r;
. . .
if (r.equals(null))
r = new Rectangle(5, 10, 20, 30);
219
220
Exercise R5.15 Explain how the lexicographic ordering of strings differs
from the ordering of words in a dictionary or telephone book. Hint:
Consider strings, such as
IBM, wiley.com, Century 21,
While-U-Wait
, and 7-11.
Exercise R5.16. Write Java code to test whether two objects of type
Line2D.Double
represent the same line when displayed on the
graphics screen. Do not use
a.equals(b)
.
Line2D.Double a;
Line2D.Double b;
if
(your condition goes here)
g2.drawString(ÐThey look the same!Ñ, x, y);
Hint: If
p
and
q
are points, then
Line2D.Double(p, q)
and
Line2D.Double(q, p)
look the same.
Exercise R5.17. Explain why it is more difficult to compare floating-point
numbers than integers. Write Java code to test whether an integer
n
equals
10 and whether a floating-point number
x
equals 10.
Exercise R5.18 Consider the following test to see whether a point falls
inside a rectangle.
Point2D.Double p = . . .
Rectangle r = . . .
boolean xInside = false;
if (r.getX() <= p.getX() && p.getX() &= r.getX()
+ r.getWidth())
xInside = true;
boolean yInside = false;
if (r.getY() <= p.getY() && p.getY() <= r.getY()
+ r.getHeight())
yInside = true;
if (xInside && yInside)
g2.drawString(Ðp is inside the rectangle.Ñ,
p.getX(), p.getY());