CPSC 124, Fall 2001
Quiz #4, October 15

This is the third quiz in CPSC 124: Introductory Programming.

Question 1: For this question, you should write a complete Java subroutine. The name of the subroutine is pythagoras. It has a return value of type double, (so it is really a function). It has two parameters of type double named x and y. The purpose of the function is to compute and return the value of the mathematical expression sqrt(x2+y2), where sqrt is the square root function.


            static double pythagoras( double x, double y ) {
                return Math.sqrt( x*x + y*y );

(The "static" is optional, but makes sense here since this subroutine seems to stand on its own, rather than being part of an object. It's possible to write this in more lines, but unnecessary.)

Question 2: Show how you would use the subroutine described in problem 1) to compute sqrt(32+52).



(This is a minimal correct answer. But this does not show how the result returned by the function is to be used. In an actual program, the result would be used in some way. For example, it could be assigned to a variable:  "answer = pythagoras(3,5);".  Or it might be printed out:  System.out.println("The length of the hypotenuse is " + pythagoras(3,5));

Note that this function would never be called with a statement such as "pythagoras(3,5);" since it doesn't make sense to call this function unless you do something with the return value.)

Question 3: Briefly explain how subroutines can be useful in the bottom-up design of programs and what this has to do with software toolkits.


When solving a problem, the bottom-up approach is to start by designing some useful, low-level components that would be useful to help solve the problem. These components can be combined into more complex components, which can then be used in even more complex components, and so on until the problem is solved. When designing a program, the components can be subroutines. That is, you can start by writing some low-level subroutines, use them in higher-level subroutines, and so on. A toolkit is collection of subroutines that have already been written and tested. These subroutines can be used as black boxes in the design of programs, so that they form the "bottom" of the design. By using toolkits, you can build your program on a higher-level foundation, rather than on the primitive capabilities of raw programming language.

Question 4: Discuss the concept of parameters. What are parameters for? What is the difference between formal parameters and actual parameters ?

(Note: This question is taken directly from the sample quiz for Chapter 4. The following answer is taken directly from the answers to that quiz.)

Answer: Parameters are used for communication between a subroutine and the part of the program that calls the subroutine. If a subroutine is thought of as a black box, then parameters are part of the interface to that black box. Formal parameters are found in the subroutine definition. Actual parameters are found in subroutine call statements. When the subroutine is called, the values of the actual parameters are assigned to the formal parameters before the body of the subroutine is executed.

David Eck, eck@hws.edu