Results 1 to 7 of 7

Thread: Abs() Function???

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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....

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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....???

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    OK...thanks for answering my dumb question.

  6. #6
    Hyperactive Member bsw2112's Avatar
    Join Date
    Nov 2001
    Location
    ottawa, canada
    Posts
    292
    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

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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
  •  



Click Here to Expand Forum to Full Width