## CS
1401, Quiz 4 (based on Test 1), MW 12-1:20 version

**Date:** Wednesday, September 25, 2013

**Name** (please type legibly, ideally in block letters):
______________________________________________________________________
3. To compute the square root of a given number n, Java starts with
some approximation a and then computes the more accurate approximation m as
follows:

1 / n \
m = - * | a + - |
2 \ a /

(For example, when we compute the square root of n = 2.0 and start with
a = 1.0, we get a more accurate approximation m = (1/2) * (1.0 + 2.0/1.0) = 1.5.)
Assuming that n and a are already placed in the corresponding variables of type double, write a
Java code statement for assigning the corresponding value to the variable m of type double. Explain,
step-by-step, which arithmetic operations will be performed first, which next, etc., and
trace the computations on the above example.
Describe two different ways to avoid getting 0 as the result of evaluating 1/2.

6. Suppose that we need to send one more ship, and, correspondingly, one more captain.
If the number of ships is stored in the variable *ships*, and the number of
captains in the variable *captains*, which of the two lines of code leads to a
correct increase in both:
- captains = ships++;
- captains = ++ships;

If originally, before each line,
we had 5 ships and 5 captains, explain what will happen after each of these lines
is implemented by Java. What is a clearer way (different from those above) to increase both values?