Solved

return statement

Posted on 2014-10-29
13
138 Views
Last Modified: 2014-10-30
Hi,

I am going through below challenge solution

http://codingbat.com/prob/p140449

I have not understood below return statement.

  return (talking && (hour < 7 || hour > 20));

Please advise.
public boolean parrotTrouble(boolean talking, int hour) {
  return (talking && (hour < 7 || hour > 20));
  // Need extra parenthesis around the || clause
  // since && binds more tightly than ||
  // && is like arithmetic *, || is like arithmetic +
}
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
13 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 167 total points
ID: 40411054
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
&& is  Conditional-AND;  false when left operand (talking) is false, otherwise evaluate the second operand
|| is Conditional-OR;  true when the left operand (hour < 7) is true, otherwise evaluate the second operand (hour > 20)

Without the parentheses, talking && hour < 7 || hour > 20 would evaluate as ((talking && hour < 7) || hour > 20)
0
 
LVL 35

Assisted Solution

by:Kimputer
Kimputer earned 166 total points
ID: 40411072
While I don't understand the coding logic (why it returns, for what reason), interpreting the code is simple.

 parrotTrouble(True, 1) = True
 parrotTrouble(True, 8) = False
 parrotTrouble(True, 21) = True
 parrotTrouble(False, 1) = False
 parrotTrouble(False, 8) = False
 parrotTrouble(False, 21) = False

It just returns the AND of the two variables. Talking is either True or False, it's already input as the first variable.
The second part is evaluated from the second variable. If hours is below 7 OR above 20, that second part is True (otherwise False).
With these two outcomes, you do the AND operation again for the final result.
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 167 total points
ID: 40411307
>>talking && (hour < 7 || hour > 20)

Quite simply - talking has to be true, and hour has to be EITHER less than 7 OR more than 20, then a final true is returned - otherwise false is returned of course.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:krakatoa
ID: 40411389
Thanks.

Footnote : (now you've done the challenge) -

This would be a way of splitting the logic up also :

public boolean parrotTrouble(boolean talking, int hour) {

   if (talking==false) {return talking;}
  return (hour<7||hour>20);
  
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40411410
//If you're going to split it up
 if( !talking ){ return false; }
  if( hour<7 ){ return true; }
  return hour>20;
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40411421
You can do that too of course, but I was sticking to the illustration of keeping the ORs together.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40411427
And don't forget our old friend Mr Ternary

return talking = talking==true?hour<7||hour>20?true:false:false;

Open in new window

0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40411445
This might also be an option at some point :

if ((!(talking==false))&& !((hour>6)&&(hour<21))){return true;}
           else{return false;}

Open in new window



- which is just for people who don't like ors. ;)

OR should it be :

if ((!(talking==false))&& !((hour>6)&&(hour<21))){return !false;}
           else{return !true;}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40411496
Presumably those are just meant to be silly, but
 !((hour>6)&&(hour<21))
is not equivalent to
 (hour < 7 || hour > 20)
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40411519
They are not silly inasmuch as they all pass the codingbat tests - so maybe the tests are silly.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40411540
Remember that >6 and <21 are the hours that ARE ok to speak.

(and so for the record, your ID: 40411496 is not correct).

You can look at it this way :
"<x OR >y" MUST be the complement of ">=x AND <=y".
0
 
LVL 84

Expert Comment

by:ozo
ID: 40413299
Sorry, I misread "!((>x-1)&&(<y+1))", expecting  ">=x AND <=y" (one of the reasons to avoid silly extraneous transforms)
!((hour>6)&&(hour<21)) is equivalent when hour is an int
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40413339
Right, ok.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 62
ejb example issues 3 85
sql import cannot be resolved jsp 3 110
Eclipse with various Java releases 7 82
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question