CPSC 124, Winter 1998

Quiz Number 3

This is the third quiz given in CPSC 124: Introductory Programming, Winter 1998. See the information page for that course for more information.

The answers given here are sample answers that would receive full credit. However, they are not necessarily the only correct answers.

Question 1: A "black box" has an interface and an implementation. Explain what is meant by the terms interface and implementation.

Answer: The interface of a black box is its connection with the rest of the world, such as the name and parameters of a subroutine or the dial for setting the temperature on a thermostat. The implementation refers to internal workings of the black box. To use the black box, you need to understand its interface, but you don't need to know anything about the implementation.

Question 2: Define the term static method (also called a se "static subroutine").

Answer: A static method is a method that belongs to the class in which it is defined, rather than to objects created using that class. A static method, when used outside the class where it is defined, is accessed using the name of the class, followed by a period, followed by the name of the method.

Question 3: To compute the square root of x in a Java program, you would use Math.sqrt(x). How do you interpret the name, "Math.sqrt"? What is "Math"? What is "sqrt"?

Answer: Math.sqrt is a static method. Math is the name of the class in which the sqrt method is defined. sqrt is the name of the method itself. "Math.sqrt" is the name for the method when it is called from outside the Math class.

Question 4: Briefly explain how subroutines can be a useful tool in the top-down design of programs.

Answer: Top-down refers to starting from the overall problem to be solved, and breaking it up into smaller problems that can be solved separately. When designing a program to solve the problem, you can simply make up a subroutine to solve each of the smaller problems. Then you can separately design and test each subroutine.

Question 5: Write a complete subroutine definition for a subroutine named min. It should have two parameters of type double, and it should return a value of type double. The value returned should be the smaller of the two parameters. For example, min(3.14,2.72) would be 2.72.

Answer: Here is one possible answer.

               static double min(double x, double y) {
                  if (x < y)
                     return x;
                     return y;

David Eck, 6 February 1998