Mutator+Methods

In computer science, a **mutator method** is a method used to control changes to a variable. The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation. According to this principle, member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable. Often a "setter" is accompanied by a "getter" (also known as an accessor method), which simply returns the current value of the private member variable. Mutator methods may also be used in non-object-oriented environments. In this case, a reference to the variable to be modified is passed to the mutator, along with the new value. In this scenario, the data is not protected from changes that bypass the mutator method, whose role becomes simply to validate the input, and the onus falls to the developer to ensure the variable isn't modified directly.

Ex: public int setHeight
 * Mutator Methods,** also called modifiers, are public methods that change the value of an instance variable. They are often recognized as starting with the word "set."

Example 2: the mutator method can be found on line 12 ( public String setCatsColor) code class Cat {    private static String animalType = "feline"; private String catColor; Cat(String colorIn) {        setCatsColor(colorIn); }    public String getCatsColor {       return this.catColor; }    public String setCatsColor {       this.catColor; }

public static void main (String[] argsIn) {       Cat patches = new Cat("calico"); } }

code

"**.move**" is a **Mutator Method** that allows you to move a designated object a certain distance along the x-axis and y-axis. On the x-axis, a positive number moves the object to the right, a negative number moves it to the left. On the y-axis, a positive number moves the object down, a negative number moves it up.

"object".move("change in x value", "change in y value")

Example:

code import java.awt.*; import objectdraw.*;

public class Movement extends WindowController {   private FilledRect moving; //designates 'moving' as a filled rectangle (FilledRect) public void main {       moving = new FilledRect(0, 25, 25, 25, canvas); //sets the origin and the boundaries of 'moving' moving.move(25, - 25); //this moves object 'moving' 25 units to the right and 25 units up   } } code

"**.moveTo**" is a **Mutator Method** that allows you to move a designated object to a specified location on the x and y axis.

"object".moveTo("location on the x-axis", "location on the y-axis")

code import java.awt.*; import objectdraw.*;

public class MovementTo extends WindowController {   private FilledRect movingTo; //designates 'movingTo' as a filled rectangle (FilledRect) public void main {       movingTo = new FilledRect(0, 0, 25, 25, canvas); //sets the origin and the boundaries of 'movingTo' movingTo.moveTo(25, 25); //this moves object "movingTo"'s origin to 25 on the x-axis and 25 on the y-axis } } code

"**.setColor**" is a **Mutator Method** that allows you to set an object to a certain color.

"object".setColor(Color."color you want the object to be")

code import java.awt.*; import objectdraw.*;

public class Coloring extends WindowController {   private FilledRect colors; //designates 'colors' as a filled rectangle (FilledRect) public void main {       colors = new FilledRect(0, 0, 25, 25, canvas); //sets the origin and the boundaries of 'colors' colors.setColor(Color.red); //this set the color of object "colors" to red } } code

"**.setWidth**" is a **Mutator Method** that allows you to change the width of a designated object. Width is the distance from the origin to the other side of an object along the y-axis.

"object".setWidth("distance from the origin you want the width to be")

code import java.awt.*; import objectdraw.*;

public class Width extends WindowController {   private FilledRect yChange; //designates 'yChange' as a filled rectangle (FilledRect) public void main {       yChange = new FilledRect(0, 0, 25, 25, canvas); //sets the origin and the boundaries of 'yChange' yChange.setWidth(35); //changes the width of object 'yChange' from 25 to 35 } } code

"**.setHeight**" is a **Mutator Method** that allows you to change the height of a designated object. Height is the distance from the origin to the other side of an object along the x-axis.

"object".setHeight("distance from the origin you want the height to be")

code import java.awt.*; import objectdraw.*;

public class Height extends WindowController {   private FilledRect xChange; //designates 'xChange' as a filled rectangle (FilledRect) public void main {       xChange = new FilledRect(0, 0, 25, 25, canvas); //sets the origin and the boundaries of 'xChange' xChange.setWidth(35); //changes the height of object 'xChange' from 25 to 35 } } code

"**.setEndPoints**" is a **Mutator Method** that allows you to change the end points of a designated **line**. "**.setEndPoints**" can not be used with anything but a line.

"line".setEndPoints("new x1", "new y1", "new x2", "new y2")

code import java.awt.*; import objectdraw.*;

public class line extends WindowController {   private Line pointing; //designates 'pointing' as a line (Line) public void main {       pointing = new Line(0, 0, 25, 25, canvas); //sets the two end points of 'pointing' pointing.setEndPoints(10, 20, 35, 45); //changes the end points of object 'pointing' from (0, 0) to (10, 20) and from (25, 25) //to (35, 45) } } code

"**.hide**" is a **Mutator Method** that hides an object from view.

"object".hide

code import java.awt.*; import objectdraw.*;

public class Hiding extends WindowController {   private FilledRect hidden; //designates 'hidden' as a filled rectangle (FilledRect) public void main {       hidden = new FilledRect(0, 0, 25, 25, canvas); //sets the origin and the boundaries of 'hidden' hidden.hide; //this hides object "hidden" } } code

"**.show**" is a **Mutator Method** that shows an object after it has been hidden by **.hide** and places it on top of any other object that is in the same space.

"object".show

code import java.awt.*; import objectdraw.*;

public class ISeeYou extends WindowController {   private FilledRect onTop; //designates 'onTop' as a filled rectangle (FilledRect) private FilledRect other; //designates 'other' as a filled rectangle (FilledRect) public void main {       onTop = new FilledRect(0, 0, 25, 25, canvas); //sets the origin and the boundaries of 'onTop' other = new FilledRect(10, 10, 25, 25, canvas); //sets the origin and the boundaries of 'other' onTop.hide; //this hides object "onTop" onTop.show; //this makes object "onTop" reappear in the same location, and places it on top of           //object "other" } } code