Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I'm doing a practice test for my Introductory Java class. It's fill in the blank and our teacher wants us to write a simple if statement for various problems. The one I'm stumped with is below. We aren't supposed to use Math.abs(); and we're apparently only supposed to write one line of code.


3. x absolute value is less than 100
if ( )


any hints on how to accomplish this would be appreciated!

Thank you,
Matt
Posted
Comments
Gian rebollido 26-Mar-13 1:49am    
can we?? i don't think so..:)

You don't have to use Math. You can simply write:

Java
if (x*x < 10000) //...


Simple, isn't it?

—SA
 
Share this answer
 
Comments
msg1991 22-Feb-13 2:11am    
This solution seems like it would apply perfectly to the question. I didn't think it would be this simple! Thank you :)
Sergey Alexandrovich Kryukov 22-Feb-13 11:31am    
Sure. You are very welcome.
Good luck, call again.
—SA
Very simply try
if (x>100 || x < -100)
{
  return true;
}
else
{
  return false;
}
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 22-Feb-13 1:40am    
No! Are you serious?! It's just:

return (x>100 || x < -100); //!!!

—SA
Abhinav S 22-Feb-13 1:52am    
I doubt that he just wants to return boolean values. Just made it cleared for the OP.
He can replace true and false with his own implementation.

Sergey Alexandrovich Kryukov 22-Feb-13 1:56am    
No matter, if someone looks just at you code sample, what that person should think?..
—SA
Abhinav S 22-Feb-13 1:57am    
So long as the OP understands, why would I care what people think. :)
msg1991 22-Feb-13 1:44am    
I'm confused by your code, it applies to x > 100 and x < -100 which is outside of the bounds of the absolute value of x < 100.

After thinking more on the topic, wouldn't the solution be
if (x => 0 && x <= 100)?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900