Results 1 to 8 of 8
  1. #1
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default While Loops, need a bit of help.

    The following is what I am working on and the instructions are in the comments. I am having a hard time implementing the methods with loops because I have really no knowledge about loops. I have been reading up about them though. I have more knowledge about the syntax of a while than a for and would like to use a while over a for if possible. Any help for Step 1 and 2 would be great to point me in the right direction.

    Java Code:
     /**
         * Adds up the sum of the squares of the first n integers.
         * @param n The last integer.
         * @return 1 * 1 + 2 * 2 + 3 * 3 + . . . + n * n
         */
        public static int sumOfSquareOfIntegers(int n)
        {
                // STEP 1: Implement this method.  See the comment for details of what it should do.  You can use either a for loop or a while
                //                loop.  Your choice.
                
                
                
                
                
                
                
                
                
                return 0;
        }
        
        
        /**
         * Computes the n-th fibonacci number.  The fibonacci sequence is as follows: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, . . .  
         * The first two fibonacci numbers are both 1.  Each successive fibonacci number is the sum of the previous two.
         * 
         * @param n Position in the fibonacci sequence.
         * @return The n-th fibonacci number.  If n is 0, this method returns 0.  If n is either 1 or 2, this method returns 1.  
         *                 If n is greater than 2, this method computes the number in position n of the fibonacci sequence.
         */
        public static int fibonacci(int n)
        {
                // STEP 2: Implement this method.  See the comment for details of what it should do.  You can use either a for loop or a while
                //                loop.  Your choice.
                
                return 0;
        }

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    so it should take the square of all number under and incl the number given?
    and retiurn the sum?
    PHP Code:
    public int functionName(n){
    int sum = 0;
    for (int x = 0;x<n+1;x++){
    sum = sum+x^2;
    }
    return sum;
    }

  3. #3
    Menre is offline Member
    Join Date
    Feb 2008
    Posts
    79
    Rep Power
    0

    Default

    Hi,

    I understood your problem and question, but you posted only the comments for you code and not actually the program.

  4. #4
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    The entire code.


    Java Code:
    public class MathOperations
    {
        /**
         * Adds up the first n integers.
         * @param n The last integer in our sum
         * @return The sum of the integers from 1 to n inclusive.
         */
        public static int sumOfIntegers(int n)
        {
            int total = 0;
            /* // The original while loop example of adding the first n integers so you can compare the logic with that of the for loop
            int i = 1;
            while (i <= n)
            {
                total += i;
                i++;
            }
            */
           // general syntax of the for loop
           // for ( init; condition; update)
           //     body;
           
           for (int i = 1; i <= n; i++) 
           {
               total += i;
           }
            return total;
        }
        
        /**
         * Computes factorials
         * @param n The value of n
         * @return n!  If n is negative, returns -1.
         */
        public static int factorial(int n)
        {
            if ( n < 0) return -1;
            
            int fact = 1;
            for (int i = 2; i <= n; i++)
            {
                fact = i * fact;   
            }
            
            return fact;
        }
    
        /**
         * Adds up the sum of the squares of the first n integers.
         * @param n The last integer.
         * @return 1 * 1 + 2 * 2 + 3 * 3 + . . . + n * n
         */
        public static int sumOfSquareOfIntegers(int n)
        {
                // STEP 1: Implement this method.  See the comment for details of what it should do.  You can use either a for loop or a while
                //                loop.  Your choice.
                
                
                
                
                
                
                
                
                
                return 0;
        }
        
        
        /**
         * Computes the n-th fibonacci number.  The fibonacci sequence is as follows: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, . . .  
         * The first two fibonacci numbers are both 1.  Each successive fibonacci number is the sum of the previous two.
         * 
         * @param n Position in the fibonacci sequence.
         * @return The n-th fibonacci number.  If n is 0, this method returns 0.  If n is either 1 or 2, this method returns 1.  
         *                 If n is greater than 2, this method computes the number in position n of the fibonacci sequence.
         */
        public static int fibonacci(int n)
        {
                // STEP 2: Implement this method.  See the comment for details of what it should do.  You can use either a for loop or a while
                //                loop.  Your choice.
                
                return 0;
        }
        
        
        
        
        /**
         * Approximates the square root of a number using Herron's method.
         * 
         * @param a 
         * @return The square root of a.
         */
        public static double herronsSquareRoot( double a  )
        {
            /* EXTRA CREDIT STEP:  
             * If you don't do the extra credit step, please leave this incomplete version of the method intact to avoid
             * breaking my test program.
             * 
             * Implement a method that uses Herron's method (explained
             * below) to approximate the square root of a number.  Do the following:
             * 
             * 1) Define a constant EPSILON which
             *    has some small floating point value.
             *    
             * 2) Implement Herron's method.  Here is how Herron's method works.
             *    
             *    You begin by "guessing" the answer.  Any guess will do.  
             *    Note, DO NOT call the sqrt method of class Math (forget that it exists for the sake of this
             *    extra credit).  We want our method to compute the square root.
             *    
             *    Store your guess in a local variable of type double named x.
             *    
             *    According to Herron's method, if x is a "guess" for the square root of
             *    a, then the average of x and a/x is an even better guess.
             *    
             *    Herron's method then repeatedly "guesses" using the above procedure
             *    until two successive guesses are sufficiently close to each other
             *    (i.e., the absolute value of their difference is smaller than some
             *    small floating point value, in other words your constant EPSILON indicated earlier).
             *    
             *    Write a while loop inside the herronsSquareRoot method
             *    to compute the square root of a using Herron's method.
             *    
             *    Don't forget to change the return statement to return the result of your Herron's method.
             */
            
                return 0;
        }
        
        
        
    
    
    }

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    The name is Heron, not "Herron".

    kind regards,

    Jos

  6. #6
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Thanks for that correction Ill change it, still need a bit of help with step 1 and 2 though.

  7. #7
    cemm is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default

    I suspect that this is for a class and you have a java text book. I don't think you'll find too many people that will write code for you for class. Especially when it is this basic. If you can't understand from the comments in the code and read a couple of pages in the text book, you should drop the class.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Keno777 View Post
    Thanks for that correction Ill change it, still need a bit of help with step 1 and 2 though.
    For step 1 you don't need loops; by mathematical induction you can show that the sum of the first n squares is n*(n+1)*(2*n+1)/6.

    kind regards,

    Jos

Similar Threads

  1. Help with loops!
    By jrelvi23 in forum New To Java
    Replies: 9
    Last Post: 09-04-2012, 02:36 PM
  2. Question about loops
    By SwEeTAcTioN in forum New To Java
    Replies: 1
    Last Post: 10-23-2009, 08:15 AM
  3. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 07:59 PM
  4. how to use do while loops
    By mikeitalydz in forum New To Java
    Replies: 32
    Last Post: 09-26-2009, 09:30 PM
  5. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 04:59 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
  •