My getMethod does not return the value in the setMethod: Java

427 views Asked by At
public class OOP_10_Encapsulation {
  private String firstName;
  private String favFood;
  private int age;
  private float weight;

//create a set method for firstName
public void setFirstName(String firstName){
    this.firstName = firstName;
}
//create a get method for firstName
public String getFirstName(String firstName){
    this.firstName = firstName;
    return firstName;
}

//create a set method for favFood
public void setFavFood(String favFood){
    this.favFood = favFood;
}
//create get method for favFood
public String getFavFood(String favFood){
    this.favFood = favFood;
    return favFood;
}

//create a set method for age
public void setAge(int age){
    this.age = age;
}
//create a get method for age
public int getAge(int age){
    this.age = age;
    return age;
}

//create a set method for weight
public void setWeight(float weight){
    this.weight = weight;
}
//create a get method for weight
public float getWeight(float weight){
    this.weight = weight;
    return weight;
}
}

public class OOP_11_TestEncapsulation {
public static void main(String[] args) {
    OOP_10_Encapsulation learner1 = new OOP_10_Encapsulation(); //create object of OOP_10_Encapsulation
    learner1.setFirstName("Evander O A");
    learner1.setAge(31);
    learner1.setWeight(98.6F);
    learner1.setFavFood("waakye");

    System.out.println("Hi, my name is " + learner1.getFirstName());
    }
}

I have this challenge that I am not able to get my head around. When I run the code in the main method, the get method does not return the name "Evander O A" as set in the set method. Actually, none of my get methods are getting anything I need them to get.

1

There are 1 answers

1
Mihai On

Let's look at one of your get methods:

//create a get method for age
public int getAge(int age){
    this.age = age;
    return age;
}

The purpose of a get method, also called "getter" is to get something from inside an object, usually it's some of the object's attribute that you wanna get.

If you just want to get an attribute, there is no need to pass a parameter to the get method:

public int getAge(int age) // age parameter is useless

Not only that it is useless here, but you are also doing something wrong, in the get method you are first changing attribute's value. You are effectively doing a set inside a getter method:

this.age = age; // wrong

Given your code for getAge method, let's see how it will execute when you call something like:

System.out.println("Hi, my name is " + learner1.getAge())

The function getAge will be called with no parameter, although it is expecting one. So most probably you won't be able to compile this piece of code. I got the following error when trying to compile something similar:

https://i.stack.imgur.com/xcUGi.jpg

So if you create a function with 1, 2, 3 or whatever number of parameters then you must call it using the same number of arguments to the function call. Otherwise most probably that code won't run.

Now let's assume I call it with some value as argument, probably you did too.

System.out.println("Hi, my name is " + learner1.getAge(0))

This is going to happen:

//create a get method for age
public int getAge(int age // which is 0){
    this.age = age; // you set the age to 0, losing the old value
    return age; // you return 0, because you no longer have the old value
}

How should you do it? Simple:

//create a get method for age
public int getAge(){
    return age;
}

Now apply this fix to all of your getters (the get methods) and you are good to go. Let me know if something is not clear.