Java > Logic-1 > squirrelPlay (CodingBat Solution)

Problem:

The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean isSummer, return true if the squirrels play and false otherwise.

squirrelPlay(70, false) → true
squirrelPlay(95, false) → false
squirrelPlay(95, true) → true


Solution:

public boolean squirrelPlay(int temp, boolean isSummer) {
  if (isSummer && temp <= 100 && temp >= 60)
    return true;
  if (!(isSummer) && temp <= 90 && temp >=60)
    return true;
  else
    return false;
}


8 comments :

  1. Bad solution.
    see that, you have used temp>=60 twice and in both cases it is returning true.
    Try differently.

    ReplyDelete
  2. public boolean squirrelPlay(int temp, boolean isSummer) {
    return ((temp >= 60 && temp <= 90) || (isSummer && temp >= 60 && temp <= 100));
    }

    ReplyDelete
  3. public boolean squirrelPlay(int temp, boolean isSummer) {
    return (!isSummer && 60 <= temp && temp <= 90) || (isSummer && 60<= temp && temp <= 100);
    }


    ReplyDelete
  4. public boolean squirrelPlay(int temp, boolean isSummer) {
    return (isSummer? temp>=60 && temp <=100 : temp>=60 && temp <=90 );
    }

    ReplyDelete
  5. public boolean squirrelPlay(int temp, boolean isSummer) {
    return 60 <= temp && (temp <= 90 || temp <= 100 && isSummer);
    }

    ReplyDelete
  6. public boolean squirrelPlay(int temp, boolean isSummer) {

    if((isSummer) && (temp >= 60 && temp <= 100))
    return true;
    else if (!(isSummer) && (temp >= 60 && temp <= 90))
    return true;
    else
    return false;




    }

    ReplyDelete
  7. public boolean squirrelPlay(int temp, boolean isSummer) {
    if(temp>=60&&((temp<=90&&!isSummer)||(temp<=100 && isSummer))){
    return true;
    }else{
    return false;
    }
    }

    ReplyDelete
  8. In JavaScript:

    function squirrelPlay(temp, isSummer){
    if((temp >= 60 && temp <= 100 && isSummer) || ((temp >= 60 && temp <= 90) && !isSummer)) {
    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