# Thread: While Loops, need a bit of help.

1. Member Join Date
Oct 2009
Posts
27
Rep Power
0

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

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

return 0;
}```  Reply With Quote

2. Senior Member Join Date
Aug 2009
Posts
294
Rep Power
0

## 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;
}```  Reply With Quote

3. Member Join Date
Feb 2008
Posts
79
Rep Power
0

## Hi,

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

4. Member Join Date
Oct 2009
Posts
27
Rep Power
0

## 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;
}
}

/**
* 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

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

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;
}

}```  Reply With Quote

5. ## The name is Heron, not "Herron".

kind regards,

Jos  Reply With Quote

6. Member Join Date
Oct 2009
Posts
27
Rep Power
0

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

7. Member Join Date
Oct 2009
Posts
1
Rep Power
0

## 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.  Reply With Quote

8. ##  Originally Posted by Keno777 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  Reply With Quote

#### Posting Permissions

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