Results 1 to 12 of 12
  1. #1
    vudoo is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Create Math.sin without math.sin

    I have a homework assignment where it asks me to do this: Java has a "sine"-computing function: Math.sin(#)

    I am new to java so please be patient with me and teach me with babysteps. :(

    This is what I have:
    Java Code:
    mport sdsu.io.*;
    public class hw7
    {
    
    public static void main(String args[])
    {
     double x;
     System.out.println("Please enter a value for x.");
     x = Console.readInt();
    
     double maxe;
     System.out.println("Please enter an odd exponent");
      double[]a =  new double[10];
     maxe = Console.readInt();
      for (int i = 0; i <= maxe; i++)
            {
            a[i] = i;
            if ( i %2 !=0);
    
     System.out.println(+a[i]);
            }
    }
    
    //public static double f(double x) //I'm not sure how to do a summation to represent the sine function. This is as close as I got.
    //        {
    //        return  x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6] -
    //        (Math.pow(x,a[8]/a[8]);
    //        }
    //}
    }
    I keep getting these errors:
    Java Code:
    hw7.java:26: ')' expected
            (Math.pow(x,a[8]/a[8]);
                                  ^
    hw7.java:26: ')' expected
            (Math.pow(x,a[8]/a[8]);
                                   ^
    hw7.java:27: ';' expected
            }
             ^
    hw7.java:29: reached end of file while parsing
    }
     ^
    5 errors

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    The error pointed that you've missing one closing bracket here.

    Java Code:
    return  x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6] - (Math.pow(x,a[8]/a[8]);
    It should be something like this.

    Java Code:
    return  x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) - (Math.pow(x,a[8]/a[8]));
    And more clearly,

    Java Code:
    return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) - (Math.pow(x,a[8]/a[8])));

  3. #3
    vudoo is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    It seems like I brought out a hellstorm.
    Java Code:
    symbol  : variable a
    location: class hw7
            return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) -
                                    ^
    hw7.java:25: cannot find symbol
    symbol  : variable a
    location: class hw7
            return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) -
                                          ^
    hw7.java:25: cannot find symbol
    symbol  : variable a
    location: class hw7
            return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) -
                                                              ^
    hw7.java:25: cannot find symbol
    symbol  : variable a
    location: class hw7
            return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) -
                                                                   ^
    hw7.java:26: cannot find symbol
    symbol  : variable a
    location: class hw7
            (Math.pow(x,a[8]/a[8])));
                        ^
    hw7.java:26: cannot find symbol
    symbol  : variable a
    location: class hw7
            (Math.pow(x,a[8]/a[8])));
                             ^
    6 errors
    Did I not code my array correctly?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    vudoo is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Java Code:
    import sdsu.io.*;
    public class hw7
    {
    
    public static void main(String args[])
    {
     double x;
     System.out.println("Please enter a value for x.");
     x = Console.readInt();
    
     double maxe;
     System.out.println("Please enter an odd exponent");
      double[]a =  new double[10];
     maxe = Console.readInt();
      for (int i = 0; i <= maxe; i++)
            {
            a[i] = i;
            if ( i %2 !=0);
    
     System.out.println(+a[i]);
            }
    }
    public static double f(double x)
            {
            return  (x -(Math.pow(x,a[4])/a[4]) + (Math.pow(x,a[6]/a[6])) -
            (Math.pow(x,a[8]/a[8])));
                    }
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have define the array a in main method. So it's not possible to access it from another method. It's an invalid scope to access. Do you know about variable scope?

    If you want to use the same array then you've define them globally. Something like this.

    Java Code:
    public class hw7{
    
        private static double[] a = new double[10];
    
        public static void main(String args[]) {
            double x;
            System.out.println("Please enter a value for x.");
            x = Console.readInt();
    
            double maxe;
            System.out.println("Please enter an odd exponent");
    
            maxe = Console.readInt();
            for (int i = 0; i <= maxe; i++) {
                a[i] = i;
                if (i % 2 != 0);
    
                System.out.println(+a[i]);
            }
        }
    
        public static double f(double x) {
            return (x - (Math.pow(x, a[4]) / a[4]) + (Math.pow(x, a[6] / a[6]))
                    - (Math.pow(x, a[8] / a[8])));
        }
    }

  7. #7
    vudoo is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    No, I do not what a variable scope is, but from what you did, I think I understand what you mean by defining it globally.

    Could you please show me how I would be able to do this equation in Java?
    Java Code:
                3         5         7                           maxe
               x         x         x                           x
       x   -   ---   +   ---   -   ---   +   .......   (+/-)   ---
               3!        5!        7!                          maxe!
    (it is in my pastebin link for more clarification, my current equation does not work.)

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all separate those into to two, numerator and denominator. Just forget about the sign and all. So think how to calculate the term (Y^7 / X!). Did you get my point?

    By the way did you know what this equation is, any special kind of name?

  9. #9
    vudoo is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    I am not sure what you mean by separate them in a numerator and denominator.
    Java Code:
     
    (Math.pow(x, a[4]) / a[4])
            ^               ^
    Numerator         Denominators
    Is that not correct? Also is it called a summation right?

  10. #10
    vudoo is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Would this be correct to represent the equation above?
    Java Code:
    (x - (Math.pow(x, a[4]) / a[4]*a[3]*a[2]*a[1]) + (Math.pow(x, a[6] /
                    a[6]*a[5]*a[4]*a[3]*a[2]*a[1]))
                    - (Math.pow(x, a[8] / a[8]*a[7]*a[6]*a[5]*a[4]*a[3]*a[2]*a[1])));

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by vudoo View Post
    I am not sure what you mean by separate them in a numerator and denominator.
    Java Code:
     
    (Math.pow(x, a[4]) / a[4])
            ^               ^
    Numerator         Denominators
    Is that not correct? Also is it called a summation right?

    What I mean, numerator is simply power of a digit. That's could be narrow down into multiplication.

    eg: x ^ 5 = x * x * x * x * x;

    Denominator is factorial of a number. That is x! = x * (x -1) * ..... * 1;

    Actually what you are going to deals with is a standard number manipulation. The following article explain it, if you could try to understand it. So it may helpful to you, to solve the problem.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by vudoo View Post
    Would this be correct to represent the equation above?
    Java Code:
    (x - (Math.pow(x, a[4]) / a[4]*a[3]*a[2]*a[1]) + (Math.pow(x, a[6] /
                    a[6]*a[5]*a[4]*a[3]*a[2]*a[1]))
                    - (Math.pow(x, a[8] / a[8]*a[7]*a[6]*a[5]*a[4]*a[3]*a[2]*a[1])));
    Looks to me this is too complex. And I'm sure you are in trouble when you look at this again at a later time.

    Don't implement such complex calculations in a single statement. And also don't write complex statements in return. Step-down it. Look at my previous post.

Similar Threads

  1. Math in java
    By blug in forum New To Java
    Replies: 2
    Last Post: 11-22-2010, 02:21 PM
  2. Help with java math
    By gabrielfirestorm in forum New To Java
    Replies: 6
    Last Post: 10-25-2010, 01:54 PM
  3. Math Class
    By ritwik07 in forum New To Java
    Replies: 2
    Last Post: 09-14-2009, 04:06 PM
  4. Help with math in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:05 AM
  5. Date math
    By orchid in forum New To Java
    Replies: 2
    Last Post: 04-18-2007, 07:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •