Write a program using ternary operator to print whether the person is Man, Boy, Woman or Girl depending upon the gender and age. Note if the age is greater than 25 boy will become man and if the age is greater than 20 girl will become woman.
Input (Integer, Character) |
Output (String) |
40, 'M' |
Man |
15, 'M' |
Boy |
25, 'M' |
Boy |
35, 'F' |
Woman |
12, 'F' |
Girl |
20, 'F' |
Girl |
class ClassifyPersonUsingTernaryOperator
{
public static void main(String s[])
{
System.out.println("A person with age 25 and female will be : " + classifyPerson(25, 'F'));
}
public static String classifyPerson(int age, char gender)
{
String result = null;
return result;
}
}
Topic:
Ternary Operator In Java
If you need explanation Read this topic
If you need Answer Take test on this topic
User comments below. All of them might not be correct.
to implement this program we can use ternary operator.syntax: a>b?1:0 here if a>b return true thn 1 is ans else 0 is get returned.coming to this program we need to check 2 input arguments ,so here nested ternary concept should be used.result=(gender==M)?((age>25)?"Man":"Boy"):((age>20)?"Woman":"Girl") like this we can implement nested operation.here gender M thn age>25 option get selected and executed,otherwise age>20 get executed
Posted by Maheshwari Natarajan 2014-12-17 07:53:35
here we can solve this problem..in 3 ways..
If else
Ternary
Switch
switch:
keep to label in Switch as 'M' and 'F'
and check the age in case using ternary
(age>25)?print man :print boy
in another case label (age>20)?print Woman : print Girl
Ternary: (gender=='M')?(age>25)print Man: print Boy:(age>20)print woman:Girl
Posted by Uday Kumar 2014-12-18 01:15:23
This dose is now closed and the winners are Maheshwari Natarajan, for 'First Correct Comment', Uday Kumar, for 'Second Correct Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.
Posted by Merit Campus 2014-12-18 04:49:13