M Ann
M Ann

Reputation: 107

Where to put the 'return' keyword in java?

Here are the requirements for the task:

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!

Upvotes: 0

Views: 401

Answers (1)

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

Upvotes: 1

Related Questions