Quiz: Accessor or Mutator? 2
Run this code in the BlueJ code pad:
Rectangle box = new Rectangle(5, 10, 60, 90); System.out.println(box.getX()); System.out.println(box.getWidth()); box.translate(25, 40); System.out.println(box.getX()); System.out.println(box.getWidth());
| What are the first two lines of the printout? (Just put the numbers next to each other, separated by a space, even though they will appear in separate lines.) | 5 60 |
Remember the Rectangle constructor has four parameters: x, y, width, and height. |
| What are the third and fourth line of the printout? | 30 60 |
The translate method shifts the rectangle by 25 pixels to the right, so now x is 5 + 25 = 30. The width does not change. |
What kind of method is translate, accessor or mutator? |
mutator | The translate method has changed the box object, as evidenced by the different result of the getX method. |