Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: main calls Math.cos and myCos twice.....

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default main calls Math.cos and myCos twice.....

    Task: a program that calls Math.cos and myCos twice, once passing Math.PI another passing Math.PI/6.0

    myCos should compute and return the value of

    1.0 - x^2/(2!) + x^4/(4!) - x^6/(6!) + ... + x^96/(96!) - x^98/(98!)



    import java.util.Scanner;
    class Cos
    {
        double x;
        double sum = 0.0;
        double count= 2.0;
        double term= 1.0;
        double sign = 1.0;
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter number of term to sum:");
     
       while (count <= 98)
        {
            sum = sum + sign* 1.0/term;
            sign = -sign;
            sum= count*(count-1)*sum;
            term = term*x*x;
            count ++;
            double y= Math.pow(X4);
        }
     
        System.out.println("Math.cos =" +Math.cos);
        System.out.println("myCos=" +sum)
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: main calls Math.cos and myCos twice.....

    You don't have your code inside of a method.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: main calls Math.cos and myCos twice.....

    Oh, yes thank you! I believe my formula is not correct and that's my main problem. I don't understand how to formulate all the things that is asked for in this code.

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: main calls Math.cos and myCos twice.....

    I need help!!!!! I spent the whole day working on this, that's how bad I am.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: main calls Math.cos and myCos twice.....

    Hard to help you with code when we can't see the code.
    If you post any code please explain what your problems are.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: main calls Math.cos and myCos twice.....

    public class Cos
    {
        public static void main(String[] args)
        {
            // your code goes here
        }
    }

    If you are unsure of the Java syntax, I would recommend reading The Java Tutorials, starting with Getting Started and the Learning the Java Language sections.

  7. #7
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: main calls Math.cos and myCos twice.....

    Thank you for your help. Now I have this and I still have a problem with the factorial.....
    import java.math.BigInteger;
    import java.util.Scanner;
    class Cos
    {
        public static void main(String[] args)
        {
           double sum = 0.0;
           int f = 2, count = 1, sign = 1, n=0, numberTerms;
           BigInteger factorial = BigInteger.ONE;
           for (double i=1.0; i<=98; i++)
           {
               factorial=factorial.multiply(BigInteger.valueOf(long));
           }
     
           Scanner kb = new Scanner(System.in);
     
           System.out.println("Enter number of terms to sum:");
           numberTerms.kb.nextInt();
     
           while (count <= numberTerms)
           {
               sum = sum + sign * Math.pow(kb,n)/factorial;
               sign = -sign;
               factorial = factorial+2;
               n = n+2;
               count++;
           }
     
           System.out.println("Mycos = " +sum);
           System.out.println("Math.cos =" +Math.cos);
        }  
    }


    when trying to compile, it says class is expected in
    factorial=factorial.multiply(BigInteger.valueOf(long));

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: main calls Math.cos and myCos twice.....

    it says class is expected in
    Please post the full text of the error message.

    "long" is a keyword not a variable. Create and Use a variable for the valueOf argument.

  9. #9
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: main calls Math.cos and myCos twice.....

    O.K. So, now it compiles.... but it's not right. I believe it's my equation. =_= I don't know what to do.... I can't believe I have been doing this problem for a week now.... I need a book... Please help me >.<


    import java.util.Scanner;
    class Cos
    {
        public static void main(String[] args)
        {
           double sum = 0.0;
           double count = 1, sign = 1, d=0, numberTerms=98;
           double n = 3;
           double f = n;
           for(double p = (n - 1); p > 1; p--)
           {
               f = f * p;
           }
     
           Scanner input = new Scanner(System.in);
           System.out.println("Enter number of terms to sum:");
           double kb = input.nextDouble();
     
           while (count <= numberTerms)
           {
               double t= Math.pow(kb,d);
               sum = (sum + sign * t)/f;
               sign = -sign;
               d = d+2;
               count++;
           }
     
           System.out.println("Mycos = " +sum);
           System.out.println("Math.cos =" +Math.PI/6.0);
           System.out.println("Math.cos =" +Math.PI);
        }  
    }

  10. #10
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: main calls Math.cos and myCos twice.....

    Correction!

     System.out.println("Mycos = " +sum);
           System.out.println("Math.cos =" +Math.cos(Math.PI/6.0));
           System.out.println("Math.cos =" +Math.cos(Math.PI));

  11. #11
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: main calls Math.cos and myCos twice.....

    Is everything solved?

  12. #12
    Junior Member
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: main calls Math.cos and myCos twice.....

    Nope, I believe my factorial is wrong but I don't know how to correct it....

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: main calls Math.cos and myCos twice.....

    my factorial is wrong
    Can you do the steps manually and get the correct answer and then compare that with what the program does when you step thru it statement by statement.
    Can you write a small test program that does just the factorial function and get that to work?

Similar Threads

  1. Need help understanding method calls and such
    By hackman2007 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 14th, 2010, 08:18 AM
  2. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM
  3. Dare 2010 calls for emerging game developers
    By jeet893 in forum Java Networking
    Replies: 0
    Last Post: March 5th, 2010, 03:51 PM
  4. Question on my math
    By SwEeTAcTioN in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 25th, 2009, 05:42 PM
  5. Any way to map method calls?
    By Swiftslide in forum Collections and Generics
    Replies: 1
    Last Post: September 21st, 2009, 04:37 AM