Results 1 to 4 of 4
 11032010, 09:10 PM #1Member
 Join Date
 Nov 2010
 Posts
 2
 Rep Power
 0
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
 11032010, 10:27 PM #2Member
 Join Date
 Nov 2010
 Posts
 2
 Rep Power
 0
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();
}
}
 11032010, 11:12 PM #3Member
 Join Date
 Sep 2010
 Location
 Oregon, usa
 Posts
 69
 Rep Power
 0
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
Java Code:int variable = Math.sqrt(n);
Java Code:!! NOT THIS !! Math someVariable = new Math(); <Wrong! someVariable.sqrt(n); <Wrong!
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);
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.:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
 11042010, 01:09 AM #4Member
 Join Date
 Oct 2010
 Posts
 94
 Rep Power
 0
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 } }
Java Code:x = 0.5 * (x + n / x);
Java Code:y = 0.5 * (x + n / x); x = y;
ErikI'm new to Java but I like to help where ever I can. :)
Similar Threads

Find the square root with a particular method
By roud9 in forum New To JavaReplies: 2Last Post: 09282010, 12:39 AM 
Error deploying web application directory ROOT
By busymanis in forum GWTReplies: 0Last Post: 09232010, 08:34 AM 
square root and prime numbers
By roud9 in forum New To JavaReplies: 16Last Post: 09222010, 04:20 PM 
Simple square root problem!
By nortski in forum New To JavaReplies: 7Last Post: 04012009, 03:11 PM 
Creating a New Method for Square Root Loop
By SapphireSpark in forum New To JavaReplies: 14Last Post: 02252009, 02:21 PM
Bookmarks