tags:

views:

224

answers:

3

We are having a party with amounts of tea and candy. Return the int outcome of the party encoded as 0=bad, 1=good, or 2=great. A party is good (1) if both tea and candy are at least 5. However, if either tea or candy is at least double the amount of the other one, the party is great (2). However, in all cases, if either tea or candy is less than 5, the party is always bad (0).

teaParty(6, 8) → 1 teaParty(3, 8) → 0 teaParty(20, 6) → 2

public int teaParty(int tea, int candy) 
{
    if ( teaandcandy >=5) return "2";
    if ( teanandcandy <= 5) return "0"; 
} 
+3  A: 

I guess you have to write a method teaParty like this:

int teaParty( int teaValue, int candyValue ) {
    int result = ....
    // enter the given conditions and calculations here
    return result;
}
tangens
You forgot the calculations! How is she supposed to turn in this??? sheesh!!
Byron Whitlock
A: 

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:

  1. If there is less that 5 candy or less than 5 tea, the party was bad (0)
  2. If there is at least twice as much of one of the two things (candy or tea) then the party was great (2)
  3. 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;
}
jjnguy
I won't down vote you but is this really helpful?
ChaosPandion
You ought to be ashamed of yourself. :( [I am not a downvoter btw]
Byron Whitlock
@Chaos, I do believe it would be helpful for me. If I wasn't sure how to implement a method, seeing the implementation would clarify what the question was looking for.
jjnguy
@Byron why should I be ashamed? I am just being helpful.
jjnguy
Because you just did her homework in 38 seconds flat. Give a man a fish...
Byron Whitlock
i am little week in understand the actual question because i am not a english native speaker and if some one teach me whats the way to solve this problem i can do because i am not nil in writing code but statement give me trouble , if you guys mind it then sorry i will not ask again .
anleea
@byron yup, it makes me happy to know that this person will get a good grade on their homework and then learn a valuable lesson when it comes time for the test `;)`
jjnguy
Or learn nothing at all and be propelled into an industry that needs problem solvers?
ChaosPandion
+1 LOL well played sir.
Byron Whitlock
its not my home work , i am preparing my self for my midterm and i am practicing the if and else statement and trying to understand the question and there logic, thanks you guys to help me out .
anleea
@anleea look at my answer again, I added some more info to the top of it. It may be more helpful than before.
jjnguy
@ justin , why we don't use else here ?
anleea
@Justin - It may be better to lay out the logic structure in a more verbose matter so they understand it better.
ChaosPandion
@anleea The reason I did not use else is because I returned directly after the if statement were true. It can be re-written using `else` but it is not necessary in this case because I am returning from multiple points in the method. I will show you a version that uses else statements.
jjnguy
@anleea I added a method to the bottom that uses `else`.
jjnguy
@ Justin, if (tea >= 2 * candy) return 2; if (candy >= 2 * tea) return 2; i undertand first condition , in above condition you multiply with for this rule as you mentioned # If there is at least twice as much of one of the two things (candy or tea) then the party was great (2) ?????
anleea
why we cannot write just if (tea >= 5) return 2; if (candy >=5 * tea) return 2;?
anleea
@anleea - Correct, you can also write that like this `if (tea >= 2 * candy || candy >= 2 * tea) return 2;`
ChaosPandion
as you describe the question in steps its very helpful for me because now i can understand whats the basically question is .
anleea
@anleea `if (tea >= 5) return 2;` All that tells you is that you have at least `5` tea. In order for the party to be great (2), you need to have two times as much tea as you have candy, or you need to have two times as much candy as you have tea. That is why I have two `else` conditions that multiply tea or candy by 2.
jjnguy
@anleea - If the amount of tea is greater than 5 but less than the amount of candy doubled how can we say it is a great party?
ChaosPandion
@anleea, what Chaos posted is also correct for checking to see if the party was great (2) `if (tea >= 2 * candy || candy >= 2 * tea)` I just broke it into two steps to be more explicit.
jjnguy
just only Explain the question as you did first .Your cell phone rings. Return true if you should answer it. Normally you answer, except in the morning you only answer if it is your mom calling. In all cases, if you are asleep, you do not answer.answerCell(false, false, false) → trueanswerCell(false, false, true) → falseanswerCell(true, false, false) → false
anleea
so in first question i need integers , in this question have i just compare true values ?
anleea
@anleea So you need to write a method that takes in three booleans (is It Morning, is It Mom, are You Asleep).
jjnguy
@anleea If you are asleep you always return false. If is is not the morning and you are awake you return true. If it is the morning and it is your mom calling, return true, otherwise return false.
jjnguy
like in previous question we gave the value 5 , what we have to give here to get return answer true or false ?
anleea
@anleea You need to check the boolean values that were passed in, for example: `if (asleep) return false;` That means, if the person is asleep, you return false (don't answer the phone) from the method.
jjnguy
i am just confuse that which values i can assign in if condition its could be integer or it cold be a true , as you said i need to check Boolean values this confuse me i don't know exact way from where i should take values and asing them
anleea
@anleea You will get the boolean values passed into the method. The method signature will look like: `public boolean answerCell(boolean isMorning, boolean isMom, boolean amAsleep)` You will use those booleans that were passed in to decide whether or not to return true or false from the method.
jjnguy
Like This ?public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) { if (isMorning if (isAsleep) return "false";
anleea
@anleea you can have a boolean to assign to: `boolean answerPhone = false;` Later, after assigning to it, `return answerPhone;`
jjnguy
@anleea yup, that is really close. You need a few else clauses in there to make it correct.
jjnguy
public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) { if (isMorning } else { if (isAsleep) return "false"; }
anleea
but it dose not work
anleea
public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) { if (isMorning } else if (isAsleep) { return "true"; } else if (idMom) { return "true"; } }
anleea
@anleea I have to leave now, but the following code satisfies the method. `if (isMorning) return isMom;` `return !isAsleep`
jjnguy
any ways thanks very much sir for help me , bless you
anleea
A: 
int greatnessOfParty(int teaAmount, int candyAmount)  
{  
  //evaluate condition for a bad party  
  //evaluate condition for good party   
  // else return 2
}

The third condition does not need to be evaulated because if it gets to that point you know it must be true. Without giving the answer directly this should help.

Woot4Moo
thanks for the downvote, any particular reason? The OP's question is almost identical to this: http://javabat.com/prob/p159531
Woot4Moo