Search code examples
javareturnreturn-value

Where to put the 'return' keyword in java?


Here are the requirements for the task:

  • The class should have a constructor that takes two arguments: a string value for the person's name and an integer value for the person's age.
  • Create a toString method that returns the name and age of the person. The values ​​must be separated by a space.
  • Create the getName methods () and getAge () methods to return the person's name and age.
  • Create the growOlder (nYears) method to increase the person's age by nYears (an integer).
  • In the growOlder (nYears) method, the value of nYears must be ensured that it is a positive value. At positive value (including the value zero), the person's age should be increased and the method should return the value true (to indicate that the change in value was completed). If the value of nYears is negative, no change in age should occur and the method should return the value false (to indicate that the change in value did not occur).

Note: nYears = 13 is provided for the task This is what our code will be tested on: enter image description here


So far I have the following code and it works BUT I am having trouble finalizing it because of the "return" keyword.

public class Person{

    private String name;
    private int age;
    private int getOlder;

    public Person(){
        this("Otto", 25);
    }
    public Person( String name, int age ){
        this.name = name;
        this.age = age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
    public int growOlder(int nYears){

        if(nYears >= 0){
            age = age + 13;
            System.out.println("true");

        }else{
            System.out.println("false");
        }
       return nYears; //ERROR
    }

    public String toString( ){
        return getName() + " " + getAge();

    }
}

I am supposed to get results like:

Otto 25
true
Otto 38
false
Otto 38

But with this code, I get these results:

Otto 25
true
13
Otto 38
false
-13
Otto 38

If I don't place a return or return value, it creates an error. I dont really know what to do here. Thank you in advance!


Solution

  • In java, there are pieces of code called methods. (Methods are sometimes referred to as functions.)

    In this code snippet you have defined a method:

    public int growOlder(int nYears){
        if(nYears >= 0){
            age = age + 13;
            System.out.println("true");
    
        }else{
            System.out.println("false");
        }
       return nYears; //ERROR
    }
    

    The method is publicly accessible from code outside the class.

    The method returns a value of type int.

    The method is called growOlder.

    The method accepts a variable of type int declared as nYears.

    So methods are defined the following way:

    <visibility> <return type> <name>    <parameters>
       public         int     growOlder  (int nYears)
    

    The return statement is used to exit a method. When a method is defined with a return type (anything other than void), it's mandatory for all code paths in the method to have a return statement along with the value it's returning.

    Let's take a look at your method again and see how to make the output work as you wish.

       public int growOlder(int nYears){
    
            if(nYears >= 0){
                age = age + 13;
                System.out.println("true");
    
            }else{
                System.out.println("false");
            }
           return nYears; //ERROR
        }
    

    It looks as if you are printing true and false instead of returning it.

    The action here is to change the return type from int to boolean, and replace your System.out.println() with return true and return false.

    After this change, we can condense the method to communicate our message more clearly:

    public boolean growOlder(int nYears){
        age += nYears; //Add nYears to age and update its value
        return nYears >= 0; //Return the value of the expression "is nYears greater than or equal to 0" yes results in true, no results in false.
    }