|
-
Jan 26th, 2003, 02:13 PM
#1
Abs() Function???
Is it a function or something in java that makes negativ numer to positiv numbers. Do I have to import something like the math thing or something....thanks in advance.
I'm using NetBeans from Sun...and I can't belive that there is no help file for these kind of questions in it....
-
Jan 26th, 2003, 03:41 PM
#2
use Math.abs()
Code:
System.out.println(Math.abs(-4));
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jan 26th, 2003, 05:50 PM
#3
Thanks that worked. But just an other question. If I import the Math "thingy" like this...
import java.math.*;
shouldn't I then be able to write
abs(MyInteger);
in stead of
math.abs(MyInteger);
or what did I not understand about the import keyword....???
-
Jan 26th, 2003, 05:58 PM
#4
You use import for packages so that you don't have to specify the entire path to the classes it contains. eg...
Code:
import java.awt.*;
//...
Frame fra = new Frame();
instead of...
Code:
//...
java.awt.Frame fra = new java.awt.Frame();
imports can also be used for individual classes, too. Like in the above example, if all you will ever reference from the java.awt package is the Frame class, then you can do...
Code:
import java.awt.Frame;
to make it easier.
But to answer your question, no, it wouldn't work that way. abs() is a static method of the Math class. So you need to state Math.abs(...), otherwise, how will it know where to look for the method?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jan 26th, 2003, 06:03 PM
#5
OK...thanks for answering my dumb question.
-
Feb 3rd, 2003, 10:59 AM
#6
Hyperactive Member
btw: you don't import the math "thingy" ie class 
Math class is part of a package called java.lang
which is imported behind the scenes for you
Code:
public class Test
{
public static void main(String args[])
{
double someVal = -10.0;
someVal = Math.abs(someVal);
System.out.print(someVal);
System.exit(0);
}
}
I also show here the use of class System that is also stored
in java.lang
bsw
-
Feb 3rd, 2003, 12:10 PM
#7
OK...thanks, for that one too...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|