Second Summer 2002


Methods (Chapter 7): The warm-ups.
These are questions of the kind you might see on the final or the midterm make-up. I didn't have the time to make them into multiple-choice format and upload them to QuizSite so please make sure that you review them and their solutions before the exams. The pointer to the solutions file is at the end.

Questions:

  1. Give realistic examples of the following:

    1. A method with a double parameter and a double return value.

    2. A method with an int parameter and a double return value.

    3. A method with an int parameter and a String return value.

    4. A method with two double parameters and a boolean return value.

    5. A method with no parameter and an int return value.

    6. A method with an Ellipse2D.Double parameter and a double return value.

    7. A method with a Line2D.Double parameter and a Point2D.Double return value.
    Just describe what these methods do. Do not program them. For example, some answers to the first question are "sine" and "square root".

  2. True or false?

    1. A method has exactly one return statement

    2. A method has at least one return statement

    3. A method has at most one return value

    4. A method with return value void never has a return statement

    5. When executing a return statement, the method exits immediately

    6. A method without parameters always has a side effect.

    7. A method with return value void always has a side effect.

    8. A method without side effects always returns the same value when called with the same parameters

  3. Write detailed method comments. Be sure to describe those conditions under which the method cannot compute its result. Just write the comments, not the methods.
    public static double sqrt(double x)
    public static Point2D.Double
      midpoint(Point2D.Double a, Point2D.Double b) 
    public static double area(Ellipse2D.Double c)
    public static String romanNumeral(int n)
    public static double slope(Line2D.Double a)
    public static boolean isLeapYear(int year)
    public static String weekday(int day)

  4. Consider these methods:
    public static double f(double x)
    {
      return g(x) + Math.sqrt(h(x));
    }
    
    public static double g(double x) { return 4 * h(x); }
    
    public static double h(double x) { return x * x + k(x) - 1; } 
    
    public static double k(double x) { return 2 * (x + 1); } 
    Without actually compiling and running a program, determine the results of the following method calls:
    double x1 = f(2); 
    double x2 = g(h(2));
    double x3 = k(g(2) + h(2));
    double x4 = f(0) + f(1) + f(2); 
    double x5 = f(-1) + g(-1) + h(-1) + k(-1);

  5. A predicate method is a method with return type boolean. Give an example of a predicate method and an example of how to use it.

  6. What is the difference between a parameter value and a return value? What is the difference between a parameter value and a parameter variable? What is the difference between a parameter value and a value parameter?

  7. Ideally, a method should have no side effect. Can you write a program in which no method has a side effect? Would such a program be useful?

  8. What is the difference between a method and a program? The main method and a program?

  9. What preconditions do the following methods from the standard Java library have?
    Math.sqrt
    
    Math.tan
    
    Math.log 
    
    Math.exp 
    
    Math.pow
    
    Math.abs

  10. When a method is called with parameters that violate its precondition, it can terminate, or it can fail safely. Give two examples of library methods (standard, or the library methods used in this book) that fail safely when called with invalid parameters, and give two examples of library methods that terminate.

  11. Consider the following method that is intended to swap the values of two floating point numbers:
    public static void falseSwap(double a, double b) 
    {
      double temp = a; 
      a = b; 
      b = temp; 
    } 
    
    public static void main(String[] args) 
    {
      double x = 3; 
      double y = 4; 
      falseSwap(x, y); 
      System.out.println(x + " " + y); 
    }
    Why doesn't the method swap the contents of x and y?

  12. How can you write a method that swaps two floating-point numbers? Hint: Point2D.Double.

Here's where the solutions are.


Last updated: Jun 16, 2002 by Adrian German for A201