Results 1 to 15 of 15
 10252008, 09:18 AM #1Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
Creating a New Method for Square Root Loop
Hi! I'm writing a program that uses a while new method to calculate the square root of a. We can't use the Math class at all, we have to make this method using Newton's method and the userinputed integer n:
Then I have to set the initial value of x^0 = 1, and perform 1000 iterations. (I'm not totally sure why or how to do that. I don't think x^n should be increasing like x^n++ or anything.) Then I have to call this method from the main method to compute the following expression, where there are n radicals:
The output is the result of this second formula. Now, these are the problems I'm having creating this:Last edited by SapphireSpark; 10252008 at 09:26 AM.
 10252008, 09:22 AM #2Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
Here's what I have so far, I'm sure there's mistakes in it already:
Java Code:import java.util.Scanner; public class SquareRoot { public static void main(String[] args) { Scanner scan = new Scanner (System.in); System.out.println("Enter n: "); int n = scan.nextInt(); //double a = (eventually the main method will plug values into a) double x = 1; for (int i = 0; i<1000; i++) { x = 0.5*(x+a/x); } System.out.println("sqrt of " + a + "is " + x); } }
How do I make the method apply within itself n times? I'm thinking it's a for loop wth a counter increases until it reaches n, but I don't know how to put the method in it, or what else to do it. Also, should the Newton's method part be in its own class?Last edited by SapphireSpark; 10252008 at 09:31 AM.
 10252008, 09:44 AM #3Member
 Join Date
 Oct 2008
 Location
 UK
 Posts
 65
 Rep Power
 0
This will work fine if you initialise a, e.g. like this:
Java Code:double a = (double) n;
I think this is fine inside one class, you don't need any others.
To apply it n times, declare and initialise n, and use this:
Java Code:for (int i = 0; i<n; i++)
 10252008, 09:47 AM #4
I think "a" should be inserted as well as you've inserted value for "n" variable.
And for the other question, I think you can use recursion which will help you to solve the problem with iteration. If you don't know what is recursion or how to "use" it, search it in google :)TEAM = Together Everyone Achieves More :)
 10252008, 09:47 AM #5Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
ok. But I thought a didn't equal n, it was the subject of the radical, not the number of radicals, n.
Thanks, Andy, but I think the teacher'd get suspicious if I used something like recursion, which we haven't covered in class yet.
 10272008, 06:31 PM #6
Not necessarily. If you have been taught how to call methods within a class you can accidentally stumble upon it. Recursion is just where a method calls itself which would work perfect for your example. Lets say you have a method called calculateRoot(some number). You do the equation to calculate the root and then you need the root of that answer so you send it back to the calculateRoot method.
Java Code:public void calculateRoot(int number){ while (number >= 1){ //do your equation to calculate the root System.out.println("The root of "+number+" is "+answerfromcalculation; calculateRoot(answerfromcalculation); } }
I strayed off my original intention for this post. I was going to say my friend, whom has no previous programming experience, ended up using recursion on accident for a similar problem. The teacher was a little surprised and explained to us what recursion was after his little mistake.
 10292008, 06:17 AM #7Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
Hmm, that sounds pretty useful. I'm still just practicing calling methods.
So, " calculateRoot(answerfromcalculation);" is what makes it send the answer back to the method?
 10292008, 08:07 AM #8Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 8
For your second problem, you don't need recursion. A loop will suit just fine and is much more efficient, as you have a set number of iterations in 'n'. It's one of those problems where if you try to solve it by hand that you can devise a loop to solve it, and isn't much different than the first algorithm you created. Just think about what you're taking the square root of.
 10292008, 04:04 PM #9
yup that's exactly what sends it back. Just make sure you have a way to check (at the beginning of calculateRoot() ) that the number being brought in is greater then 1 or else it will become an endless root and keep square rooting 1, return 1, root 1, return 1, etc
 10302008, 04:27 AM #10Member
 Join Date
 Oct 2008
 Posts
 5
 Rep Power
 0
SapphireSpark are you in Hong Zhangs class? I am in there too if you are. lol PM me or something.
 11052008, 09:12 AM #11Member
 Join Date
 Oct 2008
 Posts
 38
 Rep Power
 0
Yes, I am! Dude, I'm the indian girl in the back. Who are you?
 02252009, 08:54 AM #12Member
 Join Date
 Dec 2008
 Posts
 2
 Rep Power
 0
how to make asterisks triangle using java code
how to make an asterisk triangle using java could i hope you can help me..?
 02252009, 09:46 AM #13Senior Member
 Join Date
 Dec 2008
 Location
 Hong Kong
 Posts
 473
 Rep Power
 7
first equation given by SapphireSpark is the general equation
of finding square root
as you said, x(0) = 1,
a in the equation should be the input integer
x(n) will be the square root of a
 02252009, 10:05 AM #14
nimboots!! don't hijack someone's thread.
thread is 4 months' old, is that a zoombie?USE CODE TAGS> [CODE]...[/CODE]
Get NotePad++ (free)
 02252009, 02:21 PM #15
Similar Threads

change the square to triangle java
By anotsu in forum New To JavaReplies: 3Last Post: 07092009, 11:17 AM 
Creating a new equals() method help
By Dave0703 in forum New To JavaReplies: 2Last Post: 09212008, 05:32 PM 
I want to recreate the method drawRect with a nested forloop
By chelseacortez in forum Java 2DReplies: 4Last Post: 09052008, 04:47 PM 
SWT OpenGL snippet: draw a square
By Java Tip in forum SWT TipsReplies: 0Last Post: 06282008, 09:29 PM 
Problem using buttons to creat a magic square game
By goldman in forum New To JavaReplies: 5Last Post: 05052008, 04:04 AM
Bookmarks