The question is asking you to write a Java method that ranks how good a party is.
It ranks the party based on 2 things, that ammount of candy, and the amount of tea.
The rank is determined by 2 rules:
- If there is less that
5
candy or less than 5
tea, the party was bad (0)
- If there is at least twice as much of one of the two things (candy or tea) then
the party was great (2)
- In all other cases, the party was just good (1)
Here is the implementation:
// This method will tell you how good the party was
// based on some pre defined rules
public int teaParty(int tea, int candy){
// first rule, if they is less that 5 candy or tea
// the party is bad (0)
if (tea < 5 || candy < 5) return 0;
// second rule, if there is twice as much tea as candy
// or there is twice as much candy as tea, great party (2)
if (tea >= 2 * candy) return 2;
if (candy >= 2 * tea) return 2;
// otherwise, the party was just good
return 1;
}
Implementation that uses else
and only returns from one point in the method:
// This method will tell you how good the party was
// based on some pre defined rules
public int teaParty(int tea, int candy){
int score = -1;
// first rule, if they is less that 5 candy or tea
// the party is bad (0)
if (tea < 5 || candy < 5) {
score = 0;
}
// second rule, if there is twice as much tea as candy
// or there is twice as much candy as tea, great party (2)
else if (tea >= 2 * candy) {
score = 2;
}
else if (candy >= 2 * tea) {
score = 2;
}
else {
// otherwise, the party was just good
score = 1;
}
return score;
}