gudii9
asked on
squirrelPlay java challenge
Hi,
i anm working on below challenge
http://codingbat.com/prob/p141061
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
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;
}
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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;
}
above also errorr
@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.
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.
ASKER
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;
}
}
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
ASKER
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;
}
}
i fixed now there is additional extracurly bracket hanging there
ASKER
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;
}
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
ASKER
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;
}
now all passed. I wonder how can i improve my code. please advise
I've already given u the improved code. Or even one better by rrz.
Read carefully
Read carefully
ASKER
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;
}
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;
}
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.
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.
codingbat living up to its limitations again:
return (temp>=90&&temp<=100)&&isSummer?true:temp>=60&&temp<=90;
return (temp>=60&&temp<=100)&&isSummer?true:temp>=60&&temp<=90;
ASKER
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?
yes
in general,
if( x ){
return true;
}else{
return false;
}
is equivalent to
return x?true:false;
or just
return x;
in general,
if( x ){
return true;
}else{
return false;
}
is equivalent to
return x?true:false;
or just
return x;
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;
return (temp < 60) ? false : (temp < 90) ? true : (isSummer && temp <=100) ? true : false;
@awking00
Your code doesn't pass all the tests.
Your code doesn't pass all the tests.
(temp < 90) should have been (temp <= 90) :-(
ASKER
if( x ){
return true;
}else{
return false;
}
is equivalent to
return x?true:false;
i got above explanation.
Not below
or just
return x;
please advise
ASKER
public boolean squirrelPlay(int temp, boolean isSummer) {
return (temp<60)?false:(temp < =90)?true:(isSummer && temp<=100)?true : false;
}
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
ASKER
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
you have a extra space character in "< =90". It should be <= not < =
ASKER
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==tr
return true;
else
return false;
}
gudii9 please one question at a time. Flooding the same post with questions is going to become difficult to manage.
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);
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);
ASKER
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
By "you started with" I meant the code you posted in your initial question.
ASKER
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;
}
}
i will remember this approach
ASKER
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
ASKER
ID: 41146724
what is the difference between first and second approach?
Not clear. please advise
what is the difference between first and second approach?
Not clear. please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
(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?
ASKER
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
ASKER
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
If i see more than one ? then my mind is freezing thinking it is some rocket science
If you understand one "iteration" then you understand it the rest is pasta.
This question began 29th October!
ASKER
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)
October - long way to go yet😈
for each "?" just consider a truth condition gone. And if it's nested, then repeat your thoughts.
return 60<=temp && temp<=(isSummer?100:90);