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]
# 1

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?

jverda at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 2
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/sqrt.html http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
DrLaszloJamfa at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 3
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.
rosas101a at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 4
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
rosas101a at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 5
Post a tiny (<1 page) example program that demonstrates exactly one problem you are having.Ask a specific question about it.
DrLaszloJamfa at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 6
> 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.
jverda at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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

jverda at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 8
> 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.
DrLaszloJamfa at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 9

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?

rosas101a at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 10

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

}

{

}}}

rosas101a at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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.

warnerjaa at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 12

> 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?

jverda at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 13

> 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.

DrLaszloJamfa at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 14
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
YAT_Archivista at 2007-7-9 23:10:38 > top of Java-index,Java Essentials,Java Programming...
# 15

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

rosas101a at 2007-7-21 17:51:27 > top of Java-index,Java Essentials,Java Programming...
# 16
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?
Icycoola at 2007-7-21 17:51:27 > top of Java-index,Java Essentials,Java Programming...