NAME
___________________________
Perm. no. _________________
  


CS 10: Introduction to Computer Programming

Final Examination

Closed-Book, 3 hours, Winter 1998
 

General Instructions


Question  Value  Score
1  10   
2  10   
3  15   
4  25   
5 10  
6 30
Total 100   

1. (10 points) For the Color constructors below, indicate whether there is a compile-time error, an execution-time error, or that the constructor will work.  Assume the following declaration and initialization:

 
Constructor
 Error (Compile, Execution, or None)
Color(1, 2, r + 3)
Color(0.9, 0.9, 0.9)
Color(r, r, r)
Color(0, 200, r + 0.5)
Color(0, r, (float) 0.125)
Color(1, 2, 3)
Color(0, (int) r, 225)
Color((float) 2, 1, 0)
Color((int) 256.78, 0, 25)
Color(-1, 0 , 1)
  2. A. (5 points) Describe the difference between a static method and one that is not static.

      B. (5 points) Would it ever make sense to create a private static method?  Explain.

3. A. (7 points) You are given the following declaration.

4.
  1. (3 points) Write a method abs whose only parameter, i, is a double and which returns a double whose value is the absolute value of i.  For example, abs(-3.0) would return a double of value 3.0; abs(3.0) would return a double of value 3.0.
  2. Instead of writing this method, you can give a pre-defined method, if you know one.
     

  3. (10 points) Using the method abs (even if you did not answer part 1), write a method called max that has an array, a, of double as a parameter, and returns a double whose value is the absolute value of the element whose absolute value is maximum over all the elements in a.  For example, if max were passed the array whose values are {-3.0, 4.0, -5.0, 6.0, -7.0}, then max would return a double whose value was 7.0.

  4.  
  5. (10 points) Using the method max (even if you did not answer part 2), write a method maxes that has 2 parameters:
    1. a 2-dimensional array b of doubles
    2. a 1-dimensional array c of doubles
      Method maxes, for each element, i, of c, puts the maximum absolute value of the elements in row b[i] into c[i].  If b[i] does not exist, then set c[i] to -1.  Method maxes has no return value.  For example, given the declarations:

      double c[] = new int[3], b[][] = {{1, 2, 3}, {-1, -2, -3}} 
      Then maxes would put 3 into c[0], 3 into c[1], and -1 into c[2].
       
  6. (2 points) When maxes gets invoked with its 2 array arguments, does the array corresponding to c actually get changed by the maxes method?

  7.  
5.  (5 points) Given the following java declaration and initialization, 6. A.  (20 points) Define a class called Node that will be used to construct a list of nodes, each representing a square.  Each Node has the following private attributes: It has the following methods: B. (10 points) Make the color of a square a public static attribute of the class Node (rather than an attribute of Node objects).  Give the changes that need to be made to the Node class.  How does the resulting class behave differently?