Results 1 to 8 of 8
Thread: While Loops, need a bit of help.
 10302009, 04:20 PM #1Member
 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 // loop. Your choice. return 0; } /** * Computes the nth 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 nth 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; }
 10302009, 04:27 PM #2Senior 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; }
 10302009, 04:29 PM #3Member
 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.
 10302009, 04:33 PM #4Member
 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; } 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 nth 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 nth 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; } }
 10302009, 06:41 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
The name is Heron, not "Herron".
kind regards,
Jos
 10302009, 07:31 PM #6Member
 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.
 10302009, 08:53 PM #7Member
 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.
 10302009, 09:24 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Similar Threads

Help with loops!
By jrelvi23 in forum New To JavaReplies: 9Last Post: 09042012, 01:36 PM 
Question about loops
By SwEeTAcTioN in forum New To JavaReplies: 1Last Post: 10232009, 07:15 AM 
these loops...
By Blaedel in forum New To JavaReplies: 0Last Post: 10012009, 06:59 PM 
how to use do while loops
By mikeitalydz in forum New To JavaReplies: 32Last Post: 09262009, 08:30 PM 
Loops (while do etc)
By manupr in forum New To JavaReplies: 1Last Post: 01152008, 04:59 AM
Bookmarks