Which of the following is a mutator method for the rectangle class?

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?

asked Apr 10, 2014 at 2:40

9

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.

answered Apr 10, 2014 at 2:49

stinepikestinepike

53.1k14 gold badges91 silver badges111 bronze badges

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.

answered Apr 10, 2014 at 2:48

Martin SerranoMartin Serrano

3,6471 gold badge33 silver badges47 bronze badges

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; }

answered Apr 10, 2014 at 2:48

Which of the following is a mutator method for the rectangle class?

Elliott FrischElliott Frisch

194k20 gold badges152 silver badges241 bronze badges

Introduction

In Java accessors are used to get the value of a private field and mutators are used to set the value of a private field. Accessors are also known as getters and mutators are also known as setters. If we have declared the variables as private then they would not be accessible by all so we need to use getter and setter methods.

Accessors

An Accessor method is commonly known as a get method or simply a getter. A property of the object is returned by the accessor method. They are declared as public. A naming scheme is followed by accessors, in other words they add a word to get in the start of the method name. They are used to return the value of a private field. The same data type is returned by these methods depending on their private field.

Syntax

  1. public int getNumber()  
  2. {  
  3.   return Number;  
  4. }  

Example

  1. public class Employee {  
  2.     private int number;  
  3.     public int getNumber() {  
  4.         return number;  
  5.     }  
  6.     public void setNumber(int newNumber) {  
  7.         number = newNumber;  
  8.     }  
  9. }  

Mutators

A Mutator method is commonly known as a set method or simply a setter. A Mutator method mutates things, in other words change things. It shows us the principle of encapsulation. They are also known as modifiers. They are easily spotted because they started with the word set. They are declared as public. Mutator methods do not have any return type and they also accept a parameter of the same data type depending on their private field. After that it is used to set the value of the private field.

Syntax

  1. public void set Age(int Age) {  
  2.     this.Age = Age;  
  3. }  

Example

  1. public class Cat {  
  2.     private int Age;  
  3.     public int getAge() {  
  4.         return this.Age;  
  5.     }  
  6.     public void set Age(int Age) {  
  7.         this.Age = Age;  
  8.     }  
  9. }  

How to Create Accessors and Mutators Automatically

Accessors and mutators can be generated automatically by the editor. Use the following procedure.

  • Move the cursor towards the end of all private fields.

    Which of the following is a mutator method for the rectangle class?

  • Right-click and then select insert code from there.

    Which of the following is a mutator method for the rectangle class?

  • Select getter from the list and then the window appears then check the required checkbox and then click ok.

    Which of the following is a mutator method for the rectangle class?

  • Repeat the entire process for setters.

    Which of the following is a mutator method for the rectangle class?

Purpose of Accessors and Mutators

Our main purpose is to hide the data of the object as much as we can, so these methods prevent illegal access to these objects either willful or not. Validation is also imposed by these methods on the values that are being set.

Difference Between Accessors and Mutators

  • Any value is not returned by a mutator method while the accessor method returns a value.
  • A Mutator method is a setPriority( ) method, on the other hand the accessor method is the getPriority method.
  • This method is present in both setter and getter in the Thread class in java.lang package.
  • In a setter we set the name to the thread and in the getter we retrieve the name to the thread.

Summary

This article introduces the accessor and mutator methods in Java, including the differences between accessors and mutators.

Which method is a mutator?

In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter (together also known as accessors), which returns the value of the private member variable.

What are mutator methods in Java?

In Java, mutator methods reset the value of a private variable. This gives other classes the ability to modify the value stored in that variable without having direct access to the variable itself. Mutator methods take one parameter whose type matches the type of the variable it is modifying.

What are accessor methods What are mutator methods?

In Java accessors are used to get the value of a private field and mutators are used to set the value of a private field. Accessors are also known as getters and mutators are also known as setters.

How do you write a mutator method?

Moreover, the method name should begin with the word set in lowercase. Followed by the class variable name with an initial capital letter. For instance, if our class has a variable called age , then the mutator method should look as follows: public void setAge(int age){...}