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

Date: Wednesday, September 25, 2013
Name (please type legibly, ideally in block letters): ______________________________________________________________________

3. The following formula enables us to compute the area a of a right triangle with sides x and y:

      1
  a = - xy
      2
Assuming that x and y are already placed in the corresponding variables of type double, write a Java code statement for assigning the corresponding value to the variable a 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. Explain what happens if you simply write xy in your Java code.








































6. Suppose that would like to add 2 to the number of years. If the number of years is stored in the integer variable years, which of the two lines of code leads to a correct increase: If originally, before each line, we had 3 years, explain what will happen after each of these lines is implemented by Java. What is a clearer way (different from those above) to add 2 to the variable years?