Program to write fractional approximations of square roots
I can't figure out how to write a program in order to find fractional approximations of square roots. How can i do this? this is what i need to do and I cant seem to get started. This is an example of the output taht needs to come from my program:
This program gives fractional approxomations
to square roots of integers.
Which integer will you test?
5
To what accuracy? (input the number of digits of correctness
in the fractional component)
2
The number 5 is between the perfect squares
of 4 and 9.
Beginning iteration...
Trying 2/1 and 3/1
Trying 2/1 and 5/2
Trying 2/1 and 7/3
Trying 2/1 and 9/4
Trying 11/5 and 9/4
Trying 20/9 and 9/4
The square root of 5 is approxomately 29 / 13 = 2.230769230769231
The true square root of 5 is:2.23606797749979
any help?
[886 byte] By [
rosas101a] at [2007-11-26 20:08:08]

What are you having trouble with?
Do you know how to do it outside of Java?
Do you know basic Java syntax for performing arithmetic?
Do you know the Java constructs for repeating the same sequence of steps multiple times?
Do you know how to use variables in Java?
Do you know how to get user input?
Do you know how to display output?
Do you know how to write and compile and run a bare minimum Java program--for instance, can you get "Hello World" to work?
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/sqrt.html http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
im having problems with performing the arithmetic through java code. I can't seem to understand what ints and doubles i need to use. I know i need to use arithmetics and the math class, but i dont know how to wite the code in order to do it.
Im also having trouble with the area for which Java constructs for repeating the same sequence of steps multiple times. I cant seem to put the arithmetic part and this part together
Post a tiny (<1 page) example program that demonstrates exactly one problem you are having.Ask a specific question about it.
> im having problems with performing the arithmetic> through java code. I can't seem to understand what> ints and doubles i need to use. The same ones you use when you do it by hand.
> Im also having trouble with the area for which Java
> constructs for repeating the same sequence of steps
> multiple times. I cant seem to put the arithmetic
> part and this part together
Well, you want to do the same arithmetic multiple times, but with different values. That means loops and variables.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/while.html
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
> The same ones you use when you do it by hand.I took that to mean problems with mixed arithmetic. But we can only guess until some code is posted.
Which integer will you test?
5
To what accuracy? (input the number of digits of correctness
in the fractional component)
2
The number 5 is between the perfect squares
of 4 and 9. --> this is the part that i do notknow how to get. How do I get the program to print out between what perfect squares the integer entered is between. What code do ined to write for this?
This is the only thig i have so far since i do not know howto make te jump to begin with the arithmetic part >
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
Scanner acc = new Scanner(System.in);
System.out.println("This program gives fractional approxomations to square roots of integers.");
System.out.println();
System.out.println("Which integer will you test?");
while ( ! num.hasNextDouble() )
{
num.nextLine();
System.out.println("Enter a valid number:");
}
System.out.println("To what accuracy? (input the number of digits of correctness in the fractional component)");
while (! acc.hasNextDouble())
{
acc.nextLine();
System.out.println("Enter a valid number:");
}
{
}}}
> Which integer will you test?
> 5
> To what accuracy? (input the number of digits of
> correctness
> in the fractional component)
> 2
>
> The number 5 is between the perfect squares
> of 4 and 9. --> this is the part that i do notknow
> how to get. How do I get the program to print out
> between what perfect squares the integer entered is
> between. What code do ined to write for this?
When you square 1, you get 1.
When you square 2, you get 4. Both of those are still less than 5.
When you squere 3, you get 9. Ah-hah! Now you have the number less than 5, and the number greater than 5.
If you can't figure out how to put that into code, you're still just asking someone else to do it for you. And that likely will not happen.
> How do I get the program to print out
> between what perfect squares the integer entered is
> between. What code do ined to write for this?
Part of your problem may be that you're thinking of it as "how to print out between which perfect squares." That makes it sound like it's a single operation. There are a couple of different tasks here: finding out which perfect squares bracket the number, and printing them.
So for the first part, what part don't you understand?
How would you do it "by hand", without Java?
Which specific, precise steps would you take?
> This is the only thing i have so far since i do not know how to make te jump to begin with the arithmetic part
I would ignore the input/output part since it is fiddly and you can probably
copy an example your teacher has given you, and concentrate on
"the arithmetic part". Some people find it helpful to start with pseudo code.
I'm not sure what your iterative technique is, but if you have flexibility then I'd urge you to consider using continued fractions. For sqrt(5) they yield the sequence2/1 = 29/4 = 2.2538/17 ~= 2.235294118
that is what i need to do. I need to find fractions in order to approximate. I have trouble using the math class.
I know i need to use something of this sort:
double realRoot = Math.sqrt();
double testRoot = Math.floor(realRoot);
but i dont know how to link that with the user's input
using Math.sqrt() would be kind of cheat on your problem @_@Your are asked to find the square root without using ready made square root function isn't it?