0

Consider a potential instance method named rotate that would rotate a Rectangle object 90 degrees by swapping its width and height values. For example, if a Rectangle's dimensions are 10 x 30, then calling the rotate method would change its dimensions to 30 x 10.

I think this will be an accessor method, right? Since we are not actually changing the values?

  • How do you envision `Rectangle` class? What fields should it have? – PM 77-1 Apr 10 '14 at 02:43
  • private int x; private int y; private int width; private int height; – chickennoodle Apr 10 '14 at 02:44
  • OK. What values would `width` and `height` have **before** you execute `rotate()` and **after**? – PM 77-1 Apr 10 '14 at 02:46
  • they can be any values. this was just a really general question :) – chickennoodle Apr 10 '14 at 02:48
  • Wrong. Rotating by 90 degrees define these values, and you specifically state that "*if a Rectangle's dimensions are 10 x 30, then calling the rotate method would change its dimensions to 30 x 10.*" So, please, start thinking. – PM 77-1 Apr 10 '14 at 02:49
  • oh.. so since you are actually changing the state of the fields, it is a mutator.. – chickennoodle Apr 10 '14 at 02:51
  • Can a possible header be: public void rotate(int dWidth, int dHeight) { ? – chickennoodle Apr 10 '14 at 02:59
  • I think it should be `public void rotate();` as long as it always for 90 degrees. No need to expose `width` and `height`. – PM 77-1 Apr 10 '14 at 03:20
  • Here is another case: Now consider a potential instance method named contains that would take two integers specifying x and y coordinates and determine if the point with those coordinates falls inside the Rectangle object on which the method is invoked. The method would return true if the point falls inside the Rectangle, and false otherwise. ---------------------------->>>> I know this is an accesor method. So can a possible header start like: public int contains() { ? – chickennoodle Apr 10 '14 at 03:46

3 Answers3

1

Accessor Methods

An accessor method is used to return the value of a private field.

Mutator Methods

A mutator method is used to set a value of a private field.

So here as you are changing the state of private fields so it is a mutator.

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

See this discussion of the meaning of accessor methods. The general opinion is that they do not modify state. If you are rotating the object, you are modifying its state and rotate is mutating the object.

Community
  • 1
  • 1
Martin Serrano
  • 3,727
  • 1
  • 35
  • 48
0

Any method that changes an object's state is properly a mutator. As you are changing the dimensions (through the swap) it is a mutation. These would be accessor methods given your class definition,

public int getX() { return x; }
public int getY() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249