answerCell java challenge

Hi,
I am trying below challenge


http://codingbat.com/prob/p110973

I tried as  below
public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
  
  
  if(isAsleep==true){
  return false;
  }
  
  else if(isMorning==false){
  
  return true;
  }
  
  return false;
  
}

Open in new window


I am getting below result


Expected      Run            
answerCell(false, false, false) → true      true      OK         
answerCell(false, false, true) → false      false      OK         
answerCell(true, false, false) → false      false      OK         
answerCell(true, true, false) → true      false      X         
answerCell(false, true, false) → true      true      OK         
answerCell(true, true, true) → false      false      OK         
other tests
OK            
          



how to  improve my approach and design of this challenge. How do i make a graphical venn or some other relevant diagram to design it before writing single line of code to decide best strategy?
 Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
return !isAsleep&&(isMom||!isMorning);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
return isAsleep?false:isMorning&&isMom?true:!isMorning?true:false;

Open in new window

0
ozoCommented:
answerCell.Venn.png
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jim CakalicSenior Developer/ArchitectCommented:
Similar to krakatoa, my one line solution is
    return isAsleep ? false : isMorning ? isMom : true;

Open in new window

0
krakatoaCommented:
Similar to krakatoa
But sleeker. ;)

On the Venn - to anyone who wants to comment - there are 8 possible "sections" to resolve. Now I haven't given it a massive amount of thought, but I'm wondering to what extent a Venn can really help in designing an algorithm or method logic . . . except perhaps for graphically confirming that all cases are covered. But it doesn't look as if it makes a problem easier to resolve.


btw another one : return !isAsleep&&!isMorning||isMom&&!isAsleep?true:false;
0
ozoCommented:
isMorning ? isMom : true
reduces to
   !isMorning || isMom
and
   isAsleep ? false : !isMorning || isMom
reduces to
   !isAsleep && (!isMorning || isMom)

also,
  !isMorning?true:false
reduces to
  !isMorning
and
  (isMorning&&isMom?true:!isMorning)
reduces to
  (isMorning&&isMom||!isMorning)
which reduces to
  (!isMorningis||isMom)
0
ozoCommented:
In general,
  (boolean expression)?true:false
is the same as just
  (boolean expression)

  !isAsleep&&!isMorning||isMom&&!isAsleep?true:false;
is the same as
  !isAsleep&&!isMorning||isMom&&!isAsleep
and
  (!isAsleep&&!isMorning)||(isMom&&!isAsleep);
distributes to
  !isAsleep&&(!isMorning||isMom);
0
gudii9Author Commented:
In general,
  (boolean expression)?true:false
is the same as just
  (boolean expression)

if this is case

then why people tend to write long code like below
public bolean methodA(){
if(condition){

return true;
}
else(condition(){
return false;
}

}
instead wrte as belwo

public bolean methodA(){
return (boolean expression)

}

or as even more short cut to (boolean expression)?true:false
0
krakatoaCommented:
As I've mentioned before, a good way to move toward robust method formulation is to use clearly visible  signposts for your logic - in other words to write in such terms as :

if (a==1){ return true;}
else {return false;}

then everyone, including you, can see immediately what the intention is.

But once you have your logic right, you can sometimes take shorter routes, so in the above you could write :

return a==1;

The Ternary operator is just an inline 'if' statement really, which, like 'if', can also be nested.
0
ozoCommented:
I don't know.
The unnecessary clutter seems to make it harder to see what is important.
But in other circumstances, if the statements inside the conditions have side effects,
then it possible that the long code could add clarity.
If there is a possibility that the code could have to be modified to require such statements with side effects,
then it may be useful for the code structure to reflect that, making it easier to imagine the possible variations.
If the code had originally evolved from other code in which it would have made sense to have a lot of extra statements, then the extraneous structure could be just a historical remnant.
In some cases it may be informative to leave remnants of history, or not worth cleaning it up.
But if the history is not important, it could be a distraction to the reader who may be left wondering what other things you might have been thinking of and what you thought might be useful to do with the the extra structure.
0
krakatoaCommented:
public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
  int morn,mum,sleeping,sum;
  
  morn = isMorning?-1:1;
  mum = isMom?2:0;
  sleeping = isAsleep?-3:0;
  
  sum = morn+mum+sleeping;
  
  return sum>=1;
  
}

Open in new window


:)
0
gudii9Author Commented:
public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
  
if(isAsleep){

return false;
}

 else if(!isMorning){

return true;
}



else{




if(isMom){

return true;
}

else 

return false;



}
  
}

Open in new window


i wrote as above and passed all tests
0
gudii9Author Commented:
this kind of flow charts helping me to understand better at this stage than venn or tru false table..not sure if there are any limitations of flow charts?
MomCall.png
0
gudii9Author Commented:
return isAsleep?false:isMorning&&isMom?true:!isMorning?true:false;


    return isAsleep ? false : isMorning ? isMom : true;



return !isAsleep&&(isMom||!isMorning);

Open in new window


not able to understand clearly above 3 lines?please advise

how do i start constructing above one line solutions beginning my flow chart posted in last post?
0
gudii9Author Commented:
 return isAsleep ? false : isMorning ? isMom : true;

Open in new window

\

if Asleet it true then return false which is fine
if Asleep false then check morning true or false
morning true check mom true or false
if mom true return true which is all fine
what happened to mom fase morning false cases? we do not need to write them or is it by default understood by jvm something??
0
ozoCommented:
 return isAsleep ? false : isMorning ? isMom : true;
// more verbosely:
 return
    ( isAsleep )?(
         false
    ):(
       ( isMorning )?(
            isMom
       ):(
             true
       )
    )
 ;
// in other words:
   if( isAsleep ){
         return false;
   }else{
       if( isMorning ){
            return isMom;
       }else{
             return true;
       }
    }
// or more succinctly:
 return !isAsleep&&(!isMorning||isMom);

Open in new window

0
gudii9Author Commented:
return isAsleep ? false : isMorning ? isMom : true;

how to break above to below?
 if( isAsleep ){
         return false;
   }else{
       if( isMorning ){
            return isMom;
       }else{
             return true;
       }
    }

Open in new window


is there is rule or thumb rule for this?
0
ozoCommented:
It follows from the definition of the ?: operator
- and an intention style that always uses braces after an if() and cuddles elses at the same indentation level.
0
gudii9Author Commented:
It follows from the definition of the ?: operator
- and an intention style that always uses braces after an if() and cuddles elses at the same indentation level.

i got it

return isAsleep ? false : isMorning ? isMom : true;


how to break above to below?
 if( isAsleep ){
         return false;
   }else{
    //return true//or can result boolean still having one other if else
                                 if(){
                                       }
                                        else{
                                                 }//can be one other if else??
       }
    }


how many levels down we can go like this???
0
krakatoaCommented:
We've been over this "depth" and "nesting" question many times, and it's starting to look like we are repeating ourselves.

You can nest if and else and else if as many times as the Java language will allow. Jim cakalic and mccarl already said that your brain would give up before you reached that level of nesting -so why are you worried about it? If you had a problem of that level of if-else ness, you would be implementing it the wrong way by then.
0
gudii9Author Commented:
Not ? But : means we almost reached end of nesting right
0
ozoCommented:
You don't reach the end until you have as many : as ?
0
gudii9Author Commented:
You don't reach the end until you have as many : as ?

i was not aware of this fact.
0
ozoCommented:
Does that mean you thought you could have ? without :
0
gudii9Author Commented:
i thought ? could have more than one : which is wrong.

where i can read all about ternary operator at great great detail.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.