Results 1 to 3 of 3
- 05-07-2011, 12:42 AM #1
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
Bisection Method to find Square and Cube Roots
Hi,
I am new to Java and trying to write a program that uses the bisection method to calculate square roots. I need to calculate the roots good to 15 decimal places. I am only allowing the user to
When I run the program, it never actually enters the sqrtBisect method and instead repeatedly prompts the user for numbers. Thank you so much for you help, here is what I have so far:
import java.util.*;
public class Bisection {
public static double sqrtBisect(double target){
double high = 1000000; //Value above n
double low = 1; //Value below n
double mid = (high + low)/2;
boolean found = false;
while(!found && low<=high) {
if(target < (mid*mid))
high= mid-1;
else if(target > (mid*mid))
low = mid + 1;
else
found = true;
}
return mid;
}
public static void main(String [] args){
System.out.println("Please enter a number between 1 and 1000000.");
Scanner console;
console = new Scanner(System.in);
double n = console.nextInt(); //input number
while(n<1 || n> 1000000){
System.out.println("Number invalid, Please try again.");
n = console.nextInt();
}
double root = sqrtBisect(n);
System.out.println("The square root of " + n + " equals " +root+ ".");
}
}
- 05-07-2011, 01:01 AM #2
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
Fixed it.
Sorry,
tec
- 05-07-2011, 01:03 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
HI, welcome to the forums.
When you post code, use the "code" tags. You put [code] at the start of the code and [/code] at the end, that way the code will preserve indentation and be readable.
When I run the program, it never actually enters the sqrtBisect method and instead repeatedly prompts the user for numbers.
I'm not so sure about this. What actually happens when you run the program: ie what do you see? (not what do you think causes what you see)
[Edit] slow ;( But welcome anyway! ANd my comment stands, when debugging: objectivity beats speculation every time...
Similar Threads
-
Find the square root with a particular method
By roud9 in forum New To JavaReplies: 2Last Post: 09-27-2010, 11:39 PM -
Cube
By Evil Smurf in forum Advanced JavaReplies: 0Last Post: 08-30-2009, 06:22 PM -
Display Square Roots
By hypes057 in forum New To JavaReplies: 8Last Post: 08-25-2009, 10:34 AM -
Creating a New Method for Square Root Loop
By SapphireSpark in forum New To JavaReplies: 14Last Post: 02-25-2009, 01:21 PM -
26 roots
By Nicholas Jordan in forum Advanced JavaReplies: 6Last Post: 08-25-2008, 11:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks