Java Reference
In-Depth Information
double width = box.getWidth();
Now let us consider a mutator method. Programs that manipulate rectangles frequently
need to move them around, for example, to display animations. The Rectangle
class has a method for that purpose, called translate . (Mathematicians use the
term Ȓtranslationȓ for a rigid motion of the plane.) This method moves a rectangle by a
certain distance in the x- and y-directions. The method call,
box.translate(15, 25);
moves the rectangle by 15 units in the x-direction and 25 units in the y-direction (see
Figure 11 ). Moving a rectangle doesn't change its width or height, but it changes the
top-left corner. Afterwards, the top-left corner is at (20, 35).
This method is a mutator because it modifies the implicit parameter object.
46
47
Figure 11
Using the translate Method to Move a Rectangle
S ELF C HECK
18. Is the toUpperCase method of the String class an accessor or a
mutator?
19. Which call to translate is needed to move the box rectangle so that
its top-left corner is the origin (0, 0)?
Search WWH ::




Custom Search