Monday, February 27, 2012

Can I sleep in?

So the term "Program" in "Program a day" is a little bit misleading. It can refer to yes, a program, or just a class, a method, a function, or just implementing an example of a technique.


A little simple, but here's the idea. Time starts as I finish reading this: "The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in."

public boolean sleepIn(boolean weekday, boolean vacation) {
     if(!weekday && vacation){
          return true;
     }
     else if(weekday && vacation){
          return true;
     }
     else if(!weekday && !vacation){
          return true;
     }
     else
          return false;
}
Total time: 2:19


In a lot of my coding, I have a lot of the if else's, so there's a way to cut back on making it so wordy. Rereading what to do, "Sleep in if not weekday, or on vacation". This one if statement fills both requirements, instead of making all statements for all requirements. The actual answer was:
  
{
     if(!weekday || vacation)
          return true;
     else 
          return false; 
}

Just know it for next time! 
 

No comments:

Post a Comment