Java > Logic-1 > cigarParty (CodingBat Solution)

Problem:

When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return true if the party with the given values is successful, or false otherwise.

cigarParty(30, false) → false
cigarParty(50, false) → true
cigarParty(70, true) → true


Solution:

public boolean cigarParty(int cigars, boolean isWeekend) {
  if (isWeekend == true && cigars >= 40)
    return true;
  if (!(isWeekend) && cigars >=40 && cigars <= 60)
    return true;
  else
    return false;
}


5 comments :

  1. public boolean cigarParty(int cigars, boolean isWeekend) {
    return isWeekend ? cigars >= 40 : cigars >= 40 && cigars <= 60;
    }

    ReplyDelete
  2. if(cigars<40||(cigars>60&&!isWeekend))return false;
    return true;
    //Shorter way

    ReplyDelete
  3. public boolean cigarParty(int cigars, boolean isWeekend) {
    return (40 <= cigars) && (cigars <= 60 || isWeekend );
    }

    ReplyDelete
  4. In JavaScript:

    function cigarParty(cigars, isWeekend){
    if(((cigars > 39) && isWeekend) || ((cigars > 39 && cigars <= 60) && !isWeekend)) {
    return true
    }

    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