# More exercise on class and loops

• Goal of lab

• Reinforce the concepts of instance variables, parameter variables and local variables
• Reinforce if statements and loops
• Reinforce working with doubles
• Reinforce static methods

• Preparation

```   mkdir ~/cs170/lab9                     # make lab directory
cd /home/cs170001/share/labs/lab9      # Go to share folder with lab9
cp * ~/cs170/lab9                      # Make a copy to your folder
cd ~/cs170/lab9                        # Go back to your lab folder
```

• If you use BlueJ

• If you are using BlueJ for the lab, you must open lab9 as "Non BlueJ"

I am CERTAIN that you have work enough with BlueJ to turn Non-BlueJ Java projects into a BlueJ one by now....

• Lab assignment

• Edit the program file MathToolBox.java that write a static method Sqrt() that returns the square root a floating point number.

• The method Sqrt() has one parameter of the type double

• NOTE: You CANNOT use the Java library function Math.Sqrt() to compute the square root - you must compute the square root using only arithmethic operations (so use only +, -, * and / !!!)

• Algorithm to compute the square root of a number x

• I will describe this with an example: compute the square root of 3 - your program must be able to compute the squarr root for any positive decimal number.

• From Mathematics, we know that a square with sided of length Sqrt(3) has an area of 3.

• We will construct a sequence of rectangles that will slowly converge to a square.

Each rectangle will have area equal to 3 !!!

• The first rectangle has a length

l = 1.

Since the are of this rectangle is 3, the width must be equal to:

w = 3 / l = 3

• Subsequence rectangles are constructed as follows:

• The length of the next rectangle is the average of the length and the width of the previous rectangle.
• The width of the next rectangle is the quotient of 3 and the new length (this will make sure that the area is equal to 3).

• So the second rectangle has:

Length l = (1 + 3)/2 = 2.0

and

Width w = 3/l = 1.5

• And the third rectangle has:

Length l = (2.0 + 1.5)/2 = 1.75

and

Width w = 3/l = 1.71428...

• And the fourth rectangle has:

Length l = (1.75 + 1.71428...)/2 = 1.73214...

and

Width w = 3/l = 1.73195...

• As you can see, the sequence of values of the length (and also the width) converges slowly to the square root of 3.

Make the program stop when the difference between the length and the width is less than 0.0000000001

• NOTE: Your program must print out the value of the variable l while it loops to find the square root.

• Psuedo program

• To help you get started with the lab, here is a psuedo program:

 ``` Compute an initial value for l and w System.out.println(l); while ( Stop Condition is NOT met ) { compute a new value for l; compute a new value for w; System.out.println(l); } ```

• Testing

• Compile with:
```	javac SqrtTest.java
```
• Run program with:
```        java SqrtTest
```
• Sample output 1:
```	cheung@compute(1047)> java SqrtTest
Enter a number: 3
1.0
2.0
1.75
1.7321428571428572
1.7320508100147274
1.7320508075688772

square root of 3.0 = 1.7320508075688772
sqrtX * sqrtX = 2.9999999999999996
```

• Sample output 2:
```        cheung@compute(1047)> java SqrtTest
Enter a number: 5
1.0
3.0
2.3333333333333335
2.238095238095238
2.2360688956433634
2.236067977499978

square root of 5.0 = 2.236067977499978
sqrtX * sqrtX = 5.000000000000843
```

• Turn in:

• Turn in your MathToolBox.java file before 6 PM using the commands:
```	cd ~/cs170/lab9

/home/cs170001/turnin MathToolBox.java  lab9
```