Trig functions in the Math class

One of the techniques suggested to master for the Java Programmer exam is using the trigonometry methods in the Math class. Does that mean that on the exam I would need a trigonometry knowledge or is it enough to be familiar with their signature ?

No trig knowledge - just questions on the Java language, including method signatures. Check out the various mock exams such as at http://www.javaranch.com/certfaq.jsp

Similar Messages

  • Speed of the Math class

    If I am performing a lot of calculations is it best to use a Math class function or write it yourself (best in terms of speed not accuracy).
    e.g. Math.pow(a-b,2) OR (a-b) * (a-b)
    I am grateful for any opinions.

    Your version would allow the compiler to use the fact that the expression is completely knowable at compilation time, providing for a trivial solution for the in-line multiplication approach.
    The version below avoids thatpublic class Test {
        public static void main(String[] args) {
            long start = 0;
            long end = 0;
            long powtime = 0;
            long multtime = 0;
            double x = Double.parseDouble(args[0]);
            double y = Double.parseDouble(args[1]);
            System.out.println("Starting the pow test.");
            start = System.currentTimeMillis();
            for (int i = 0; i < 10000000; i++) {
                double value = (x - y) * (x - y);
            end = System.currentTimeMillis();
            multtime = end - start;
            System.out.println(
                "By using multiplication it took: " + ((double) multtime / 1000) + " seconds.");
            start = System.currentTimeMillis();
            for (int j = 0; j < 10000000; j++) {
                double value = Math.pow((x - y), 2);
            end = System.currentTimeMillis();
            powtime = end - start;
            System.out.println(
                "By using Math.pow() it took: " + ((double) powtime / 1000) + " seconds.");
    }With this I get a 6:1 (1.272 to 0.200) ratio of Math.pow() to inline multiplication time. Still a fairly large ratio.
    Playing with double, int and using increasing larger values of the exponent, the Math.pow() seems to be far slower that multiplication. Of course Math.pow() can handle complex exponents (2.3645463) and take roots while the simple multiplication approach cannot.
    Chuck

  • Calling functions of the inner class in .java code file

    Hello,
    I created a .java code file in Visual J#.Net and converted it into
    the application by adding the "public static void main(String args[])"
    function.
    I have created the two classes one extends from Applet, and the other
    extends from Frame. The class which I inherited from the Frame class becomes
    the inner class of the class extended from the Applet. Now How do I
    call the functions of the class extended from Frame class - MenuBarFrame
    class. the outline code is
    public class menu_show extends Applet
    ------init , paint action function---------
    public class MenuBarFrame extends Frame
    paint,action function for Menu
    public static void main(String args[])
    applet class instance is created
    instance of frame is created
    Menu , MenuBar, MenuItem instance is created
    and all these objects added
    I have Created MenuBarFrame class instance as
    Object x= new menu_show().new MenuBarFrame
    ????? How to call the functions such as action of MenuBarFrame class - what
    should be its parameters??????
    }

    Here's how I would do it:
    interface Operation {
        public int op(int y);
    class X {
        private int x;
        public X(int x) {
            this.x = x;
        private class Y implements Operation {
            public int op(int y) {
                return x+y;
        public Operation createOperation() {
            return new Y();
        public static void main(String[] args) {
            X app = new X(17);
            Operation f = app.createOperation();
            System.out.println(f.op(-11));
    }Your code, however, has some serious "issues". You typically don't
    instantiate an applet class -- that's the job of the applet viewer or Java plugin
    your browser is using. If you instantiate the applet directly, you're going
    to have to supply it an AppletStub. Do you really want to go that way?
    Again, use an applet viewer or browser, or better yet, why write applets at all?

  • The connect function in the socket class has problems

    when i call this function connect(SocketAddress endpoint, int timeout)
    the timeout variable doesnt wait for the specific time that i tell it
    example
    connect(endpoint, 10000);
    this call should wait for this amount of time but it doesnt wait that amount
    any body has a answer ???

    yes that is exactly what I am saying
    I specify 10 sec, as 10000 milli sec's in the connect function but I get an exception almost imediately when i run my java program...
    run this code
    import java.net.*;
    public class Test
    public static void main(String [] args)
    { Socket s = null;
    int port = 2000;
    try
    s = new Socket();
    InetSocketAddress i = new InetSocketAddress("localhost",port);
    //this next line should wait 10 sec's then throw and exception
    //but it throws and exception upon running the this code
    s.connect(i,10000);
    catch(Exception e){ System.out.println(e.toString()); }
    }//main
    }//Test
    I have also tried setting the setSoTimeout(10000) in the Socket class but
    still with no success...
    I dont know why this doesnt wait 10 sec's then through an exception on it
    and when i try it with a java server it connects instantly...
    any help anyone???

  • Extending Math class

    As I understand, in actionscript 2 you could extend flash's Math class to add your own functions, but this is no longer possible in actionscript 3.  I want to add a mod() function to the Math class, and I'm assuming I have to write a wrapper class to accomplish this.  How do I import the existing Math class into my MathPlus or whatever class?  Also, is there anything else I should know about this?

    Thanks for the replies (very good to know about %).  The only reason I would prefer to extend Math or build a wrapper around it is if I were to write a sufficient number of additions such that trying to remember which were in Math and which were in my custom class became difficult.  I just think it would be nice to have access to all of my (and Flash's) math related code within one class.
    It just occurs to me that I just need to make functions in my class that call the ones in Math.

  • How can i add a new function to the Basic IVI class vi's?

    I need to use the IVI vi's to control some instruments, let's say a DC Power Supply.
    Since there's a DC Power supplyclass, i can used those VI's but waht if one of the VI's does not contains all the functions that i need?. How can i add more functions to the IVI classes?. What do i need to do to add a new function to the iviFGen class?

    If you would like to try developing your own instrument driver (or modify the existing one), we have documentation, model instrument drivers, and driver templates to help at :
    http://www.ni.com/devzone/idnet/development.htm
    We also have a syndicate of third party vendors that specialize in National Instruments' products and services. Some of the vendors specialize in driver development. I would suggest contacting one of the Alliance members at:
    http://www.ni.com/alliance

  • Java ME SDK 3.0 Math Class

    Using Eclipse and Java ME SDK 3.0
    When the Application Descriptor configuration is set to use DefaultCldcPhone1 or other emulators in the Java ME SDK 3.0 many of the Math class methods such as Math.sqrt(double arg0) are not available. However if I change the configuration to use a Nokia emulator all the Math class methods become available.
    This issue has just shown up since moving from WTK25 to Java ME SDK 3.0
    Note:
    Under Eclipse/Preferences/Java ME/Device Management the Configuration of all the emulators from the Java ME SDK 3.0 is listed as CLDC-1.0 whereas the other emulators from Nokia and from the Sun Java Wireless Toolkit 2.5 are listed as CLDC-1.1.

    It is related to the configuration of the emulator (CLDC 1.0 or CLDC 1.1)...
    If the configuration is CLDC 1.0 all the floating point data types (float, double, Float, Double) will not be available...
    If you change the configuration to CLDC1.1 , then all the previous types will be available.
    PS: I don't know how to do this in EclipseME

  • Best way to call a function in a generic class from a base class

    Hi,
    I have a generic class that works with subclasses of some other baseclass. When I construct the subclass, I call the constructor of the baseclass which I want to call a function in the generic class. e.g. suppose I have the following classes
    public class List<T extends BaseClass>
        newTCreated(T t)
    }

    Sorry, I pressed Tab and Enter last time when typing the code so I posted without meaning to
    Hi,
    I have a generic class that works with subclasses of some other baseclass. When I construct the subclass, I call the constructor of the baseclass which I want to call a function in the generic class. e.g. suppose I have the following classes
    public class List<T extends BaseClass>
        public void newTCreated(T t)
            // add the t to some internal list
        public T getT(int index)
            // get the object from the internal list
    public class BaseClass
        public BaseClass(List<?> list)
            list.newTCreated(this);
    public class SubClass extends BaseClass
        public SubClass(List<SubCass> list)
            super(list);
    }This doesn't compile because of the call to newTCreated in the BaseClass constructor because BaseClass is not necessarily of type T. Is there any way of checking when I call the newTCreated function that the BaseClass is actually of type SubClass? I could either add the call explicitly in each SubClass's constructor or have a function addToList in BaseClass that is called from the BaseClass constructor but overloaded in each subclass but both of those rely on future subclasses doing the same. Or I could change the newTCreated function to take an argument of type BaseClass and then cast it to type T but this doesn't give a compilation error, only a runtime exception.
    It seems like there should be solution but having only recently started writing Generic classes I can't find it. Thanks in advance for any help,
    Tom

  • Why Math class does not have any constructor.

    Math class does not have any constructor.
    But all the classes have a default constructor. Then why the math class is not having any constructor. Is math class having some other constructor or is the default constructor concept applicable only for user defined classes.

    Math class does not have any constructor.
    But all the classes have a default constructor. Then
    why the math class is not having any constructor. Is
    math class having some other constructor or is the
    default constructor concept applicable only for user
    defined classes.You cannot instantiate a new Math object, nor can you extend the Math class. All you can do is use Math's static methods and variables. Its a utility class, thats what its susposed to do.
    JJ

  • Question for the math lovers!  Using while loop and exponents

    Hi there Java geniuses! ; ) A little question here that has been perplexing me for the past few days. I'm trying to create a class that contains a single static method as follows:
    public static double exp(double a)
    It's supposed to work to calculate an "exp" value much like the Math.exp method of the Math class. Essentially, I need to keep calculating approximations of "exp" until a certain condition is met. This condition is specified in a while loop. Following is what I've written so far. But I can't seem to get the thing to work properly. Any suggestions/ advice would be greatly appreciated! Thanks.
    A class that computes the "exp" for a user-defined
    value.
    public class MyMath
    public static double exp(double x)
    double dbloldGuess;
    double dblnewGuess = 1 + x; // First value for newGuess
    double dblcounter = 1;
    do
    dblcounter++; // increments counter
    dbloldGuess = dblnewGuess;
    dblnewGuess = dbloldGuess + ((x ** dblcounter) / counter);
    while (Math.abs((newGuess - oldGuess) / newGuess) > 1e-15);
    return x;
    }

    ** wasn't an operator in C when I last checked...
    Anyway here's my lousy attempt:public static double exp(double a) {
            double guess = 1;
            int N = 18;
            while (N --> 1) guess = 1 + a/N * guess;
            return guess;
    }When called with 1.0 as argument gives the value equal to the constant Math.E, so is "probably" ok for arguments 0.0 -- 1.0. I wont guarantee anything else because this method is pretty pathological when it comes to loss of precision due to rounding, even if you increase N.

  • Question about Math Class

    I have a couple of Java questions I was wondering if anyone could answer:
    1). In this one statement, which takes the greatest common denominator and places it in the variable commom:
    int common = gcd (Math.abs(numerator), denominator);
    A).What is gcd? I looked it up and it's not a reserved word in the math class. In the class that this statement comes from there is no variable, object, or method named gcd. There is also no gcd variable, object, or invoked method in the client code that uses the class that this statement comes from.
    B ). I know that abs is a method of the math class for the absolute value of a number(in this case the number inside the variable numerator.). And I know that when the abs method is invoked it sends the variable 'numerator' as the parameter for the data to take the absolute value of. What I don't understand is the syntax of this statement in regards to how you can take the absolute value of the variable 'denominator' haveing a variable 'numerator' inclosed in the parenthesis and simpley adding a comma to include the variable 'denominator' in the argument to be sent in the invocation of the abs method. It seems like this would be the correct syntax:
    int common = (Math.abs(numerator))/(Math.abs(denominator));
    Can anyone explain the 'int common = gcd (Math.abs(numerator), denominator);'
    statement?
    Thanks,
    -dman

    > A).What is gcd?
    As already been said: it's the Greatest Common Divider.
    Example: fraction 9/24, then gcd(9, 24) == 3.
    >It seems like this would be the correct syntax:
    int common = (Math.abs(numerator))/(Math.abs(denominator));
    Probably the gcd(...) method is used to normalize a fraction whose denominator is always positive. If the fraction is smaller then zero, the numerator is negative. And to caculate the Greatest Common Divider of two numbers, the gcd(...) method needs two positive arguments.
    Google for "Euclid GCD algorithm".

  • Extend the String Class

    Is there a way to extend the string class? I know it's final, if i make my class final too would that work? I want to add some functionality to the string class, but I wanna be able to do concatination with the + operator and stuff like that. That's special to the String class.
    Any help would be great.
    Thanks.
    Joe

    Well, put your mind at easy with the fact that being
    able to use the '+' operator on Strings is a design
    flaw in Java. At least from a purist point of view...And from a pragmatist's point of view, it's a reasonable compromise, the benefit of which outweighs the downside. This is consistent with Java's goal as a good general-purpose language. It's not intended to be pure OO or pure anything else.

  • Warning :: Derived class hides the base class virtual function

    We are porting from CC5.3 to CC5.8 compiler with Sun Studio one compiler. After plenty of hurdles we are in the final stage of removing the warning messages... Amoung the plenty the following one is very common and in different files. Why am I getting this error in 5.8 and not in 5.3 compiler....
    Warning: derived_Object::markRead Hides the virtual function base_Object::markRead(ut_SourceCodeLocation&) const in a virtual base
    From this it is easily understandable that the base class mark read was hidden by derived class markRead... when we drive and override the derived class function.... It is all over the place....
    Thank you,
    Saravanan Kannan
    //public: using xx_Object :: markRead;
    virtual void markRead() const;

    The Sun C++ FAQ discusses the warning message:
    http://developers.sun.com/prodtech/cc/documentation/ss11/mr/READMEs/c++_faq.html#Coding1
    Notice that warnings are not necessarily errors. But I applaud your desire to fix the code so that it generates no warnings. I wish more of our customers could be persuaded to do the same. :-)
    C++ 5.3 issues this warning, by the way. Example:
    struct B { virtual int foo(int); };
    struct D : B { virtual int foo(double); }; // line 2
    D d;
    line 2: Warning: D::foo hides the virtual function B::foo(int).
    If for your particular code you do not see a warning with C++ 5.3, it would be due to a bug in C++ 5.3 that was later fixed.

  • How to use the different class for each screen as well as function.

    Hi Experts,
    How to use the different class for each screen as well as function.
    With BestRegards,
    M.Thippa Reddy.

    Hi ThippaReddy,
    see this sample code
    Public Class ClsMenInBlack
    #Region "Declarations"
        'Class objects
        'UI and Di objects
        Dim objForm As SAPbouiCOM.Form
        'Variables
        Dim strQuery As String
    #End Region
    #Region "Methods"
        Private Function GeRate() As Double
                Return Double
        End Function
    #End Region
    Public Sub SBO_Appln_MenuEvent(ByRef pVal As SAPbouiCOM.MenuEvent, ByRef BubbleEvent As Boolean)
            If pVal.BeforeAction = True Then
                If pVal.MenuUID = "ENV_Menu_MIB" Then
                End If
            Else ' Before Action False
                End If
        End Sub
    #End Region
    End Class
    End Class
    Rgds
    Micheal
    Vasu Anna Regional Feeling a???? Just Kidding
    Edited by: micheal willis on Jul 27, 2009 5:49 PM
    Edited by: micheal willis on Jul 27, 2009 5:50 PM

  • How to find out what are the functions supported by string class

    Hi,
    Can any one let me know how to find what are all the functions supported by the string class in standard(STL) library on solaris.
    Regards,
    Vignesh

    1. Any C++ textbook that covers the Standard Library will tell you about the standard string class. A good tutorial and reference for the entire Standard Library is "The C++ Standard Library" by Nicolai Josuttis, published by Addison Wesley.
    2. WIth Sun C++, the command
    man -s3C++ basic_string
    provides documentation for the default libCstd version of the Standard Library.
    3. You could look at the <string> header itself. I don't recommend that approach.

Maybe you are looking for