Results 1 to 15 of 15
  1. #1
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Question 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 user-inputed 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; 10-25-2008 at 09:26 AM.

  2. #2
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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);
        }
    
    }
    -First of all, I'm not sure how this can perform 1000 iterations if it doesn't know what a is yet.

    -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; 10-25-2008 at 09:31 AM.

  3. #3
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by SapphireSpark View Post
    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);
        }
    
    }
    -First of all, I'm not sure how this can perform 1000 iterations if it doesn't know what a is yet.

    -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?
    This will work fine if you initialise a, e.g. like this:

    Java Code:
    double a = (double) n;
    If you don't declare and initialise a, the code won't compile, so it won't perform any iterations!

    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++)

  4. #4
    andy_man's Avatar
    andy_man is offline Member
    Join Date
    Oct 2008
    Location
    Yerevan
    Posts
    14
    Rep Power
    0

    Default

    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 :)

  5. #5
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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.

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Quote Originally Posted by SapphireSpark View Post
    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.
    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);
                    }
    }
    Thats pretty much it. It's fairly common for students to come across recursion on their own. Specially when using a specific operation over and over.

    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.

  7. #7
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    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?

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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.

  9. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Quote Originally Posted by SapphireSpark View Post
    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?
    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

  10. #10
    edgewater21 is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    SapphireSpark are you in Hong Zhangs class? I am in there too if you are. lol PM me or something.

  11. #11
    SapphireSpark is offline Member
    Join Date
    Oct 2008
    Posts
    38
    Rep Power
    0

    Default

    Yes, I am! Dude, I'm the indian girl in the back. Who are you?

  12. #12
    nimboots is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default how to make asterisks triangle using java code

    how to make an asterisk triangle using java could i hope you can help me..?

  13. #13
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    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

  14. #14
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    nimboots!! don't hijack someone's thread.

    thread is 4 months' old, is that a zoombie?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  15. #15
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Zombie alert...

    Yeah... Zombie alert...
    nimboots... please open a new post with your question.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. change the square to triangle java
    By anotsu in forum New To Java
    Replies: 3
    Last Post: 07-09-2009, 11:17 AM
  2. Creating a new equals() method help
    By Dave0703 in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 05:32 PM
  3. Replies: 4
    Last Post: 09-05-2008, 04:47 PM
  4. SWT OpenGL snippet: draw a square
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 09:29 PM
  5. Problem using buttons to creat a magic square game
    By goldman in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 04:04 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
  •