2

I have an int variable and I want to create a String method to return that int variable, how do I go about it? Example below... and set the getAge() method to return "young" when age is 18, "old" when age is 30.

private int age;

public String getAge() {

}
5
  • 6
    5 rep says you get downvoted for calling people old :) Commented Sep 17, 2011 at 6:18
  • @user - Hint: You can achieve it using a conditional statement if. Commented Sep 17, 2011 at 6:22
  • 1
    What happens if you are 17, 19, 29 or 31? Commented Sep 17, 2011 at 6:32
  • Eighteen yo want to be treated as adults, so they won't like "young" either. ;) Commented Sep 17, 2011 at 6:32
  • Hmm, no downvotes. I'll be back in six years. Commented Sep 17, 2011 at 7:18

4 Answers 4

5

Taken literally:

public String getAge() {
    return (30 == age)? "old":
           (18 == age)? "young":
                // because you said 18 is young, 30 is old, but didn't say
                // anything about all of the other ages!
                "I don't understand!";
}

You can do this a few ways. Ternary structure and "if" statement are generally the best.

// this if/else reads "(if age >= 30 then return old) else return young"
public String getAge() {
    if (30 <= age)
       return "old";
    else
       return "young";
}

// this ternary statement reads "return (if age >= 30 then old) else young"
public String getAge() {
    return (30 <= age)? "old":"young";
}

// This would be my preference
public String getAge() {
    // add bounds checking!
    if (125 <= age)
       return "You are probably dead";
    else if (0 > age)
       return "Hi doc brown! What's it like to travel through time?";
    else if (30 <= age)
       return "old";
    return "young";
}
Sign up to request clarification or add additional context in comments.

Comments

0

getAge() is not a good naming method.

It confused other developers/users that getAge() will return an int number.

I think you should name your method like getAgeClass().

Notice that a public method will be exposed to other classes, it is very important your naming of public method should be meaningful, not confusing. This is a good practice when you code OO

Comments

0

I would not recommend this for the trivial case of just 2 ages, but if you want to expand...

You can of course also add display strings as well.

public enum AgeMonikers
{
    AweCute(2),
    DontTouchThat(4),
    Child(10),
    Preteen(13),
    Trouble(20),
    MoveOut(24),
    ThinkYouKnowEverythingDev(25),
    ActuallyKnowSomeDev(30),
    OldFart(100),
    WishIWasDead(Integer.MAX_VALUE);

    private int maxAge;

    private AgeMonikers(int ageLimit)
    {
        maxAge = ageLimit;
    }

    static public AgeMonikers getMoniker(int age)
    {
        if (age < 0) 
            return null;

        for(int i=values().length-1; i>0; i--)
        {
            AgeMonikers val = values()[i];

            if (age >= val.maxAge)
                return values()[i+1];
        }
        return AweCute;  // age < 2 - I know it will include negatives.
    }
}


public String getAge() 
{
    return AgeMoniker.getMoniker(age).toString();
}

Comments

-1

Better use a switch statement since the conditonal expression is not changing. So do it like:

switch (age) {
    case 18:
        return "young";
    case 30:
        return "old";
    default:
        return "??";
    }

4 Comments

Since I'm neither 18 nor 30, my current age is apparently "??". O_o
If you fill in the 'placemats' from 5 y.o. - 110 y.o., it should become obvious why this approach is less than optimal.
break after return? This should be a compile error: unreachable code (assuming there is a ; after "??")
@Andrew: I got what you are trying to make me understand. But read the question the guy has asked, he doesnt want to handle a range! If there is range, then if/else is obviously better than switch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.