2

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;
    }
}
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • Are you asking how to create the getter and setter methods? – Andrew Oct 17 '13 at 19:49
  • You should try to solve this yourself first and come back to us when you have hit a roadblock. If you've tried then ask a more specific question and we should be able to help get you past it. – Shaded Oct 17 '13 at 19:49
  • @Andrew Yes i would like how to create them e.g the syntax. please –  Oct 17 '13 at 19:50
  • 1
    http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html – Andrew Oct 17 '13 at 19:51
  • @Shaded I have already given it ago, but im not entirely sure. –  Oct 17 '13 at 19:51
  • Have a look at [this SO entry](http://stackoverflow.com/questions/6638964/set-and-get-methods-in-java). – keenthinker Oct 17 '13 at 19:52
  • If you don't like your question anymore, delete it. Filling it with garbage characters makes all the answers meaningless. –  Oct 17 '13 at 21:37

5 Answers5

4

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');

Tim Knox
  • 89
  • 5
1

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.

Whoppa
  • 949
  • 3
  • 13
  • 23
0

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;
   }
ozzyzig
  • 709
  • 8
  • 19
0
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;
     }


}
Shaban Mousa
  • 154
  • 1
  • 2
  • 8
  • I had though the getName and getAge were returning it but i wasnt entirely sure what the setters did. Thats why i questioned if i was doing it correctly and asked on here. Thanks for the help –  Oct 17 '13 at 19:57
  • setters do setting the value of a field . the idea is you define a private variable inside the class to force the callers to call your setter methods . suppose if you want to add condition on age like age must be more than 15 years . so you will place that condition inside age setter method . – Shaban Mousa Oct 17 '13 at 19:59
0

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.

Stella
  • 1
  • 4