1

Need to write a method describePerson() that takes 3 parameters, a String giving a person’s name, a boolean indicating their gender (true for female, false for male), and an integer giving their age. The method should return a String formatted as in the following examples:

Lark is female. She is 2 years old. Or Jay is male. He is 1 year old.

I am not sure how to write it correctly (my code):

int describePerson(String name, boolean gender, int age) {
    String words="";
    if(gender==true)  return (name + "is "+gender+". "+"She is"+age+ "years old.);
    else
        return (name + "is "+gender+". "+"She is"+age+ "years old.);

} 

The outcome "year" and "years" is also differs, but i don't know how to make it correct..

3
  • 6
    I think changing int describePerson to String describePerson would be a good start Commented Oct 22, 2013 at 3:25
  • 1
    "The method should return a String" Then why do you have it return an int? Commented Oct 22, 2013 at 3:25
  • Just keep nesting your if statements. There are more clever ways to do it, but simple is good in this case. (And the method should return String, not int.) Commented Oct 22, 2013 at 3:39

7 Answers 7

3

The return value of the describePerson should be of type String. Also, the boolean gender is not a string, so you need to write expressions into the return statement yourself.

String describePerson(String name, boolean gender, int age) {
    String yearString = (age == 1 ?  "year" : "years");

    if (gender) return (name + " is female. She is " + age + year + " old.");
    else return (name + " is male. He is " + age + year + " old.");
} 

Aside from the other described problems, be sure to remember to end strings with ".

Sign up to request clarification or add additional context in comments.

4 Comments

Plus a third return statement for "other" genders! Love that code.
Indeed. I probably should get rid of that since something can only be true or false.
How can I get output not only "years", but "year" as well? For example, he is 1 year old. Not 1 "years"
Note that usual convention would be to say "0 years old" rather than "0 year old", so the compare should be for == 1 vs > 1.
3

Try using a "ternary" or "conditional operator" for the gender. You want to output "male" or "female", not "true" or "false" which simply appending the boolean to the string will give.

Assuming 'true' is female:

String genderStr = (gender ?  "female" : "male");
String pronoun = (gender ?  "She" : "He");

And yes, you probably want to return a String rather than an int.

Comments

1

First of you, if you want to return a String, your return type should be String so the method signature should be

String describePerson(String name, boolean gender, int age)

Also you need to print he and she depending on the gender, so there must be a if condition. Try the following code in place of your existing method

String describePerson(String name, boolean gender, int age) {
    String genderStr=null;
    String genderPro = null;
    String year = null;
    if(gender){
        genderStr = "female";
        genderPro = "She";
    }
    else{
        genderStr = "male";
        genderPro = "He";
    }
    if (age == 1) {
                   year = "year";
                 }
                 else {
                   year = "years";
                 }
    return (name + " is "+genderStr+". "+genderPro+" is "+age+ " "+ year+" old");
    } 

1 Comment

Looks kinda familiar.
1

Try this

public String describePerson(String name, boolean gender, int age) {
    String describe = "%s is %s. %s is %d years old.";
    boolean isFemale = gender == true;
    String sexGender = isFemale  ? "female" : "male";
    String sexType = isFemale  ? "She" : "He";
    String finalDescribe = String.format(describe, name, sexGender, sexType, age);
    System.out.println(finalDescribe);
    return finalDescribe;
}

2 Comments

Good approach, but code formatting should be improved, result-variable should be called 'result', format variable should be called 'format', 'pattern' or 'template', boolean == true is redundant, and no println in the method.
Note that gender == true is redundant.
0

Since you are returning a string you need to change your return type to String

String describePerson(String name, boolean gender, int age) {
    String words="";
    if(gender==true)  return (name + "is "+gender+". "+"She is"+age+ "years old.);
    else
        return (name + "is "+gender+". "+"She is"+age+ "years old.);

} 

Comments

0

Probably the best way for you to do it is to pre-compute the parts of your statement, then put it together:

String maleFemale;
String heShe;
String yearYears;

if (gender) {
  maleFemale = "female";
  heShe = "She";
}
else {
  maleFemale = "male";
  heShe = "He"
}
if (age == 1) [
  yearYears = "year";
}
else {
  yearYears = "years";
}
return name + " is " + maleFemale + ". " + heShe + " is " + age + " " + yearYears + " old.";

It is usually best to break things up like this, into pieces you understand, rather than trying to use fancy operations you don't understand, glommed together tightly to where you can't follow the program flow and add println statements in-between operations.

3 Comments

To be honest I find @Jayasagar's answer or three ternary/condition operators at lot more concise & clearly formatted, than this. I mostly avoid "declaration without value" and "else on separate line" style, except for large if-else trees. Vertical conciseness is important as studies find the maximum understandable size of an algorithm, to be around one vertical page.
OTOH, interesting approach to variable-naming of the parts -- seems workable & systemic, and I'm always interested in that. My approach would generally have been "genderStr", "pronoun", "yearPlural" or somesuch and in some ways yours could definitely be clearer.
@ThomasW - Yeah, if it were for me I'd use the ternary operators, but they're confusing to newbies, so for this case I skipped them. And the above is compact enough. And, in my 40 years of programming, I've found the else on a separate line to be less error-prone and easier to read than glomming them together.
-2

1: Use String.format

2: introduce variable heShe

string describePerson(String name, boolean gender, int age) 
{
    String x = gender ? "She" : "He";

    return (name + "is "+gender+". " + x + " is"+age+ "years old.);
} 

3: parameter gender is boolean, better is to make use of an enum

4 Comments

Do you mean string or String? Also, I think there are some problems with the String that is being returned... As written, it'd produce something like "Foois true. She is42years old."
Not my downvote, but I generally avoid the style of assigning one value, and then conditionally overriding that. The words variable also appears to be redundant here - and if it were used, it should be named result.
@jason - Thanks for the feedback ..(so you also should give other people -1 , because the also make this "mistake" ")
@lordkain It is not my intention to offend. The answer you have given has several problems which would be confusing or misleading to novice Java developer. I can (respectfully) give you feedback in comments or I can edit your post (which then has to be reviewed & approved) to correct the errors. Either way, an edit might cause the folks who've down voted to change their votes...

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.