Results 1 to 4 of 4
  1. #1
    racingoutbac is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Help with Square root function and Overload(calc error)

    Here is what i have to do.

    1. Create a new class called SlowMath that contains a static function to find the square root of a given integer n using the iterative algorithm

    x i +1 = 0.5(x i + n/x i) x0 = n/2.

    The user will also supply a parameter indicating the number of iterations. Hint: Use a for loop to perform the iteration.

    Note: For part 1, create a static method called sqrtIterate that takes two arguments, the number to be operated on (a double), and another number (an int) that specifies the number of iterations

    2. Overload your square root function by providing another call that does the same thing but continues to iterate until difference between the actual value and the calculated value is less than a user provided error (use Math.sqrt to calculate the actual value). Return the number of iterations required. Hint: Use a while loop with the key test as the while conditional.

    Note: For part 2, create a another method with the same name but with 2 double arguments (the first the number, the second the error). This method keeps iterating until the actual error (computed as the difference between the current value and the actual square root given by Math.sqrt) is less than the error parameter supplied to the method. Hope this helps.



    Here is what i did for one:

    import java.util.*;

    public class SlowMath {

    public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    System.out.println("Enter number for square root: ");
    double n = in.nextDouble();

    System.out.println("Number of iterations: ");
    int numIt = in.nextInt();
    double x = n / 2;

    double y = 0;

    for (int i = 0; i < numIt; i++) {
    y = 0.5 * (x + n / x);
    x = y;


    }
    System.out.println("Approximation of Square root: " + x);

    }
    }



    Here is what i did for 2...so far. Need help completing it.

    import java.util.*;

    public class Overload {

    public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    System.out.println("Enter number for square root: ");
    double n = in.nextDouble();

    System.out.println("Number of iterations: ");
    int numIt = in.nextInt();
    double x = n / 2;

    double y = 0;

    for (int i = 0; i < numIt; i++) {
    y = 0.5 * (x + n / x);
    x = y;

    }

    double error = 0;
    double threshold = 0;

    while (error > threshold) {

    error = x - Math.sqrt(n);

    if(x < Math.sqrt(n)){
    error = Math.abs(x - Math.sqrt(n));
    }
    else if(error > Math.sqrt(n)){
    error = Math.abs(x - Math.sqrt(n));
    }
    else{
    error = 0;
    }

    }
    System.out.println("Approximation of Square root: " + x);
    System.out.println("Error equals: " + error);

    }
    }

    Can anyone help me, i think i'm almost done, but i just can't figure out how to show, or calculate the error properly

  2. #2
    racingoutbac is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default changed code

    i just changed up the code to see if this is more useful, both are used together.

    1:

    import java.util.*;

    public class SlowMath {


    double n;
    int numIt;
    double x;


    public void getSlowMath(double n, int numIt) {

    double x = n / 2;

    double y = 0;

    for (int i = 0; i < numIt; i++) {
    y = 0.5 * (x + n / x);
    x = y;

    }
    System.out.println("Approximation of Square root: " + x);

    }
    public static void main(String[] args){
    Scanner in = new Scanner(System.in);

    SlowMath sqrt = new SlowMath();

    System.out.println("Enter number for square root: ");
    double n = in.nextDouble();
    System.out.println("Number of iterations: ");
    int numIt = in.nextInt();
    sqrt.getSlowMath(n, numIt);

    }
    }




    2:

    import java.util.*;

    public class Overload extends SlowMath {

    double error;

    public void getOverload() {

    double threshold = 1;

    while (error > threshold) {


    error = x - Math.sqrt(n);

    if (x < Math.sqrt(n)) {
    error = Math.abs(x - Math.sqrt(n));
    } else if (error > Math.sqrt(n)) {
    error = Math.abs(x - Math.sqrt(n));
    } else {
    error = 0;
    }

    }

    System.out.println("Error equals: " + error);


    }

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    Overload ol = new Overload();

    System.out.println("Enter number for square root: ");
    double n = in.nextDouble();

    System.out.println("Number of iterations: ");
    int numIt = in.nextInt();

    ol.getSlowMath(n, numIt);
    ol.getOverload();

    }
    }

  3. #3
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Note: For part 1, create a static method called sqrtIterate that takes two arguments, the number to be operated on (a double), and another number (an int) that specifies the number of iterations
    For starters, a static method is a method that doesn't need to be instantiated by creating an object of its type. A good example is the math class. We call the methods in the math class like this:
    Java Code:
    int variable = Math.sqrt(n);
    We don't call the methods in the math class by creating and instantiating an object of type math
    Java Code:
    !! NOT THIS !! 
    Math someVariable = new Math();  <----Wrong!
    someVariable.sqrt(n);  <----Wrong!
    So here's a quick example of a class with a static method that returns an integer:
    Java Code:
    public class MyClass() {
    
         public static int myMethod(String string, int x) {
                int result;
                //some code that does stuff       
                return result;
         }
    
    }
    
    
    //Example of calling the method myMethod in the class MyClass
    
    int variable = MyClass.myMethod("Some text", 2);
    I know it doesn't answer your question (yet), but it is something that is required in the assignment.
    This might help explain a bit more: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    I'll check back with you in a bit to see how you're doing on this one.

  4. #4
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Have a look at the problem description again. It says to create an overloaded function for the second algorithm. So you should have only one class with two overloaded functions in stead of two classes:

    Java Code:
    public class SlowMath {
      public static void main(String[] arguments) {
        // calling code goes here (e.g. reading input from Scanner and printing).
      }
    
      public static void sqrtIterate(double value, int iterations) {
        // first algorithm goes here
      }
    
      public static void sqrtIterate(double value, double error) {
        // second algorithm goes here
      }
    }
    Final tip:

    Java Code:
      x = 0.5 * (x + n / x);
    is the same as and better than

    Java Code:
      y = 0.5 * (x + n / x);
      x = y;
    Cheers,
    Erik
    I'm new to Java but I like to help where ever I can. :)

Similar Threads

  1. Find the square root with a particular method
    By roud9 in forum New To Java
    Replies: 2
    Last Post: 09-28-2010, 12:39 AM
  2. Replies: 0
    Last Post: 09-23-2010, 08:34 AM
  3. square root and prime numbers
    By roud9 in forum New To Java
    Replies: 16
    Last Post: 09-22-2010, 04:20 PM
  4. Simple square root problem!
    By nortski in forum New To Java
    Replies: 7
    Last Post: 04-01-2009, 03:11 PM
  5. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 02:21 PM

Posting Permissions

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