Solved

comparing sign of two numbers

Posted on 2014-10-02
6
109 Views
Last Modified: 2014-10-02
Hi,
I was going through below link
http://codingbat.com/prob/p192082



I wrote solution as below

public boolean icyHot(int temp1, int temp2) {
  if(temp1<0 && temp2>100)
  {
  return true;
  }
 
   if(temp1>0 && temp2<100)
  {
  return true;
  }
  return false;
}



I saw given solution as below


public boolean icyHot(int temp1, int temp2) {
  if ((temp1 < 0 && temp2 > 100) || (temp1 > 100 && temp2 < 0)) {
    return true;
  } else {
    return false;
  }
  // Could be written as: return ((temp1 < 0 && ...));
}

can the natural flow of next line can be written always after || (which is the only difference i see from my program.

Please advise
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
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 40357829
if( a ){
  return true;
}
if( b ){
  return true;
}
return false;

is equivalent to

if( (a) || (b) ){
  return true;
}else{
  return false;
}
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40357836
Can you please clarify your question?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40357893
public boolean icyHot(int temp1, int temp2) {


if( (a) || (b) ){
  return true;
}


}

Open in new window


why above code wont compile?  Giving return statement in if loop alone is not sufficient. Do i need to give outside if loop also?
please advise.
0
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.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
ID: 40357898
Because you don't have a return statement outside of the if block. Think about it this way:  If your code never entered the if block, what would it return? We cannot tell because you have not defined anything.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40357904
Because you didn't define a and b

(The equivalent form would also not compile for equivalent reason)

I see no loop in your code, so I don't understand your last comment.
0
 
LVL 27

Expert Comment

by:dpearson
ID: 40357905
Your original solution is fine and just as good as the "given solution" on the website.

As you said, the only difference is that they put the two conditions together with an "||" (or) and you broke them out into separate lines.  Either approach is fine.

I think you misunderstood what ozo posted.  He was basically saying that the two solutions are equivalent (he just said it in code).

Doug
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 74
JavaFX TableView not displaying correctly 3 109
web project error add remove 1 56
Code Coverage and Coding Standards 2 51
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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…

730 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