Reputation: 17
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..
Upvotes: 1
Views: 823
Reputation: 2066
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;
}
Upvotes: 1
Reputation: 91729
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 "
.
Upvotes: 3
Reputation: 2288
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");
}
Upvotes: 1
Reputation: 3109
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
Upvotes: -2
Reputation: 47739
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.
Upvotes: 0
Reputation: 1268
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.);
}
Upvotes: 0
Reputation: 14164
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.
Upvotes: 3