tags:

views:

97

answers:

4

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?

+2  A: 

Try with

public void setAge(int age) 
{
  this.age = age;
}
Grzegorz Oledzki
Though we can't be sure that the name of the field actually is `age`
Don
worked perfectly thank you
Tical
+4  A: 

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'.

ankon
+1  A: 

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

Clay Fowler
A: 

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'.

Kushal Paudyal