简体   繁体   中英

Writing a mutator method

Revision of mutator method definition

Write a mutator method setAge() that takes a single parameter of type int and sets the value of variable age Paste your answer in here:

public int setAge(int age) 
{
   return age;
}

Comments:

* Test 1 (0.0 out of 1)

      The compilation was successful
      The output should have been:
          setAge() Correct

      This is what was actually produced:
          setAge() not Correct

confused on why i get this error, is it because i have (int age) after setAge that is why the error is comming up?

Your mutator is not actually setting anything.

I assume you already have a piece of code that you have to modify, search in that piece for a variable/field 'age'.

Try with

public void setAge(int age) 
{
  this.age = age;
}

Your code does not "set the value of a variable age". Your method only returns the value that was passed to it.

Assuming you have a class variable called 'age', you can create the mutator class method as:

public class myTest{

public int age;

//other code here..if any

public void setAge(int age) 
{
  this.age = age;
}

//other code here.. if any
}

Normally your setAge method should not return anything. Mutator only modifies the value.

To return value you should use getAge() method which is called 'Accessor'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM