squirrelPlay java challenge

Hi,
i anm working on below challenge
http://codingbat.com/prob/p141061

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

Open in new window



i am getting below error

Compile problems:


Error:      if((60<=temp<=90) && isSummer==false)){
                                           ^
Syntax error on token ")", delete this token



How to fix and improve my code. 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:
You may have meant
if( (60<=temp && temp<=90) && isSummer==false ){
0
ozoCommented:
or you could
   return 60<=temp && temp<=(isSummer?100:90);
0
gurpsbassiCommented:
Its obvious isn't it?
you have an extra right bracket.


maybe something like this:

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

Open in new window

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.

rrzCommented:
I like  gurpsbassi code but one better.
public boolean squirrelPlay(int temp, boolean isSummer) {
  int lowerLimit = 60;
  int upperLimit = 90;
  
  if(isSummer){
    upperLimit = 100;
  }
  return(temp >= lowerLimit && temp <= upperLimit);   
}

Open in new window

0
gudii9Author Commented:
public boolean squirrelPlay(int temp, boolean isSummer) {
 boolean play;
 
 if((isSummer)&((60<=temp)&&(temp<=90))){
 

 return true;
 } 
 
 }
 
 
else if((!isSummer)&((60<=temp)&&(temp<=100))) {
 

 return true;
 }
 
 else 
 return false;
 
}

Open in new window


above also errorr
0
gurpsbassiCommented:
@guddi 9 - please use a java compiler
Dont rely on the online site to compile your code for you.

Use your IDE please.

you have compilation errors you need to sot out.
0
gudii9Author Commented:
public class TestSquirrel {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(squirrelPlay(95, false));

	}
	public static boolean squirrelPlay(int temp, boolean isSummer) {
		 boolean play;
		 
		 if (isSummer&((60<=temp)&&(temp<=90)) ){
		 

		 return true;
		 } 
		 
		 }
		 
		 
		else if ( (!isSummer)&&((60<=temp)&&(temp<=100)) ) {		 

		 return true;
		 
		 }
		 
		 else 
		 return false;
		 
		}

}

Open in new window


i wrote in eclipse still has compilation errors at line 24 and 26 as below

Syntax error on token "else", { expected

and other

Void methods cannot return a value

Not sure why. Please advise
0
gudii9Author Commented:
public class TestSquirrel {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(squirrelPlay(95, false));

	}
	public static boolean squirrelPlay(int temp, boolean isSummer) {
		 boolean play;
		 
		 if (isSummer&((60<=temp)&&(temp<=90)) ){
		 

		 return true;
		 } 
		 
		 
		 
		 
		else if ( (!isSummer)&&((60<=temp)&&(temp<=100)) ) {		 

		 return true;
		 
		 }
		 
		 else 
		 return false;
		 
		}

}

Open in new window


i fixed now there is additional extracurly  bracket hanging there
0
gudii9Author Commented:
public boolean squirrelPlay(int temp, boolean isSummer) {
 boolean play;
 
 if (isSummer&((60<=temp)&&(temp<=90)) ){
 

 return true;
 } 
 
 
 
 
else if ( (!isSummer)&&((60<=temp)&&(temp<=100)) ) {
 

 return true;
 }
 
 else 
 return false;
 
}

Open in new window


i fail 4 test cases as below now
Expected      Run            
squirrelPlay(70, false) → true      true      OK         
squirrelPlay(95, false) → false      true      X         
squirrelPlay(95, true) → true      false      X         
squirrelPlay(90, false) → true      true      OK         
squirrelPlay(90, true) → true      true      OK         
squirrelPlay(50, false) → false      false      OK         
squirrelPlay(50, true) → false      false      OK         
squirrelPlay(100, false) → false      true      X         
squirrelPlay(100, true) → true      false      X         
squirrelPlay(105, true) → false      false      OK         
squirrelPlay(59, false) → false      false      OK         
squirrelPlay(59, true) → false      false      OK         
squirrelPlay(60, false) → true      true      OK         
other tests
OK      
0
gudii9Author Commented:
public boolean squirrelPlay(int temp, boolean isSummer) {
 boolean play;
 
 if (isSummer&((60<=temp)&&(temp<=100)) ){
 

 return true;
 } 
 
 
 
 
else if ( (!isSummer)&&((60<=temp)&&(temp<=90)) ) {
 

 return true;
 }
 
 else 
 return false;
 
}

Open in new window


now all passed. I wonder how can i improve my code. please advise
0
gurpsbassiCommented:
I've already given u the improved code. Or even one better by rrz.
Read carefully
0
gudii9Author Commented:
public boolean squirrelPlay(int temp, boolean isSummer) {
 boolean play;
 
 if( (60<=temp && temp<=100) && isSummer==true){
 

 return true;
 } 
 
 
 
 
if( (60<=temp && temp<=90) && isSummer==false ){
 

 return true;
 }
 
 else 
 return false;
 
}

Open in new window


Above also worked fine. Does it matter order of checking conditions.

I mean is there is any differnece between above and below approach
public boolean squirrelPlay(int temp, boolean isSummer) {
 boolean play;
 
 if( isSummer==true&& (60<=temp && temp<=100) ){
 

 return true;
 } 
 
 
 
 
if( isSummer==false&&(60<=temp && temp<=90)  ){
 

 return true;
 }
 
 else 
 return false;
 
}

Open in new window

0
gurpsbassiCommented:
I would not use the above or below approaches.

Why don't you consider the approach myself and rrz have given. They are better than your approaches.

I would not use your code as it is, in a production environment.
0
krakatoaCommented:
codingbat living up to its limitations again:

return (temp>=90&&temp<=100)&&isSummer?true:temp>=60&&temp<=90;

Open in new window

return (temp>=60&&temp<=100)&&isSummer?true:temp>=60&&temp<=90;

Open in new window

0
gudii9Author Commented:
if(temp >= lowerLimit && temp <= upperLimit){
    return true;
  }
 
  return false;



how above can be merged as below

  //  return(temp >= lowerLimit && temp <= upperLimit);

true && true then result is true
if either is false then result is false rigtht?
0
ozoCommented:
yes

in general,
 if( x ){
   return true;
 }else{
   return false;
 }
is equivalent to
  return x?true:false;
or just
  return x;
0
awking00Commented:
Similar to a previous CodingBat challenge. You seem to get caught up with the range condition. If you eliminate everything below the range first (returning false), then everything remaining has to be above the low end of the range, so then take what's below the upper end of the range (returning true), leaving everything above the upper range, then if it's summer and the temp is equal to or below 100, return true else return false.
return (temp < 60) ? false : (temp < 90) ? true : (isSummer && temp <=100) ? true : false;
0
krakatoaCommented:
@awking00

Your code doesn't pass all the tests.
0
awking00Commented:
(temp < 90) should have been (temp <= 90) :-(
0
gudii9Author Commented:
if( x ){
   return true;
 }else{
   return false;
 }
is equivalent to
  return x?true:false;

i got above explanation.

Not below

or just
  return x;

Open in new window


please advise
0
gudii9Author Commented:
public boolean squirrelPlay(int temp, boolean isSummer) {
return (temp<60)?false:(temp < =90)?true:(isSummer && temp<=100)?true : false;
  
}

Open in new window


above gives below error

Compile problems:


Error:      return (temp<60)?false:(temp < =90)?true:(isSummer && temp<=100)?true : false;
                                   ^^^
Syntax error on tokens, they can be merge to form <=


see Example Code to help with compile problems
0
gudii9Author Commented:
Similar to a previous CodingBat challenge. You seem to get caught up with the range condition.

i am getting struck at some blocks and now noticing them as i discuss with you all experts to correct
0
gurpsbassiCommented:
you have a extra space character in "< =90". It should be <= not < =
0
gudii9Author Commented:
If you eliminate everything below the range first (returning false), then everything remaining has to be above the low end of the range, so then take what's below the upper end of the range (returning true), leaving everything above the upper range, then if it's summer and the temp is equal to or below 100, return true else return false.
return (temp < 60) ? false : (temp < 90) ? true : (isSummer && temp <=100) ? true : false;

return (temp<60)?false:(temp < =90)?true:(isSummer&& temp<=100)?true: false;
above can be written as below as we discussed earlier few times



  if(temp<60)
return false;
else if(temp<=90)
return true;
else if(temp<=100&&isSummer==true)
return true;
else
return false;
}
0
gurpsbassiCommented:
gudii9 please one question at a time. Flooding the same post with questions is going to become difficult to manage.
0
ozoCommented:
return (temp<60)?false:(temp <=90)?true:(isSummer&& temp<=100)?true: false;
return (temp<60)?false:(temp <=90)?true:(isSummer&& temp<=100);
return !(temp<60) && (temp <=90 || (isSummer&& temp<=100));
return !(temp<60) && temp <= (isSummer?100:90);
0
gudii9Author Commented:

 For example, in the squirrelplay challenge you started with "if temp >= 60 and temp <= 90 and isSummer = false, this would return true regardless of it beng summer or not.

which of my code. I have lot my own versions of code there?
please advise
0
awking00Commented:
By "you started with" I meant the code you posted in your initial question.
0
gudii9Author Commented:
is temp < 60 ---> Yes ---> return false
     |
   No
     |
    V
is temp <= 90 ---> Yes ---> return true
     |
    No
     |
    V
is temp <= 100 ---> No ---> return false
     |
   Yes
     |
    V
is it summer ---> Yes ---> return true
     |
    No
     |
    V
return false

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



}

Open in new window

i will remember this approach
0
gudii9Author Commented:
return (temp>=90&&temp<=100)&&isSummer?true:temp>=60&&temp<=90;

this is not clear to me as usual. I think my mind need to raise above all the flow controls loops to ternary loops still.

I am getting there but not there yet completely
0
gudii9Author Commented:
ID: 41146724

what is the difference between first and second approach?

Not clear. please advise
0
krakatoaCommented:
Think of the ternary as a kind of inline if statement, where the truth is after the "?" and the false condition follows the ":"

So . .  

if(a==b) {z=1;}
else{z=2;)

would be

z = a==b?1:2;
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
gudii9Author Commented:
(temp>=90&&temp<=100)&&isSummer?true

i got above before ?
 if all that stuff LHS before ? true then make RHS (right hand side of ?)true

what is not clear is later part if
 if all that stuff LHS before ? false then go and do this stuff
 temp>=60&&temp<=90
and print that boolean?
0
gudii9Author Commented:
if(a==b) {z=1;}
else{z=2;)

would be

z = a==b?1:2;

i got this concept which is pretty simple to me which says equate z to 1 or 2 depending on boomean value of the condition check..
 but in challenge i am not able to visualize esp when the length of line goes on and on
0
gudii9Author Commented:
i guess my main main challenge is with more than one ?

If i see more than one ? then my mind is freezing thinking it is some rocket science
0
krakatoaCommented:
If you understand one "iteration" then you understand it the rest is pasta.
0
gurpsbassiCommented:
This question began 29th October!
0
gudii9Author Commented:
return (temp>=90&&temp<=100)&&isSummer?true:temp>=60&&temp<=90;

i see now

you are coming from bigger restriction(if temp between 90,100 also is summer) to smaller restriction(where temp between 90,100 without summer)
0
krakatoaCommented:
October - long way to go yet😈
0
krakatoaCommented:
for each "?" just consider a truth condition gone. And if it's nested, then repeat your thoughts.
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.