Java > Logic-1 > answerCell (CodingBat Solution)

Problem:

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) → true
answerCell(false, false, true) → false
answerCell(true, false, false) → false


Solution:

public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
  if (isAsleep)
    return false;
  if ((isMorning && isMom) || !isMorning)
    return true;
  else return false;
}


14 comments :

  1. Replies
    1. it is actually correct, maybe you forgot to delete the beginning that they give you

      Delete
  2. public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
    if(!isAsleep) {
    return isMom || !isMorning ? true : false;
    }
    return false;
    }

    ReplyDelete
  3. public static void main(String[] args) {
    // TODO code application logic here
    AnswerCell n=new AnswerCell();

    System.out.println(n.answerCell(false,false,false)); //true
    System.out.println(n.answerCell(false,false,true)); //false
    System.out.println(n.answerCell(true,false,false)); //false
    }
    public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep){
    if (isAsleep)
    {
    return false;
    }
    if (!isMorning || isMom)
    {
    return true;
    }
    return false;
    }
    }

    ReplyDelete
  4. return (!isAsleep && !isMorning || (!isAsleep && isMom));

    ReplyDelete
  5. public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
    return (!isAsleep && !isMorning || !isAsleep && isMom);
    }

    ReplyDelete
  6. public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
    return (!isAsleep && !isMorning || !isAsleep && isMom);
    }

    ReplyDelete
  7. if (!isAsleep) {
    if (!isMom) {
    if (!isMorning) {
    return true;
    }
    return false;
    }
    return true;
    }
    return false;

    ReplyDelete
  8. public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
    if (isAsleep){
    return false;
    }
    else if (!isAsleep && isMorning && !isMom){
    return false;
    }else{
    return true;
    }
    }

    ReplyDelete
  9. public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
    if (isAsleep){
    return false;
    }
    else if (!isAsleep && isMorning && !isMom){
    return false;
    }else{
    return true;
    }
    }

    ReplyDelete
  10. public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
    if(isAsleep){
    return false;
    }
    if(isMorning){
    return isMom;
    }
    return false;
    }

    ReplyDelete
  11. public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
    return !isAsleep && (isMom || !isMorning);
    }

    ReplyDelete
  12. return((isMorning&&isMom&&!isAsleep||!isMorning&&!isAsleep)?true:false);

    ReplyDelete
  13. if(isAsleep)return false;
    if(!isMorning||isMom)return true;
    else return false;

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact