Question

I don't understand accessor methods and I'm stuck with creating setAge, getAge and getName.

This is the question:

Add three accessor methods, setAge, getAge and getName. These methods should set and get the values of the corresponding instance variables.

public class Player {

    protected int age;
    protected String name;

    public Player(String namArg) {
        namArg = name;
        age = 15;
    }
}
Was it helpful?

Solution

An accessor method is used to return the value of a private or protected field. It follows a naming scheme prefixing the word "get" to the start of the method name. For example let's add accessor methods for name:

class Player{
   protected name

//Accessor for name
   public String getName()
   {
     return this.name;
   }
}

you can access the value of protected name through the object such as:

Player ball = new Player()
System.out.println(ball.getName())

A mutator method is used to set a value of a private field. It follows a naming scheme prefixing the word "set" to the start of the method name. For example, let's add mutator fields for name:

//Mutator for name
   public void setName(String name)
   {
     this.name= name;
   }

now we can set the players name using: ball.setName('David');

OTHER TIPS

Your instance variables are age and name. Your setter methods are void and set your passed arguments to the corresponding variable. Your getters are not void and return the appropriate variables.

Try this and come back with questions.

public class Player {

protected int age;
protected String name;

// mutator methods
public void setAge(String a) { age = a; }

public void setName(String n) {
    name = n;
     }

// accessor method
public string getAge() { return age ; }

public string getName() {
    return name;
     }


}

setAbcd methods are mutator methods which you use to set your protected data fields. getAbcd methods are accessor methods which you use to return values of the data fields, as they are usually private and not available outside the class.

e.g

public void getvariableName() {
    return variableName;
   }

Answer for: I don't understand accessor methods here is the thing:

why do we need accessor methods? Encapsulation !!! Why do we need encapsulation?

1) Imagine you (programmer#1) gonna write those setAge, getAge and getName methods. I am programmer#2. I most probably cant access age and name directly. So I have yo use your public accessor methods setAge, getAge and getName. This is to save your code and variables from mess. Cuz u cant trust all coders.

2) In setAge method u can provide VALIDATION

random evil programmer: ya I wanna make age equal to 234 so ur program results gonna crush hahaha

u: nah I gonna add validation condition into my setAge method so u can only make age equal from 0 to 90(whatever u want)

This is the most popular reason why we use accessor methods.

Code explanation:

setAge explanation( this is just to get main idea)

public void setAge(int ageInput) { 
if ((ageInput > 10) && (ageInput <90))
 {age = a;}}

Random evil programmer sends ageInput into your public method. First of all, it checks if age value is correct. If yes, age instance(object) variable will become ageInput. If no, nothing will happen and your variables wont be messed up.

GetAge: it just returns current age value. Nothing fancy.

let me know if you have more questions ;) Best of luck to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top